秒懂JAVA线程锁

43 阅读2分钟

问题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");
    }

}