In Java, why a thread has to compete for the resource after waking up from .wait()?

For example in this classic consumer-producer code snippet:

synchronized (this) {

while (queue.isEmpty()) {

this.wait();

}

queue.remove();

this.notifyAll();

}

The .wait() consumer thread that is notified by producer will wake up and then compete for resource with other consumer threads that are waiting on synchronized (this). This causes the race condition. But why don't simply just guarantee the .wait() consumer to obtain the resource?...

Read More »

By: StackOverFlow - Tuesday, 6 November

Related Posts