一个java2个线程的死锁程序 ,跟到老毕编的,但是不知哪儿错了,反复的输出else locka else lockb死锁不了,看了很久了,纠结了很久了,求解脱
class Test5 implements Runnable
{
private boolean flag;
Test5 (boolean falg)
{
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 locka");
synchronized(MyLock.locka)
{
System.out.println("else lockb");
}
}
}
}
}
}
class MyLock
{
static Object locka =new Object();
static Object lockb =new Object();
}
public class Day11_15sisuo {
public static void main(String[] args)
{
Thread t1=new Thread(new Test5(true));
Thread t2=new Thread(new Test5(false));
t1.start();
t2.start();
}
} |
|