public class DeadLockDemo {
public static void main(String[] args) {
//创建线程任务
Task t1 = new Task(true);
Task t2 = new Task(false);
new Thread(t1).start();
new Thread(t2).start();
}
}
class Task implements Runnable{
private boolean flag ;
Task(boolean flag){
this.flag = flag;
}
public void run(){
if(flag){
while(true){
synchronized(MyLock.LOCKA){
System.out.println("if......locka");
synchronized(MyLock.LOCKB){
System.out.println("if......lockb");
}
}
}
}
else{
while(true){
synchronized(MyLock.LOCKB){
System.out.println("else......lockb");
synchronized(MyLock.LOCKA){
System.out.println("else......locka");
}
}
}
}
}
}
class MyLock{
public static final Object LOCKA = new Object();
public static final Object LOCKB = new Object();
}
以上为一个死锁的演示实例。
关注我的微信公众号(曲健磊的个人随笔),观看更多精彩内容: