class DeadtDemo{
Thread t1=new Thread(new DeadLock(true));
Thread t2=new Thread(new DeadLock(false));
t1.start();//?????????????
t2.start();//?????????词句为甚么变异无法通过
}
class DeadLock implements Runnable{
private boolean flag;//定义一个标记
public DeadLock(boolean flag){//初始化
this.flag=flag;
}
//死锁
public void run(){
if(flag){
while (true){
synchronized (MyLock.lokca) {
System.out.println("if a");
synchronized (MyLock.lokcb) {
System.out.println("if b");
}
}
}
}
else {
while (true){
synchronized (MyLock.lokcb) {
System.out.println("else b");
synchronized (MyLock.lokca) {
System.out.println("else a");
}
}
}
}
}
}
class MyLock{
static Object lokca=new Object();
static Object lokcb=new Object();
}
|
|