Search code examples
javasynchronized

where are the threads waken up from Object.notifyAll and failing to get the lock?


Below is how monitor works: monitor

  • Threads in wait set are waken up when Object.notifyAll() is called.
  • Only one of them get the lock, while the others are blocked again.
  • So where do the blocked threads go?Will they go back to wait set or go to entry set?Or this is managed by the os, because monitor depends on the os MutexLock?

Solution

  • The monitor is a fundamental conception that you should understand. It is better to read about it somewhere.

    In short, I could say that there are some major principles:

    1. If a thread go into the synchronized block - the monitor of synchronized object is blocked and all other threads couldn't execute the synchronized block (they are in the Entry Set).
    2. If you call wait() method on synchronized object, then the thread go to Wait Set
    3. If you call notify()/notifyAll() method on synchronized object, it means that one/all thread(s) go to Entry Set from Wait Set.

    The answer to your question is - when you call notifyAll() method, all threads from Wait Set go to Entry Set.