According to a colleague, JVM does not guarantee that when calling “notify” on an object, the correct “wait” will be notified at that time. He says there can be a case when a previous notify which is not valid anymore is delivered at an invalid time.
Is this true? If so, how/why is this, and what use is the wait/notify mechanism if you cannot assume something as basic as this will work?
For java.lang.Object.notify, The Javadoc says:
Here is a pattern to wait for a particular condition:
The counterpart should use
java.lang.Object.notifyAll()to ensure the vivacity of the application. Even if today, it’s only one waiter, after many evolutions of the software, it may be several waiters in the future, sonotifyAll()is more robust thannotify().