由于T1和T2相互等待对方的资源导致两个线程都无法继续执行下去;
回顾一下死锁的四个必要条件:
1.资源互斥
2.不可剥夺条件
3.请求和保持条件
4.循环等待条件
public class test {
public static void main(String[] args){
Object a=new Object();
Object b=new Object();
new Thread(()->{
synchronized (a){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+":获得a");
synchronized (b){
}
}
},"T1").start();
new Thread(()->{
synchronized (b){
System.out.println(Thread.currentThread().getName()+":获得b");
synchronized (a){
}
}
},"T2").start();
}
}