package lock;
public class DeadLock {
public static void main(String[] args) {
String lock1 = "lock1";
String lock2 = "lock2";
//线程0
new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock1){
System.out.println(Thread.currentThread().getName()+"正在锁1...");
synchronized (lock2){
System.out.println(Thread.currentThread().getName()+"正在锁2...");
}
}
}
}).start();
//线程1
new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock2){
System.out.println(Thread.currentThread().getName()+"正在锁2...");
synchronized (lock1){
System.out.println(Thread.currentThread().getName()+"正在锁1...");
}
}
}
}).start();
}
}
死锁原因: 锁的嵌套使用