synchronized锁
Posted rzkwz
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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("打电话"); } }
以上是关于synchronized锁的主要内容,如果未能解决你的问题,请参考以下文章