package com.cxt.thread; public class TestDeadLock extends Thread{ boolean b; DeadLock lock; public TestDeadLock(boolean b, DeadLock lock) { super(); this.b = b; this.lock = lock; } public static void main(String[] args) { DeadLock lock = new DeadLock(); TestDeadLock t1 = new TestDeadLock(true, lock); TestDeadLock t2 = new TestDeadLock(false, lock); t1.start(); t2.start(); } @Override public void run() { if(this.b){ lock.m1(); } else lock.m2(); } } class DeadLock { Object o1 = new Object(); Object o2 = new Object(); void m1(){ synchronized(o1){ System.out.println("m1 Lock o1 first"); synchronized(o2){ System.out.println("m1 Lock o2 second"); } } } void m2(){ synchronized(o2){ System.out.println("m2 Lock o2 first"); synchronized(o1){ System.out.println("m2 Lock o1 second"); } } } }
线程1获得A对象的锁。 线程1获得对象B的锁(同时持有对象A的锁)。 线程1决定等待另一个线程的信号再继续。 线程1调用B.wait(),从而释放了B对象上的锁,但仍然持有对象A的锁。 线程2需要同时持有对象A和对象B的锁,才能向线程1发信号。 线程2无法获得对象A上的锁,因为对象A上的锁当前正被线程1持有。 线程2一直被阻塞,等待线程1释放对象A上的锁。 线程1一直阻塞,等待线程2的信号,因此,不会释放对象A上的锁, 而线程2需要对象A上的锁才能给线程1发信号……看代码:
package com.cxt.Lock; import com.cxt.thread.Synchronizer; import com.cxt.thread.TestLock; //lock implementation with nested monitor lockout problem /** * 一个坑爹的嵌套管程锁死,区别于死锁 */ public class Lock { protected MonitorObject monitorObject = new MonitorObject(); protected boolean isLocked = false; public static void main(String[] args) { Lock l = new Lock(); l.isLocked = true; MyRunnable r1 = new MyRunnable(l, 0); MyRunnable r2 = new MyRunnable(l, 0); Thread t1 = new Thread(r1); Thread t2 = new Thread(r2); t1.start(); t2.start(); /* * 時而鎖住,時而釋放,因為另外兩條線程沒有有时捕捉不到isLocked = false */ // for (int i = 0; i < 100; i++) { // l.isLocked = false; // try { // Thread.sleep(10); // } catch (InterruptedException e) { // e.printStackTrace(); // } // } // } public void lock() throws InterruptedException { // 当执行这个方法时,isLocked=true时,其他方法无论执行lock方法还是执行Unlock方法都会导致管程死锁 // 只有手动将isLocked 设置为false才能解决死锁,设置为false时必须让其他线程检测到,所以必须设置时间长一点 synchronized (this) { while (isLocked) { synchronized (this.monitorObject) { this.monitorObject.wait(); } } isLocked = true; } } public void unlock() { synchronized (this) { this.isLocked = false; synchronized (this.monitorObject) { this.monitorObject.notify(); } } } static class MyRunnable implements Runnable { Lock l = null; int i; public MyRunnable(Lock l, int i) { this.l = l; this.i = i; } @Override public void run() { try { if (i % 2 == 0) { this.l.lock(); } else { this.l.unlock(); } } catch (InterruptedException e) { e.printStackTrace(); } } } }
package com.cxt.Lock; public class Lock2{ private boolean isLocked = false; public static void main(String[] args) { Lock2 lock = new Lock2(); MyRunnable r1 = new MyRunnable(lock, true); MyRunnable r2 = new MyRunnable(lock, false); Thread t1 = new Thread(r1); Thread t2 = new Thread(r2); t1.start(); // t2.start(); } public synchronized void lock() throws InterruptedException{ while(isLocked){ wait(); } isLocked = true; } public synchronized void unlock(){ isLocked = false; notify(); } static class MyRunnable implements Runnable{ Lock2 l = null; boolean flag = false; public MyRunnable(Lock2 l, boolean flag) { this.l = l; this.flag = flag; } @Override public void run() { if(flag == true) try { // 如果连续执行两次lock(),就会产生系统无限等待的状态 // 解决方法就是在两次中间执行一次unLock()方法 l.lock(); System.out.println("Lock!"); // l.unlock(); // System.out.println("Unlock!"); l.lock(); System.out.println("Lock!"); } catch (InterruptedException e) { e.printStackTrace(); } else l.unlock(); } } }
死锁的三种形式:一般死锁,嵌套管程锁死,重入锁死,布布扣,bubuko.com
原文:http://blog.csdn.net/dafeng_blog/article/details/38236305