amitguptageek
amitguptageek

Reputation: 537

What happens to waiting thread if notify() is not called?

What happens to waiting thread if notify() is not called? Is this spurious wakeup?

Upvotes: 7

Views: 1865

Answers (1)

Ravi K Thapliyal
Ravi K Thapliyal

Reputation: 51721

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:

  • the Thread keeps waiting in the object's wait pool
  • the Thread becomes runnable if a timeout was specified and the time elapses
  • the Thread gets interrupted and becomes runnable again
  • the Thread 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.

Upvotes: 11

Related Questions