java锁:第二章:可重入锁和递归锁
代码:
package com.javaliao.backstage;
class Phone{
public synchronized void sendSMS(){
System.out.println(Thread.currentThread().getName()+"\t 发短信");
sendEmail();
}
public synchronized void sendEmail(){
System.out.println(Thread.currentThread().getName()+"\t 发邮件");
}
}
public class Demo {
public static void main(String[] args) {
Phone phone = new Phone();
new Thread(()->{
phone.sendSMS();
},"t1").start();
new Thread(()->{
phone.sendSMS();
},"t2").start();
}
}
控制台:
生活案例:
家里的大门有一把锁,厕所没有上锁。我进了大门了,就不用在厕所上锁了。
ReentrantLock就是把可重入锁
package com.javaliao.backstage;
更多内容请见原文,原文转载自:https://blog.csdn.net/weixin_44519496/article/details/120323146