1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | public class MustDeadLockDemo { public static void main(String[] args) { Object lock1 = new Object(); Object lock2 = new Object(); new Thread( new DeadLockTask(lock1, lock2, true ), "线程1" ).start(); new Thread( new DeadLockTask(lock1, lock2, false ), "线程2" ).start(); } static class DeadLockTask implements Runnable { private boolean flag; private Object lock1; private Object lock2; public DeadLockTask(Object lock1, Object lock2, boolean flag) { this .lock1 = lock1; this .lock2 = lock2; this .flag = flag; } @Override public void run() { if (flag) { synchronized (lock1) { System.out.println(Thread.currentThread().getName() "->拿到锁1" ); try { Thread.sleep( 1000 ); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() "->等待锁2释放..." ); synchronized (lock2) { System.out.println(Thread.currentThread().getName() "->拿到锁2" ); } } } if (!flag) { synchronized (lock2) { System.out.println(Thread.currentThread().getName() "->拿到锁2" ); try { Thread.sleep( 1000 ); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() "->等待锁1释放..." ); synchronized (lock1) { System.out.println(Thread.currentThread().getName() "->拿到锁1" ); } } } } } } |