public class DeadLockTest { public static Integer s1 = 1; public static Integer s2 = 1; public static void main (String args[]){ DeadLockThread1 d1 = new DeadLockThread1(); d1.start(); DeadLockThread2 d2 = new DeadLockThread2(); d2.start(); } } class DeadLockThread1 extends Thread { public void run() { synchronized (DeadLockTest.s1){ try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (DeadLockTest.s2) { System.out.println(Thread.currentThread().getName()+" is running"); } } } } class DeadLockThread2 extends Thread { public void run() { synchronized (DeadLockTest.s2){ try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (DeadLockTest.s1) { System.out.println(Thread.currentThread().getName()+" is running"); } } } }
但是如果将DeadLockTest类中的两个静态变量改为不同的值,就能产生死锁,这是为什么?
EnchantF
相关分类