直接上代码
/**
* @desc 死锁示例
* @author bendantuohai
* @create 2016.12.08 14:36
*/
public class DeadLock {
private Object lock_1 = new Object();
private Object lock_2 = new Object();
public static void main(String[]args){
DeadLock deadLock = new DeadLock();
deadLock.goBaby();
}
public void goBaby(){
new Thread(new RunnableOne()).start();
new Thread(new RunnableTwo()).start();
}
private class RunnableOne implements Runnable{
@Override
public void run() {
synchronized (lock_1){
System.out.println("RunnableOne has Locked One!");
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (lock_2){
System.out.println("RunnableOne has Locked Two!");
}
}
}
}
private class RunnableTwo implements Runnable{
@Override
public void run() {
synchronized (lock_2){
System.out.println("RunnableTwo has Locked Two!");
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (lock_1){
System.out.println("RunnableTwo has Locked One!");
}
}
}
}
}