并发编程唤醒判断用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 {
        }
    }
posted @ 2020-11-01 21:56  IT夏天  阅读(83)  评论(0编辑  收藏  举报