public class DeadLockDemo {
public static void main(String[] args) {
final Object lock1 = new Object();
final Object lock2 = new Object();
new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock1){
System.out.println(Thread.currentThread().getName() + "锁住了lock1");
try {
Thread.sleep(5000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (lock2){
System.out.println(Thread.currentThread().getName() + "锁住了lock2");
}
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock2){
System.out.println(Thread.currentThread().getName() + "锁住了lock2");
try {
Thread.sleep(5000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (lock1){
System.out.println(Thread.currentThread().getName() + "锁住了lock1");
}
}
}
}).start();
}
}