What is the meaning of spurious wakeups in Java? Why they are so dangerous? Can you explain it with an example?
Asked
Active
Viewed 197 times
1 Answers
0
The JVM is allowed to wake a waiting thread without another thread calling its notify()
method - a so called "spurious wakeup".
If you don't consider this possibility, you may proceed with processing when the wait state has not been achieved, leading to incorrect behaviour.
The correct approach when woken up is to first check that the state your thread is waiting on has actually been achieved, otherwise return to waiting by immediately calling wait()

Bohemian
- 412,405
- 93
- 575
- 722
-
1@Risiko It's a non-trivial subject. Read the *Java Concurrency in Practice* by Brian Goetz - it has a whole chapter devoted to this issue. – Bohemian Jul 05 '13 at 05:10