public class TestDeadLock implements Runnable{ public int flag=1; static Object o1=new Object(); static Object o2=new Object(); public static void main(String[] args){ TestDeadLock t1=new TestDeadLock(); TestDeadLock t2=new TestDeadLock(); t2.flag=0; Thread th1=new Thread(t1); Thread th2=new Thread(t2); th1.start(); th2.start(); } public void run(){ if(flag==1){ synchronized(o1){ try{ Thread.sleep(500); }catch(Exception e){ } synchronized(o2){ System.out.println("th1"); } } } if(flag==0){ synchronized(o2){ try{ Thread.sleep(500); }catch(Exception e){ } synchronized(o1){ System.out.println("th2"); } } } } }