package example.xcdemo;
/**
* @author MM
* @create 2018-08-10 11:06
**/
public class DeadLockDemo {
private static String resource_a = "A";
private static String resource_b = "B";
public static void main(String[] args) {
deadLock();
}
private static void deadLock() {
Thread threadA = new Thread(new Runnable() {
@Override
public void run() {
synchronized (resource_a) {
System.out.println(Thread.currentThread().getName() + " get resource a: " + System.currentTimeMillis());
try {
Thread.sleep(10);
synchronized (resource_b) {
System.out.println(Thread.currentThread().getName() + " get resource b: " + System.currentTimeMillis());
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
Thread threadB = new Thread(new Runnable() {
@Override
public void run() {
synchronized (resource_b) {
System.out.println(Thread.currentThread().getName() + " get resource a: " + +System.currentTimeMillis());
try {
Thread.sleep(10);
synchronized (resource_a) {
System.out.println(Thread.currentThread().getName() + " get resource b: " + System.currentTimeMillis());
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
threadA.start();
threadB.start();
}
}