import java.util.concurrent.TimeUnit;
/**
* @Author: ^Sparkle。
* @Date: 2020/4/19 15:44
**/
public class deadlock {
private static Object num1 = new Object();
private static Object num2 = new Object();
public static void main(String[] args) {
new Thread(()->{
synchronized (num1){
try {
System.out.println(Thread.currentThread().getName()+"\t锁住num1");
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
//尝试获取o2
synchronized (num2){
System.out.println(Thread.currentThread().getName()+"\t锁住num2");
}
}
},"t1").start();
new Thread(()->{
synchronized (num2){
try {
System.out.println(Thread.currentThread().getName()+"\t锁住num2");
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
//尝试获取o2
synchronized (num1){
System.out.println(Thread.currentThread().getName()+"\t锁住num1");
}
}
},"t2").start();
}
}
参考资料:
https://blog.csdn.net/timo1160139211/article/details/88863963