JAVA 死锁

来源:互联网 发布:steam 在mac上打不开 编辑:程序博客网 时间:2024/05/17 00:10


class Locker
{
static Object lockerA=new Object();
static Object lockerB=new Object();
}


class CRun implements Runnable
{
    boolean flag=false;
    public CRun(boolean flag)
{
this.flag=flag;
}


public void run()
{
        if(flag)
{
             synchronized(Locker.lockerA)
{
System.out.println("True    get lockerA");
synchronized(Locker.lockerB)
{
System.out.println("True    get  lockerB");
}
}
}
else
{
 synchronized(Locker.lockerB)
{
System.out.println("False    get lockerB");
synchronized(Locker.lockerA)
{
System.out.println("False    get  lockerA");
}
}
}
}
}


class DeadLockerTest
{
   public static void main(String[] args)
   {
      Thread thread1=new Thread(new CRun(false));
 Thread thread2=new Thread(new CRun(true));


 thread1.start();
 thread2.start();
   }
}
0 0
原创粉丝点击