本帖最后由 yufeiant 于 2012-6-27 23:56 编辑
class Test implements Runnable
{
private boolean flag;
Test(boolean flag)
{
this.flag=flag;
}
public void run()
{
if (flag)
{
while(true){
synchronized(Look.looka)
{
System.out.println("if a");
synchronized(Look.lookb)
{
System.out.println("if b");
}
}
}
}
else
{
while(true){
synchronized(Look.looka)
{
System.out.println("else a");
synchronized(Look.lookb)
{
System.out.println("else b");
}
}
}
}
}
}
class Look
{
static Object looka=new Object();
static Object lookb=new Object();
}
class DeadLookDemo
{
public static void main(String[] args)
{
Thread t1=new Thread(new Test(true));
t1.start();
Thread t2=new Thread(new Test(false));
t2.start();
}
}
//为什么死锁锁不呢??好纠结 |