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