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

  • Older News
  • How does thread.join() work conceptually? StackOverFlow (Yesterday) - From the docs:The join method allows one thread to wait for the completion ofanother. If t is a Thread object whose thread is currently executing,t.join();causes the current thread to pause...