现象
资源类
class NumberDemo{ private int num = 0; private Lock lock = new ReentrantLock(); private Condition condition1 = lock.newCondition(); private Condition condition2 = lock.newCondition(); public void incr(){ lock.lock(); try{ //判断 if(num != 0){ condition1.await(); } //干活 num++; System.out.println(Thread.currentThread().getName() + "\t:\t" + num); //通知 condition2.signal(); }catch(Exception e){ e.printStackTrace(); }finally{ lock.unlock(); } } public void desc(){ lock.lock(); try{ if(num != 1){ condition2.await(); } num--; System.out.println(Thread.currentThread().getName() + "\t:\t" + num); condition1.signal(); }catch(Exception e){ e.printStackTrace(); }finally{ lock.unlock(); } } }
主进程
public static void main(String[] args) { NumberDemo number = new NumberDemo(); new Thread(()->{ for (int i = 0; i < 10; i++){ number.incr(); } },"A").start(); new Thread(()->{ for (int i = 0; i < 10; i++){ number.incr(); } },"B").start(); new Thread(()->{ for (int i = 0; i < 10; i++){ number.desc(); } },"C").start(); new Thread(()->{ for (int i = 0; i < 10; i++){ number.desc(); } },"D").start(); }
结果:
出现了意料之外的结果:-1
原因:
解决方法:
将if改为while来判断
public void incr(){ lock.lock(); try{ //判断 while (num != 0){ condition1.await(); } //干活 num++; System.out.println(Thread.currentThread().getName() + "\t:\t" + num); //通知 condition2.signal(); }catch(Exception e){ e.printStackTrace(); }finally{ lock.unlock(); } }
原文:https://www.cnblogs.com/pikachu511/p/14454571.html