How exactly wait() and notify() is implemented in Java ? When a thread calls wait() on an object , does object store the thread into a collection within the object? so that when notify() is called it can wake up the thread waiting on it
1
No, Java’s Object class doesn’t maintain a collection of waiting threads directly. When a thread calls wait(), it releases the monitor on that object and enters the wait set associated with the object’s monitor. This wait set is managed by the JVM internally, not stored as a collection within the object itself.
When notify() or notifyAll() is called, the JVM picks one or all threads from the wait set to wake up, depending on which method is called.
Would you like to dive deeper into how the JVM manages these wait sets internally?
David Wilson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.