秒懂JAVA线程锁

八锁就是关于锁的8个问题

问题1:先打印sms还是call?
答案:先sms,因为synchronized锁是锁对象的,谁先拿到锁谁就先执行

public class Lock1 {
//1. 先打印sms还是call? 答案:先sms,因为synchronized锁是锁对象的,谁先拿到锁谁就先执行
    public static void main(String[] args) {
        //线程间交替执行demo
        Phone phone = new Phone();

        new Thread(()-> {
                phone.sendSMS();
        }).start();
        new Thread(()-> {
                phone.call();

        }).start();

    }

}
class  Phone{

    public synchronized void sendSMS(){
        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("sms");
    }
    public synchronized void  call(){
        System.out.println("call");
    }
}

问题2:1. 先打印hello还是sms?
答案:先hello


public class Lock2 {
//1. 先打印hello还是sms? 答案:先hello
    public static void main(String[] args) {
        //线程间交替执行demo
        Phone2 phone = new Phone2();

        new Thread(()-> {
                phone.sendSMS();
        }).start();

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        phone.hello();

    }

}
class  Phone2{

    public synchronized void sendSMS(){
        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("sms");
    }
    public synchronized void  call(){
        System.out.println("call");
    }
    public  void  hello(){
        System.out.println("hello");
    }
}

问题3:先打印call还是sms?
答:先call后sms,因为锁的是对象,两个对象分别调用就会各自执行,不被锁住


public class Lock3 {
//1. 先打印call还是sms?  答:先call后sms,因为锁的是对象,两个对象分别调用就会各自执行,不被锁住
    public static void main(String[] args) {
        //线程间交替执行demo
        Phone2 phone = new Phone2();
        Phone2 phone2 = new Phone2();

        new Thread(()-> {
                phone.sendSMS();
        }).start();

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        new Thread(()-> {
            phone2.call();
        }).start();


    }

}
class  Phone3{

    public synchronized void sendSMS(){
        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("sms");
    }
    public synchronized void  call(){
        System.out.println("call");
    }
    public  void  hello(){
        System.out.println("hello");
    }
}

问题4: 先打印call还是sms?
答:先sms,因为静态方法锁的是class对象

    public class Lock4 {
//1. 先打印call还是sms?  答:先sms,因为静态方法锁的是class对象
    public static void main(String[] args) {
        //线程间交替执行demo
        Phone4 phone = new Phone4();
        Phone4 phone2 = new Phone4();

        new Thread(()-> {
                phone.sendSMS();
        }).start();


        new Thread(()-> {
            phone2.call();
        }).start();


    }

}
class  Phone4{

    public static synchronized void sendSMS(){
        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("sms");
    }
    public static synchronized void  call(){
        System.out.println("call");
    }
    public  void  hello(){
        System.out.println("hello");
    }
}

问题5:一个静态方法,一个普通方法,先执行call还是sms?
答案:先call,因为静态方法锁的是class对象,call锁的是当前对象,也就是说他俩上的不是同一把锁

public class Lock5 {
//一个静态方法,一个普通方法,先执行call还是sms?,
// 答案:先call,因为静态方法锁的是class对象,call锁的是当前对象,也就是说他俩上的不是同一把锁
    public static void main(String[] args) {
        //线程间交替执行demo
        Phone5 phone = new Phone5();

        new Thread(()-> {
                phone.sendSMS();
        }).start();


        new Thread(()-> {
            phone.call();
        }).start();


    }

}
class  Phone5{

    public  synchronized void sendSMS(){
        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("sms");
    }
    public static synchronized void  call(){
        System.out.println("call");
    }

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值