class Test implements Runnable
{
private boolean flag=true;
public Test(boolean flag)
{
this.flag=flag;
}
public void run()
{
if (flag)
{
synchronized (MyLock.locka)
{
System.out.println(Thread.currentThread().getName()+"...if ...locka");
synchronized (MyLock.lockb)
{
System.out.println(Thread.currentThread().getName()+"...if ...lockb");
}
}
}
else
{
synchronized (MyLock.lockb)
{
System.out.println(Thread.currentThread().getName()+"...if ...lockb");
synchronized (MyLock.locka)
{
System.out.println(Thread.currentThread().getName()+"...if ...locka");
}
}
}
}
}
class MyLock
{
public static final MyLock locka=new MyLock();
public static final MyLock lockb=new MyLock();
}
public class DeadLockDemo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Test a=new Test(true);
Test b=new Test(false);
Thread t1=new Thread(a);
Thread t2=new Thread(b);
t1.start();
t2.start();
}
}
{
private boolean flag=true;
public Test(boolean flag)
{
this.flag=flag;
}
public void run()
{
if (flag)
{
synchronized (MyLock.locka)
{
System.out.println(Thread.currentThread().getName()+"...if ...locka");
synchronized (MyLock.lockb)
{
System.out.println(Thread.currentThread().getName()+"...if ...lockb");
}
}
}
else
{
synchronized (MyLock.lockb)
{
System.out.println(Thread.currentThread().getName()+"...if ...lockb");
synchronized (MyLock.locka)
{
System.out.println(Thread.currentThread().getName()+"...if ...locka");
}
}
}
}
}
class MyLock
{
public static final MyLock locka=new MyLock();
public static final MyLock lockb=new MyLock();
}
public class DeadLockDemo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Test a=new Test(true);
Test b=new Test(false);
Thread t1=new Thread(a);
Thread t2=new Thread(b);
t1.start();
t2.start();
}
}