/**
* Author: heatdeath
* Date: 2018/7/16
* Desc:
*/
public class DeadLockDemo {
private static final Object LOCK_A = new Object();
private static final Object LOCK_B = new Object();
private static class TaskA extends Thread {
@Override
public void run() {
try {
synchronized (LOCK_A) {
System.out.println(Thread.currentThread() + "I hold the LOCK_A");
Thread.sleep(5000);
System.out.println(Thread.currentThread() + "I am wake up and try to get lock");
synchronized (LOCK_B) {
System.out.println(Thread.currentThread() + "I get the LOCK_B");
}
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
private static class TaskB extends Thread {
@Override
public void run() {
try {
synchronized (LOCK_B) {
System.out.println(Thread.currentThread() + "I hold the LOCK_B");
Thread.sleep(5000);
System.out.println(Thread.currentThread() + "I am wake up and try to get lock");
synchronized (LOCK_A) {
System.out.println(Thread.currentThread() + "I get the LOCK_A");
}
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
public static void main(String[] args) {
new TaskA().start();
new TaskB().start();
}
}
好了,两个线程都因为获取不到指定锁而阻塞