synchronized 应用于静态方法

来源:互联网 发布:守望先锋英雄技能数据 编辑:程序博客网 时间:2024/06/06 03:32

LZ就是这种臭脾气,就爱做只是搬运工。来喽,各位看官今天来写(chao)点多线程相关的东西。有钱没钱都捧个人场。

  1. 首先讲讲synchronized 用在静态方法是啥样子滴。
    synchronized关键字用在static方法上是对当前*.java文件对应的Class类进行持锁。来个栗子尝尝味:
//定义service类package service;/** * Created by xiaopguo on 2017/9/23. */public class Service {    synchronized public static void printA(){        try{            System.out.println("thread name is:"+Thread.currentThread().getName()+ " in "+System.currentTimeMillis() +" get into printA");            Thread.sleep(5000);            System.out.println("thread name is:"+Thread.currentThread().getName()+ " in "+System.currentTimeMillis() +" out printA");        }catch (InterruptedException e){            e.printStackTrace();        }    }    synchronized public static void printB(){        try{            System.out.println("thread name is " + Thread.currentThread().getName() +" in " + System.currentTimeMillis() +"get into printB");            Thread.sleep(5000);            System.out.println("thread name is "+ Thread.currentThread().getName() + " in " + System.currentTimeMillis() + "out PrintB");        }catch(InterruptedException e){            e.printStackTrace();        }    }}
//定义ThreadA类package srevicethread1;import service.Service;/** * Created by xiaopguo on 2017/9/23. */public class ThreadA extends Thread{    private Service service = new Service();    public ThreadA(Service service){        super();        this.service = service;    }    @Override    public void run(){        service.printA();    }}
//定义ThreadB类package srevicethread1;import service.Service;/** * Created by xiaopguo on 2017/9/23. */public class ThreadB extends Thread {    private Service service = new Service();    public ThreadB(Service service){        this.service = service;    }    @Override    public void run(){        service.printB();    }}
package test.run;import service.Service;import srevicethread1.ThreadA;import srevicethread1.ThreadB;import srevicethread1.ThreadC;/** * Created by xiaopguo on 2017/9/23. */public class Run9 {    public static void main(String[] args){        Service service = new Service();        ThreadA thread1 = new ThreadA(service);        thread1.setName("A");        thread1.start();        ThreadB thread2 = new ThreadB(service);        thread2.setName("B");        thread2.start();    }}

上个结果图:
这里写图片描述

从结果来看,与其他加锁方式没甚没别呀,都实现了同步效果。那你就会问那这与非static方法有什么区别,其实还是有区别的,在static方法上加synchronized方法是给类上锁,而夹在非static方法上是给对象枷锁。

来个例子吧

package service;/** * Created by xiaopguo on 2017/9/23. */public class Service {    synchronized public static void printA(){        try{            System.out.println("thread name is:"+Thread.currentThread().getName()+ " in "+System.currentTimeMillis() +" get into printA");            Thread.sleep(5000);            System.out.println("thread name is:"+Thread.currentThread().getName()+ " in "+System.currentTimeMillis() +" out printA");        }catch (InterruptedException e){            e.printStackTrace();        }    }    synchronized public static void printB(){        try{            System.out.println("thread name is " + Thread.currentThread().getName() +" in " + System.currentTimeMillis() +"get into printB");            Thread.sleep(5000);            System.out.println("thread name is "+ Thread.currentThread().getName() + " in " + System.currentTimeMillis() + "out PrintB");        }catch(InterruptedException e){            e.printStackTrace();        }    }    //new add    synchronized public void printC(){        try{            System.out.println("thread name is " + Thread.currentThread().getName() + " in " + System.currentTimeMillis() + "get into printC");            Thread.sleep(5000);            System.out.println("thread name is " + Thread.currentThread().getName() + " in " + System.currentTimeMillis() + "out printC");        }catch(InterruptedException e){            e.printStackTrace();        }    }}

ThreadA、ThreadB借用上面代码。

//new add ThreadCpackage srevicethread1;import service.Service;/** * Created by xiaopguo on 2017/9/23. */public class ThreadC extends Thread {    private Service service = new Service();    public ThreadC(Service service){        this.service = service;    }    @Override    public void run(){        service.printC();    }}

Run9改成:

package test.run;import service.Service;import srevicethread1.ThreadA;import srevicethread1.ThreadB;import srevicethread1.ThreadC;/** * Created by xiaopguo on 2017/9/23. */public class Run9 {    public static void main(String[] args){        Service service = new Service();        ThreadA thread1 = new ThreadA(service);        thread1.setName("A");        thread1.start();        ThreadB thread2 = new ThreadB(service);        thread2.setName("B");        thread2.start();        ThreadC thread3 = new ThreadC(service);        thread3.setName("C");        thread3.start();    }}

话不多说直接上图:
这里写图片描述
可以看到printA与printC可以异步执行,而printA与printB必须同步执行。异步原因是printA是Class锁而printB是对象锁。

简单说一下对象锁:
1.synchronized + (函数)function 叫做同步synchronized
2.synchronized(this) 语句块 上述两种方式获得的是当前对象锁
3.synchronized(非this对象) 锁定的是指定对象。

原创粉丝点击