7

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

amitguptageek
  • 537
  • 3
  • 13
  • You mean thread is waking up without notify? – Aniket Thakur Mar 29 '15 at 16:54
  • Yes I have called wait but didn't have coded to notify . – amitguptageek Mar 29 '15 at 16:57
  • spurious wakeups are very rare and unlikely to happen. Are you using `notifyAll()` ? – Aniket Thakur Mar 29 '15 at 17:01
  • 2
    If you want to know what happened to a particular piece of code with `wait()` in it, you should post the code. Spurious wakeups are rare, and not likely to repeat consistently if you run the program again. So if you can reproduce this problem, it is probably not a suprious wakeup. – RealSkeptic Mar 29 '15 at 17:05
  • if you are using overloaded method of `wait()` then that Thread can wake up. other than that its rare case! – Prashant Mar 29 '15 at 17:06

1 Answers1

11

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.

Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89