package my;
public class dwdwa {
public static void main(String[] args)throws Exception{
DeadLockThread d1=new DeadLockThread(true);
DeadLockThread d2=new DeadLockThread(false);
new Thread(d1,"chinese").start();
new Thread(d2,"American").start();
}
}
class DeadLockThread implements Runnable{
Object chopsticks=new Object();
Object knifeAndFork=new Object();
private boolean flag;
public DeadLockThread(boolean flag)
{
this.flag=flag;
}
public void run() {
if(flag)
{
while(true) {
synchronized(chopsticks)
{
System.out.println(Thread.currentThread().getName()+"----if----chopsticks");
/try {
Thread.sleep(100);
}catch(Exception e) {
e.printStackTrace();
}/
synchronized(knifeAndFork)
{
System.out.println(Thread.currentThread().getName()+"-----if-----knifeAndFork");
}
}
}
}else {
while(true) {
synchronized(knifeAndFork)
{
System.out.println(Thread.currentThread().getName()+"-----else----knifeAndFork");
synchronized(chopsticks)
{
System.out.println(Thread.currentThread().getName()+"-----else----chopsticks");
}
}
}
}
}
}
为什么死锁不了啊?求大神解释!!!