What happens to waiting thread if notify() is not called? Is this spurious wakeup?
In other words, if the notify() method is called when no other thread is waiting, notify() simply returns and the notification is lost. A thread that later executes the wait() method has to wait for another notification to occur.
You need notify or notifyAll to awaken the thread from its wait state. In your sample the code would enter the wait and stay there (unless interrupted).
The wait() method causes the current thread to wait until another thread invokes the notify() or notifyAll() methods for that object. The notify() method wakes up a single thread that is waiting on that object's monitor. The notifyAll() method wakes up all threads that are waiting on that object's monitor.
8 Answers. Save this answer. Show activity on this post. No -- notify / notifyAll don't release locks like wait does.
If a waiting Thread
is not notified by calling notify()
or notifyAll()
on the object the said thread is waiting on, then any one of the following may happen:
Thread
keeps waiting in the object's wait pool
Thread
becomes runnable if a timeout was specified and the time elapsesThread
gets interrupted and becomes runnable againThread
wakes up for no reason at all i.e. it was neither notified nor interrupted
The last case is known as a spurious wake-up and is one of the reasons why upon wake-up a Thread
should always check whether the condition it was waiting for is true or not. If not, the Thread
should call and go wait()
again.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With