synchronized锁


/*
* 8锁:就是关于锁的8个问题
* 先走发短信再走打电话,因为phone里面的两个方法加了synchronized锁
* */
public class Test1 {
public static void main(String[] args) throws InterruptedException {
Phone phone = new Phone();
new Thread(()->{
phone.sendSms();
},"A").start();
TimeUnit.SECONDS.sleep(1);
new Thread(()->{
phone.call();
},"B").start();
}

}

class Phone{
//发短信
public synchronized void sendSms(){
System.out.println("发短信");
}
//打电话
public synchronized void call(){
System.out.println("打电话");
}
}
 1 public class Test1 {
 2     public static void main(String[] args) throws InterruptedException {
 3         Phone phone = new Phone();
 4         new Thread(()->{
 5             phone.sendSms();
 6         },"A").start();
 7         TimeUnit.SECONDS.sleep(1);
 8         new Thread(()->{
 9             phone.call();
10         },"B").start();
11     }
12 
13 }
14 
15 class Phone{
16     //发短信synchronized 锁的对象是方法的调用者
17     public synchronized void sendSms(){
18         try {  //如果加了这句话还是会走发短信,因为在这里还是同一把锁
19             TimeUnit.SECONDS.sleep(3);
20         } catch (InterruptedException e) {
21             e.printStackTrace();
22         }
23         System.out.println("发短信");
24     }
25     //打电话
26     public synchronized void call(){
27         System.out.println("打电话");
28     }
29 }
 1 //在原有的基础上加上play会先执行谁
 2 public class Test1 {
 3     public static void main(String[] args) throws InterruptedException {
 4         Phone phone = new Phone();
 5         new Thread(()->{
 6             phone.sendSms();
 7         },"A").start();
 8         TimeUnit.SECONDS.sleep(1);
 9         new Thread(()->{
10             phone.call();
11         },"B").start();
12         new Thread(()->{
13             phone.play();
14         },"C");
15     }
16 
17 }
18 
19 class Phone{
20     //发短信
21     public synchronized void sendSms(){
22         try {
23             TimeUnit.SECONDS.sleep(3);
24         } catch (InterruptedException e) {
25             e.printStackTrace();
26         }
27         System.out.println("发短信");
28     }
29     //打电话
30     public synchronized void call(){
31         System.out.println("打电话");
32     }
33     public void play(){
34         System.out.println("玩手机");
35     }
玩手机
发短信
打电话
//如果加上static的话,锁的是整个class
public class Test1 {
    public static void main(String[] args) throws InterruptedException {
        Phone phone = new Phone();
        new Thread(()->{
            phone.sendSms();
        },"A").start();
        TimeUnit.SECONDS.sleep(1);
        new Thread(()->{
            phone.call();
        },"B").start();
    }
}
//Phone 只有唯一一个Class对象
class Phone{
    //发短信
    //synchronized 锁的对象是方法的调用者
    //static :静态方法
    //类一加载就有了  Class模板
    public static synchronized void sendSms(){
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("发短信");
    }
    //打电话
    public static synchronized void call(){
        System.out.println("打电话");
    }
}
public class Test1 {//两个调用者和两个对象
    public static void main(String[] args) throws InterruptedException {
       //加两个对象
        Phone phone = new Phone();
        Phone phone1 = new Phone();
        new Thread(()->{
            phone.sendSms();
        },"A").start();
        TimeUnit.SECONDS.sleep(1);
        new Thread(()->{
            phone1.call();
        },"B").start();
    }
}
//Phone 只有唯一一个Class对象
class Phone{
    //发短信
    //synchronized 锁的对象是方法的调用者
    //static :静态方法
    //类一加载就有了  Class模板
    public static synchronized void sendSms(){
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("发短信");
    }
    //打电话
    public static synchronized void call(){
        System.out.println("打电话");
    }
}

发短信

打电话

 1 //1个静态的同步方法,1个普通的同步方法,一个对象
 2 public class Test1 {
 3     public static void main(String[] args) throws InterruptedException {
 4         Phone phone = new Phone();
 5         Phone phone1 = new Phone();
 6         new Thread(()->{
 7             phone.sendSms();
 8         },"A").start();
 9         TimeUnit.SECONDS.sleep(1);
10         new Thread(()->{
11             phone1.call();
12         },"B").start();
13     }
14 }
15 //Phone 只有唯一一个Class对象
16 class Phone{
17 
18     //静态的同步方法,锁的是 Class模板
19     public static synchronized void sendSms(){
20         try {
21             TimeUnit.SECONDS.sleep(3);
22         } catch (InterruptedException e) {
23             e.printStackTrace();
24         }
25         System.out.println("发短信");
26     }
27     //打电话
28     public synchronized void call(){
29         System.out.println("打电话");
30     }
31 }

打电话
发短信

原文地址:https://www.cnblogs.com/rzkwz/p/12613287.html