Synchronized同步静态方法和非静态方法总结

1.Synchronized修饰非静态方法,实际上是对调用该方法的对象加锁,俗称“对象锁”。

Java中每个对象都有一个锁,并且是唯一的。假设分配的一个对象空间,里面有多个方法,相当于空间里面有多个小房间,如果我们把所有的小房间都加锁,因为这个对象只有一把钥匙,因此同一时间只能有一个人打开一个小房间,然后用完了还回去,再由JVM 去分配下一个获得钥匙的人。

情况1:同一个对象在两个线程中分别访问该对象的两个同步方法
结果:会产生互斥。

解释:因为锁针对的是对象,当对象调用一个synchronized方法时,其他同步方法需要等待其执行结束并释放锁后才能执行。

Source类

public class Source {

    public static synchronized void staticRun(Thread thread){
        System.out.println(thread.getName()+ " static source1 得到锁");
        System.out.println("------ static source1 is running ------");
        working();
        System.out.println(thread.getName()+ " static source1 释放锁");
        System.out.println();
    }
    public static synchronized void staticRun2(Thread thread){
        System.out.println(thread.getName()+ " static source2 得到锁");
        System.out.println("------ static source2 is running ------");
        working();
        System.out.println(thread.getName()+ " static source2 释放锁");
        System.out.println();
    }


    public  synchronized void  run(Thread thread){
        System.out.println(thread.getName()+ " source1 得到锁");
        System.out.println("------ source1 is running ------");
        working();
        System.out.println(thread.getName()+ " source1 释放锁");
        System.out.println();
    }


    public  synchronized void  run2(Thread thread){
        System.out.println(thread.getName()+ " 2 得到锁");
        System.out.println("------ 2 is running ------");
        working();
        System.out.println(thread.getName()+ " 2 释放锁");
        System.out.println();
    }

    public static void  working(){
        try{
            Thread.sleep(5000);
        }catch(InterruptedException e){
            e.printStackTrace();
        }
    }

}


Test类

public class Test {


    public static void main(String[] args) {

        Source source = new Source();

        Thread t1 = new Thread(){
            @Override
            public void run() {
                source.run(Thread.currentThread()); //同步实例方法1
            }
        };
        t1.start();

        //线程2
        Thread t2 = new Thread(){
            @Override
            public void run() {
                source.run2(Thread.currentThread()); //同步实例方法2
            }
        };
        t2.start();
    }


    }

执行结果:
在这里插入图片描述

情况2:不同对象在两个线程中调用同一个同步方法

结果:不会产生互斥。

解释:因为是两个对象,锁针对的是对象,并不是方法,所以可以并发执行,不会互斥。形象的来说就是因为我们每个线程在调用方法的时候都是new 一个对象,那么就会出现两个空间,两把钥匙,

public class Test {


    public static void main(String[] args) {

        Source source1 = new Source();
        Source source2 = new Source();

        Thread t1 = new Thread(){
            @Override
            public void run() {
                source1.run(Thread.currentThread()); //同步实例方法1
            }
        };
        t1.start();

        //线程2
        Thread t2 = new Thread(){
            @Override
            public void run() {
                source2.run(Thread.currentThread()); //同步实例方法2
            }
        };
        t2.start();
    }


    }

执行结果:
不互斥
在这里插入图片描述

2.Synchronized修饰静态方法,实际上是对该类对象加锁,俗称“类锁”。

情况1:用类直接在两个线程中调用两个不同的同步方法

结果:会产生互斥。(未用synchronized修饰的静态方法仍可以访问)

解释:因为对静态对象加锁实际上对类(.class)加锁,类对象只有一个,可以理解为任何时候都只有一个空间,里面有N个房间,一把锁,因此房间(同步方法)之间一定是互斥的。

注:上述情况和用单例模式声明一个对象来调用非静态方法的情况是一样的,因为永远就只有这一个对象。所以访问同步方法之间一定是互斥的。

在这里插入图片描述

情况2:一个对象在两个线程中分别调用一个静态同步方法和一个非静态同步方法

结果:不会产生互斥。

解释:因为虽然是一个对象调用,但是两个方法的锁类型不同,调用的静态方法实际上是类对象在调用,即这两个方法产生的并不是同一个对象锁,因此不会互斥,会并发执行。

public class Test {


    public static void main(String[] args) {

        Source source = new Source();

        Thread t1 = new Thread(){
            @Override
            public void run() {
                source.run(Thread.currentThread()); //同步实例方法1
            }
        };
        t1.start();

        //线程2
        Thread t2 = new Thread(){
            @Override
            public void run() {
                source.staticRun(Thread.currentThread()); //同步静态实例方法2
            }
        };
        t2.start();
    }


    }

在这里插入图片描述

总结:

1.对象锁钥匙只能有一把才能互斥,才能保证共享变量的唯一性

2.在静态方法上的锁,和 实例方法上的锁,默认不是同样的,如果同步需要制定两把锁一样。

3.关于同一个类的方法上的锁,来自于调用该方法的对象,如果调用该方法的对象是相同的,那么锁必然相同,否则就不相同。比如 new A().x() 和 new A().x(),对象不同,锁不同,如果A的单利的,就能互斥。

4.静态方法加锁,能和所有其他静态方法加锁的 进行互斥

5.静态方法加锁,和xx.class 锁效果一样,直接属于类的
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CodingPeppa

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值