7

I wrote a test app that should never stop. It issues t.wait() (t is a Thread object), but I never call notify. Why does this code end? Despite the main thread synchronizing on t, the spawned thread runs, so it doesn't lock this object.

public class ThreadWait {
    public static void main(String sArgs[]) throws InterruptedException {
        System.out.println("hello");
        Thread t = new MyThread();
        synchronized (t) {
            t.start();
            Thread.sleep(5000);
            t.wait();
            java.lang.System.out.println("main done");
        }
    }
}

class MyThread extends Thread {
    public void run() {
        for (int i = 1; i <= 5; i++) {
            java.lang.System.out.println("" + i);
            try {
                Thread.sleep(500);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }
}

The result is that the main thread waits 5 seconds and during this time worker gives its output. Then after 5 seconds are finished, the program exits. t.wait() does not wait. If the main thread wouldn't sleep for 5 seconds (commenting this line), then t.wait() would actually wait until the worker finishes. Of course, join() is a method to use here, but, unexpectedly, wait() does the same thing as join(). Why?

Maybe the JVM sees that, since only one thread is running, there is no chance to notify the main thread and solves the deadlock. If this is true, is it a documented feature?

I'm testing on Windows XP, Java 6.

1
  • What happens when you sleep only for 1 or 2 seconds? Then I bet you'll have the main thread wait for thread t to finish running...
    – Renato
    Jan 27, 2012 at 8:20

2 Answers 2

11

You're waiting on a Thread - and while most objects aren't implicitly notified, a Thread object is notified when the thread terminates. It's documented somewhere (I'm looking for it...) that you should not use wait/notify on Thread objects, as that's done internally.

This is a good example of why it's best practice to use a "private" object for synchronization (and wait/notify) - something which only your code knows about. I usually use something like:

private final Object lock = new Object();

(In general, however, it's cleaner to use some of the higher-level abstractions provided by java.util.concurrent if you can. As noted in comments, it's also a good idea to implement Runnable rather than extending Thread yourself.)

11
  • A number of method in Thread are synchronized on that object and use wait/notify, so spurious wake ups should be expected. Its also good practice not to extend Thread but to use a Runnable which is wrapped by a Thread. This would also avoid this issue. Jan 27, 2012 at 8:31
  • 1
    @PeterLawrey: So long as the OP then used wait/notify on the runnable, not on the thread :) I still prefer to have a "dumb" object which I don't expose anywhere else, personally.
    – Jon Skeet
    Jan 27, 2012 at 8:34
  • Ideally, you want both as you don't know how the code might be changed in the future. IMHO having multiple strategies which are best practice protects your code better. Jan 27, 2012 at 8:38
  • 1
    @PeterLawrey: Right, I'm with you. Totally agreed :)
    – Jon Skeet
    Jan 27, 2012 at 9:04
  • 2
    @Jon, Malcolm found it in Java 7 Thread.join documentation.
    – Jarekczek
    Jun 6, 2013 at 6:58
8

The JavaDoc for wait gives the answer: spurious wakeups are possible. This means the JVM is free to end a call to wait whenever it wants.

The documentation even gives you a solution if you don't want this (which is probably always the case): put the call to wait in a loop and check whether the condition you wait for has become true after every wakeup.

3
  • 3
    @Boris In case of a spurious wakeup, there is no exception. An InterruptedException is thrown only when the thread is interrupted. Jan 27, 2012 at 8:19
  • 2
    I doubt that this is actually a spurious wakeup - I believe it's the behaviour of waiting on Thread in particular; it will be notified when the thread terminates. It's not spurious in the normal sense.
    – Jon Skeet
    Jan 27, 2012 at 8:28
  • 2
    Even without spurious wakeups, with the monitor pattern (or any of its variants) you always want to loop and check for conditions. That prevents problems if you later add another condition (java and C# monitors use one object as both the lock and the condition variable, so you cannot use one condition variable per condition). It also prevents you from proceding if the condition was met, but another thread waiting on the same object went first, and now the condition is no longer true. Jan 27, 2012 at 16:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.