Do spurious wakeups in Java actually happen?

Viewed 40378

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?

7 Answers

The Wikipedia article on spurious wakeups has this tidbit:

The pthread_cond_wait() function in Linux is implemented using the futex system call. Each blocking system call on Linux returns abruptly with EINTR when the process receives a signal. ... pthread_cond_wait() can't restart the waiting because it may miss a real wakeup in the little time it was outside the futex system call. This race condition can only be avoided by the caller checking for an invariant. A POSIX signal will therefore generate a spurious wakeup.

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.

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!

Answering the OP's question

What can I do to wake this await up spuriously without waiting forever for a random event?

, no any spurious wakeup could wake up this awaiting thread!

Regardless of whether spurious wakeups can or cannot happen on a particular platform, in a case of the OP's snippet it is positively impossible for Condition.await() to return and to see the line "Spurious wakeup!" in the output stream.

Unless you are using very exotic Java Class Library

This is because standard, OpenJDK's ReentrantLock's method newCondition() returns the AbstractQueuedSynchronizer's implementation of Condition interface, nested ConditionObject (by the way, it is the only implementation of Condition interface in this class library), and the ConditionObject's method await() itself checks whether the condition does not holds and no any spurious wakeup could force this method to mistakenly return.

By the the way, you could check it yourself as it is pretty easy to emulate spurious wakeup once the AbstractQueuedSynchronizer-based implementation is involved. AbstractQueuedSynchronizer uses low-level LockSupport's park and unpark methods, and if you invoke LockSupport.unpark on a thread awaiting on Condition, this action cannot be distinguished from a spurious wakeup.

Slightly refactoring the OP's snippet,

public class Spurious {

    private static class AwaitingThread extends Thread {

        @Override
        public void run() {
            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();
            }
        }
    }

    private static final int AMOUNT_OF_SPURIOUS_WAKEUPS = 10;

    public static void main(String[] args) throws InterruptedException {
        Thread awaitingThread = new AwaitingThread();
        awaitingThread.start();
        Thread.sleep(10000);
        for(int i =0 ; i < AMOUNT_OF_SPURIOUS_WAKEUPS; i++)
            LockSupport.unpark(awaitingThread);
        Thread.sleep(10000);
        if (awaitingThread.isAlive())
            System.out.println("Even after " + AMOUNT_OF_SPURIOUS_WAKEUPS + " \"spurious wakeups\" the Condition is stil awaiting");
        else
            System.out.println("You are using very unusual implementation of java.util.concurrent.locks.Condition");
    }
}

, and no matter how hard the unparking(main) thread would try to awake the awaiting thread, the Condition.await() method will never return in this case.

The spurious wakeups on Condition's awaiting methods are discussed in the javadoc of Condition interface . Although it does say that,

when waiting upon a Condition, a spurious wakeup is permitted to occur

and that

it is recommended that applications programmers always assume that they can occur and so always wait in a loop.

but it later adds that

An implementation is free to remove the possibility of spurious wakeups

and AbstractQueuedSynchronizer's implementation of Condition interface does exactly that - removes any possibility of spurious wakeups.

This surely holds true for other ConditionObject's awaiting methods.

So, the conclusion is :

we should always call Condition.await in the loop and check if the condition does not hold, but with standard, OpenJDK, Java Class Library is can never happen. Unless, again, you use very unusual Java Class Library (which must be very very unusual, because another well-known non-OpenJDK Java Class Libraries, currently almost extinct GNU Classpath and Apache Harmony, seems to have identical to standard implementation of Condition interface)

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.

Related