3

I have one question regarding wait/notify based threads interaction.

The output of the below code is Im. How output can be Im as there is no other thread calling notify() on Thread object. Is it like JVM implicitly calls notify() in above such scenarios where you try to wait on Thread class instance.

A thread operation gets stuck when it waits without receiving any notification. Now what if I wait on Thread class instance wait(). For e.g.

public class WaitingThread {
    public static void main(String[] args) {
        Thread t1 = new Thread();
        t1.start();
        synchronized (t1) {
            try {
                t1.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("Im");
    }
}
Gray
  • 115,027
  • 24
  • 293
  • 354
Hitesh
  • 703
  • 1
  • 9
  • 14

1 Answers1

10

Your program is leaving wait() because the thread is finishing immediately. This is a byproduct of the Thread library that the Thread object is notified when the thread finishes. This is how join() works but it is not behavior that should be relied on since it is internal to the thread sub-system.

If you are trying to wait for the thread to finish then you should be using the t1.join() method instead.

Your thread is finishing immediately because it does not have a run() method defined so it is started and finishes. Really it is a race condition and the thread that is started could finish before the main thread gets to the wait() method call. You can see this if you put a short sleep (maybe 10ms) after the start() is called. Then you can see that you program will sit in wait() forever.

Gray
  • 115,027
  • 24
  • 293
  • 354
  • So is it like JVM internally notifies in such scenarios where you try to wait on Thread instance as you will get the same behavior even if you override run() in Thread class implementation and call wait on that particular instance. – Hitesh May 03 '13 at 15:36
  • 2
    To the downvoter: this behavior is documented since Java 7 : http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#join%28long%29 – JB Nizet May 03 '13 at 15:37
  • Uh yes but again, I do not think it is defined behavior. That is a byproduct of the way the JVM works and should not be used. You should use `join()` if you wait to wait for a thread to finish @user1841293. – Gray May 03 '13 at 15:37
  • @Gray: I agree with that. The documentation recommends not using wait on threads. My goal was just to prove that your answer was correct by providing a reliable reference. – JB Nizet May 03 '13 at 15:41
  • No, sure sure @JBNizet. My response was to the OP, not you. I hadn't realized it was in the javadocs. – Gray May 03 '13 at 15:42
  • 1
    I just came across this scenario incidentally. I also believe in using Runnable instead of Thread. But the above behavior shocked me a bit. Thanks a lot for your sharing your thoughts... – Hitesh May 03 '13 at 15:44
  • 1
    From java docs: It is recommended that applications not use wait, notify, or notifyAll on Thread instances. – Hitesh May 03 '13 at 15:48