Seeing various locking related question and (almost) always finding the 'loop because of spurious wakeups' terms1 I wonder, has anyone experienced such kind of a wakeup (assuming a decent hardware/software environment for example)?
I know the term 'spurious' means no apparent reason but what can be the reasons for such kind of an event?
(1 Note: I'm not questioning the looping practice.)
Edit: A helper question (for those who like code samples):
If I have the following program, and I run it:
public class Spurious {
public static void main(String[] args) {
Lock lock = new ReentrantLock();
Condition cond = lock.newCondition();
lock.lock();
try {
try {
cond.await();
System.out.println("Spurious wakeup!");
} catch (InterruptedException ex) {
System.out.println("Just a regular interrupt.");
}
} finally {
lock.unlock();
}
}
}
What can I do to wake this await
up spuriously without waiting forever for a random event?
Cameron Purdy wrote a blog post a while back about being hit by the spurious wakeup problem. So yes, it happens
I'm guessing it's in the spec (as a possibility) because of limitations of some of the platforms which Java gets deployed on? although I may be wrong!
The Wikipedia article on spurious wakeups has this tidbit:
Summary: If a Linux process is signaled its waiting threads will each enjoy a nice, hot spurious wakeup.
I buy it. That's an easier pill to swallow than the typically vague "it's for performance" reason often given.
https://stackoverflow.com/a/1461956/14731 contains an excellent explanation of why you need to guard against of spurious wakeups even if the underlying operating system does not trigger them. It is interesting to note that this explanation applies across multiple programming languages, including Java.
To answer the question in the titile - Yes! it does happen.Though the Wiki article mentions a good deal about spurious wakeups a nice explanation for the same that I came across is as follows -
I was reading this answer from Source and found it reasonable enough. Also read
Spurious wakeups in Java and how to avoid them.
PS: Above link is to my personal blog that has additional details about spurious wakeups.
Just to add this. Yes it happens and I spent three days searching for the cause of a multi-threading problem on a 24 core machine (JDK 6). 4 of 10 executions experienced that without any pattern. This never happened on 2 core or 8 cores.
Studied some online material and this is not a Java problem but a general rare but expected behavior.
I have a production system that exhibits this behaviour. A thread waits on a signal that there is a message in the queue. In busy periods, up to 20% of the wakeups are spurious (ie when it wakes there is nothing in the queue). This thread is the only consumer of the messages. It runs on a Linux SLES-10 8-processor box and is built with GCC 4.1.2. The messages come from an external source and are processed asynchronously because there are problems if my system does not read them fast enough.