Fork me on GitHub

Deadlock_synchromized-Java_se

class Test implements Runnable
{
private boolean flag;
Test(boolean flag)
{
this.flag = flag;
}
public void run()
{
if(flag)
{
synchronized(MyLock.locka)
{
System.out.println("if..locka");
synchronized(MyLock.lockb)
{
System.out.println("if..lockb");
}
}
}
else
{
synchronized(MyLock.lockb)
{
System.out.println("else..lockb");
synchronized(MyLock.locka)
{
System.out.println("else..locka");
}
}
}
}
}
class MyLock
{
public static final MyLock locka = new MyLock();
public static final MyLock lockb = new MyLock();
}
class DeadLockTest
{
public static void main(String[] args)
{
Test a = new Test(true);
Test b = new Test(false);
Thread t1 = new Thread(a);
Thread t2 = new Thread(b);
t1.start();
t2.start();

}
}

posted @ 2016-04-13 17:51  gouermazi  阅读(190)  评论(0编辑  收藏  举报