This question is not about, whether spurious wakeups actually happen, because this was already discussed in full length here: Do spurious wakeups in Java actually happen? Therefore this is also not about, why I do have to put a loop around my wait
Statement. What this is about:
I would like to construct a case, where the spurious wake-up happens. What I've learned so far in the question linked above is this:
If a Linux process is signaled its waiting threads will each enjoy a nice, hot spurious wakeup.
So it seems this will only work a linux machine, in fact I have Ubuntu 11.04 - 64-Bit. I have written a Java program with one thread waiting for a condition, but without loop and another class on which a thread just waits and get notified by another thread. I thought launching all three threads in one JVM would force the case described above, but it seems like this is not the case.
Has anyone else another idea how to construct such a case in Java?
You can't force a spurious wakeup, but to the running thread, a spurious wakeup is indistinguishable from a regular wakeup (the source of the event is different, but the event itself is the same)
To simulate a spurious wakeup, simply call notify();
Calling interrupt()
isn't suitable, because doing so sets the interrupt flag, and after a spurious wakeup, the interrupt flag is not set