静态同步方法的锁 是.class对象

    @Test
    public void test() throws InterruptedException {
        //那个被多个线程来共同操作的对象   这次试验使用静态方法了  。。。  所以就不用new 出来对象了。。。
        //Class thisLock = ThisLock.class;
        //线程 t1
        Thread t1 = new Thread("t1"){
            @Override
            public void run() {
                try {
                    ThisLock.m1();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        //线程 t2
        Thread t2 = new Thread("t2"){
            @Override
            public void run() {
                try {
                    ThisLock.m2();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };

        //--------------------------------------------------

        t1.start();
        t2.start();
        //加入join是因为junit在执行完测试方法 就关闭jvm了  所以其他线程还没运行完就意外退出了。。。。
        t1.join();
        t2.join();

    }

    public static void print(Object o){
        System.out.println(o);
    }
public class ThisLock {

    //方法m1
    public static synchronized void m1() throws InterruptedException {
        int i= 0;
        while (true){
            Thread.sleep(1_000);
            print(Thread.currentThread().getName()+"------m1------value"+String.valueOf(i++));
            if(i==5){
                break;
            }
        }
    }
    //方法m2
    public  static synchronized void m2() throws InterruptedException {
        int i= 0;
        while (true){
            Thread.sleep(1_000);
            print(Thread.currentThread().getName()+"------m2------value"+String.valueOf(i++));
            if(i==5){
                break;
            }
        }
    }
    private static void print(Object o){
        System.out.println(o);
    }

}

执行结果:

t2------m2------value0
t2------m2------value1
t2------m2------value2
t2------m2------value3
t2------m2------value4
t1------m1------value0
t1------m1------value1
t1------m1------value2
t1------m1------value3
t1------m1------value4

怎么证明静态同步方法的锁 是class???  我们只要对m2 进行一下改造就可以了

    //方法m2
    public  static  void m2() throws InterruptedException {
        synchronized (ThisLock.class){
            int i= 0;
            while (true){
                Thread.sleep(1_000);
                print(Thread.currentThread().getName()+"------m2------value"+String.valueOf(i++));
                if(i==5){
                    break;
                }
            }
        }
    }

继续运行程序 结果如下:

t1------m1------value0
t1------m1------value1
t1------m1------value2
t1------m1------value3
t1------m1------value4
t2------m2------value0
t2------m2------value1
t2------m2------value2
t2------m2------value3
t2------m2------value4

所以也从另一个角度证明了 静态同步方法的锁是 class对象

如果我们把同步代码块监听的对象改了呢 ????? 如下

    //方法m2
    public  static  void m2() throws InterruptedException {
        synchronized (new String("lock")){
            int i= 0;
            while (true){
                Thread.sleep(1_000);
                print(Thread.currentThread().getName()+"------m2------value"+String.valueOf(i++));
                if(i==5){
                    break;
                }
            }
        }
    }

继续运行程序 结果如下:

t2------m2------value0
t1------m1------value0
t1------m1------value1
t2------m2------value1
t1------m1------value2
t2------m2------value2
t2------m2------value3
t1------m1------value3
t1------m1------value4
t2------m2------value4

扫描二维码关注公众号,回复: 6420961 查看本文章

交替运行  谁也不影响谁   证明是两把锁监听两个方法 所以多线程访问这两个方法 是互相独立的!!

猜你喜欢

转载自blog.csdn.net/u013317653/article/details/89227719
今日推荐