并发编程唤醒判断用while

为什么判定用while不用if

用if可能会导致 不应该唤醒的时候程序继续执行如notifyAll唤醒所有进程 但是当前方法的条件不满足唤醒条件还继续向下执行

  1. 部分代码 完整代码参考:码云
public void demo() {
        synchronized (this) {
            this.notifyAll();
        }
}
 public synchronized boolean put(E e) throws InterruptedException {
        checkNotNull(e);
        try {
            /**
             * 为什么用while不用if 在多线程等待判断中要用while进行判断
             *  
             */
            while (count == items.length)
                this.wait();
            items[putIndex] = e;
            if (++putIndex == items.length)
                putIndex = 0;
            count++;
            this.notifyAll();

            return true;
        } finally {
        }
    }
原文地址:https://www.cnblogs.com/idcode/p/14551404.html