如果我们在 wait 之前立即使用 notify 会有什么缺点

what will be the disadvantage if we will use notify immediately before wait

我正在读这个 Java: notify() vs. notifyAll() all over again。 xagyg 在那里给出了一个很好的例子。我只想知道如果像下面这样把notify放在wait之前,能解决死锁问题吗?请解释。

while (buf.size()==MAX_SIZE) {
      notify();
      wait(); // called if the buffer is full (try/catch removed for brevity)

}

while (buf.size()==0) {
    notify();
    wait(); // called if the buffer is empty (try/catch removed for brevity)
    // X: this is where C1 tries to re-acquire the lock (see below)
}

我想我明白了,如果我们先通知,那么可能所有线程都会醒来并尝试访问同步函数,但由于未调用 wait,因此它仍处于锁定状态。只有调用 wait 才会释放锁,所以会再次死锁。