死锁
public class DeadLockTest{
private static Object left=new Object();
private static Object right=new Object();
public static void leftRight(){
synchronized(left){
System.out.println("leftRight:left lock,threadId:"+Thread.currentThread().getId());
sleep(100);
synchronized(right){
System.out.println("leftRight:right lock,threadId:"+Thread.currentThread().getId());
}
}
}
public static void RightLeft(){
synchronized(right){
System.out.println("rightLeft:right lock,threadId:"+Thread.currentThread().getId());
sleep(100);
synchronized(left){
System.out.println("rightLeft:left lock,threadId:"+Thread.currentThread().getId());
}
}
}
private static void sleep(long time){
try{
Thread.sleep(time);
}catch(InterruptedException e){
e.printStackTrace();
}
}
public static void main(String[] args){
ExecutorService executorService=Executors.newFixedThreadPool(10);
Runnable command1=new Runnable(){
@Override
public void run(){leftRight();}
};
Runnable command2=new Runnable(){
@Override
public void run(){rightLeft();}
};
executorService.execute(command1);
executorService.execute(command2);
executorService.shutdown();
}
}