死锁

来源:互联网 发布:游戏制作软件大全 编辑:程序博客网 时间:2024/06/05 19:59

class Test implements Runnable
{
 private boolean flag;
 Test(boolean flag)
 {
  this.flag = flag;
 }
 public void run ()
 {
  if(flag)
  {
   synchronized(MyLock.locka)
   {
    System.out.println("iflocka");
    synchronized(MyLock.lockb)
    {
     System.out.println("iflockb");
    }
   }
  }
  else
  {
   synchronized(MyLock.lockb)
   {
    System.out.println("elselockb");
    synchronized(MyLock.locka)
    {
     System.out.println("elselocka");
    }
   }
  }
 }
}

class MyLock
{
 static Object locka = new Object();
 static Object lockb = new Object();
}
class DeadLockTest
{
 public static void main(String[] args)
 {
  Thread t1 = new Thread (newTest(true));
  Thread t2 = new Thread (newTest(false));
  t1.start;
  t2.start;
 }
}