JUC-关于Lock的8锁问题

JUC-Lock的8锁问题

Lock的8锁问题,即关于锁的8个问题,以下共展示了8种情况下的8个问题,更有利于理解锁的这个概念

  • 标准情况下,两个线程先打印发短信还是打电话?

    import java.util.concurrent.TimeUnit;
    
    public class Lock1 {
        public static void main(String[] args) {
            Phone1 phone = new Phone1();
            new Thread(()->{
                phone.sendSms();
            },"A").start();
    
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    
            new Thread(()->{
                phone.call();
            },"B").start();
        }
    }
    
    class Phone1{
        public synchronized void sendSms(){
            System.out.println("发短信");
        }
    
        public synchronized void call(){
            System.out.println("打电话");
        }
    }
    

在这里插入图片描述

  • 发短信延迟4秒,两个线程先打印发短信还是打电话?

    import java.util.concurrent.TimeUnit;
    
    public class Lock2 {
        public static void main(String[] args) {
            Phone2 phone = new Phone2();
            //锁的存在
            new Thread(()->{
                phone.sendSms();
            },"A").start();
    
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            new Thread(()->{
                phone.call();
            },"B").start();
        }
    }
    
    class Phone2{
        public synchronized void sendSms(){
            try {
                TimeUnit.SECONDS.sleep(4);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("发短信");
        }
        public synchronized void call(){
            System.out.println("打电话");
        }
    }
    

在这里插入图片描述

以上例子表明,普通的同步方法锁的是new出来的对象,同一个对象调用的两个方法,谁先拿到谁先执行

  • 普通方法和普通的同步方法,先执行发短信还是hello?

    import java.util.concurrent.TimeUnit;
    
    public class Lock3{
        public static void main(String[] args) {
            Phone3 phone = new Phone3();
            new Thread(()->{
                phone.sendSms();
            },"A").start();
    
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    
            new Thread(()->{
                phone.hello();
            },"B").start();
        }
    }
    
    class Phone3{
        public synchronized void sendSms(){
            try {
                TimeUnit.SECONDS.sleep(4);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("发短信");
        }
    
        public synchronized void call(){
            System.out.println("打电话");
        }
    
        public void hello(){
            System.out.println("hello");
        }
    }
    

在这里插入图片描述

  • 两个对象,两个同步方法,发短信还是打电话?

    import java.util.concurrent.TimeUnit;
    
    public class Lock4{
        public static void main(String[] args) {
            Phone4 phone1 = new Phone4();
            Phone4 phone2 = new Phone4();
    
            new Thread(()->{
                phone1.sendSms();
            },"A").start();
    
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    
            new Thread(()->{
                phone2.call();
            },"B").start();
        }
    }
    
    class Phone4{
        public synchronized void sendSms(){
            try {
                TimeUnit.SECONDS.sleep(4);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("发短信");
        }
    
        public synchronized void call(){
            System.out.println("打电话");
        }
        
        public void hello(){
            System.out.println("hello");
        }
    }
    

在这里插入图片描述

  • 两个静态的同步方法,只有一个对象,先打印发短信还是打电话?

    import java.util.concurrent.TimeUnit;
    
    public class Lock5 {
        public static void main(String[] args) {
    
            Phone5 phone = new Phone5();
    
            new Thread(()->{
                phone.sendSms();
            },"A").start();
    
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    
            new Thread(()->{
                phone.call();
            },"B").start();
        }
    }
    
    class Phone5{
        public static synchronized void sendSms(){
            try {
                TimeUnit.SECONDS.sleep(4);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("发短信");
        }
    
        public static synchronized void call(){
            System.out.println("打电话");
        }
    }
    

在这里插入图片描述

  • 两个对象,两个静态的同步方法,先打印发短信还是打电话?

    import java.util.concurrent.TimeUnit;
    
    public class Lock6 {
        public static void main(String[] args) {
            Phone6 phone1 = new Phone6();
            Phone6 phone2 = new Phone6();
    
            new Thread(()->{
                phone1.sendSms();
            },"A").start();
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            
            new Thread(()->{
                phone2.call();
            },"B").start();
        }
    }
    
    class Phone6{
        public static synchronized void sendSms(){
            try {
                TimeUnit.SECONDS.sleep(4);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("发短信");
        }
    
        public static synchronized void call(){
            System.out.println("打电话");
        }
    }
    

在这里插入图片描述

  • 1个静态的同步方法,1个普通的同步方法,只有一个对象,先打印发短信还是打电话?

    import java.util.concurrent.TimeUnit;
    
    public class Lock7  {
        public static void main(String[] args) {
            Phone7 phone = new Phone7();
    
            new Thread(()->{
                phone.sendSms();
            },"A").start();
    
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    
            new Thread(()->{
                phone.call();
            },"B").start();
        }
    }
    
    class Phone7{
        public static synchronized void sendSms(){
            try {
                TimeUnit.SECONDS.sleep(4);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("发短信");
        }
    
        public synchronized void call(){
            System.out.println("打电话");
        }
    }
    

在这里插入图片描述

  • 1个静态的同步方法,1个普通的同步方法,两个对象,先打印发短信还是打电话?

    import java.util.concurrent.TimeUnit;
    
    public class Lock8  {
        public static void main(String[] args) {
            Phone8 phone1 = new Phone8();
            Phone8 phone2 = new Phone8();
    
            new Thread(()->{
                phone1.sendSms();
            },"A").start();
    
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    
            new Thread(()->{
                phone2.call();
            },"B").start();
        }
    }
    
    class Phone8{
        public static synchronized void sendSms(){
            try {
                TimeUnit.SECONDS.sleep(4);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("发短信");
        }
    
        public synchronized void call(){
            System.out.println("打电话");
        }
    }
    

在这里插入图片描述

总结

锁有两种,一种通过new对象,锁的是调用者,另一种通过static修饰的静态方法,锁的是Class,这是全局唯一的一个模板

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

野猫爱吃鱼

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

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

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

打赏作者

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

抵扣说明:

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

余额充值