class DeadLockDemo
{
public static void main(String[] args)
{
Thread t1 = new Thread( new Test( true));
Thread t2 = new Thread( new Test( false));
t1.start();
t2.start();
}
}
class Test implements Runnable
{
private boolean flag;
public Test( boolean flag)
{
this.flag = flag;
}
public void run()
{
if(flag)
{
synchronized(Locks.object1)
{
System.out.println("if object1");
synchronized(Locks.object2)
{
System.out.println("if object2");
}
}
}
else
{
synchronized(Locks.object2)
{
System.out.println("else object2");
synchronized(Locks.object1)
{
System.out.println("else object1");
}
}
}
}
}
class Locks
{
static Object object1 = new Object();
static Object object2 = new Object();
}
{
public static void main(String[] args)
{
Thread t1 = new Thread( new Test( true));
Thread t2 = new Thread( new Test( false));
t1.start();
t2.start();
}
}
class Test implements Runnable
{
private boolean flag;
public Test( boolean flag)
{
this.flag = flag;
}
public void run()
{
if(flag)
{
synchronized(Locks.object1)
{
System.out.println("if object1");
synchronized(Locks.object2)
{
System.out.println("if object2");
}
}
}
else
{
synchronized(Locks.object2)
{
System.out.println("else object2");
synchronized(Locks.object1)
{
System.out.println("else object1");
}
}
}
}
}
class Locks
{
static Object object1 = new Object();
static Object object2 = new Object();
}