- class Lock
- { public static Object obj1 = new Object();
- public static Object obj2 = new Object();
- }
- class DeadLockTest implements Runnable
- { private boolean flag;
- DeadLockTest(boolean flag)
- { this.flag = flag;
- }
- public void run()
- { if(flag)
- { synchronized(Lock.obj1)
- { System.out.println("if---obj1");
- synchronized(Lock.obj2)
- { System.out.println("if---obj2");
- }
- }
- }
- else
- { synchronized(Lock.obj2)
- { System.out.println("else---obj2");
- synchronized(Lock.obj1)
- { System.out.println("if---obj1");
- }
- }
- }
- }
- }
- public class Main
- { public static void main(String[] args)
- { Thread t1 = new Thread(new DeadLockTest(true));
- Thread t2 = new Thread(new DeadLockTest(false));
- t1.start();
- t2.start();
- }
- }
复制代码 |