Query : Main thread is waiting for Object t which unfortunately can not notify because it is already not-alive. But inspite of this fact, i am getting ouput X,
which conveys me that JVM notifies iff thread waits on certain object which is already dead. Is this conclusion true or i am misleading somewhere??
What you are seeing is an implementation detail -- specifically, the implementation detail of the join() method. The join() method uses a combination of checking if the thread is alive and calling the wait() method. And it calls the wait() method of the Thread object. When a thread terminates, part of the cleanup process is to call the notifyAll() method, after the thread has been set to no longer alive.
Since you are just using a Thread object, with no runnable, it will just quickly terminate. And the cleanup process will send the notification that is waking up your main thread.
Sahil Kapoor wrote:Sorry , but not getting it clearly...
If suppose Thread A is waiting on Object O and O never calls notify or notifyALL......Then i conclude that Thread A would never be Runnable and consequently running...
Is this true ???
If it is ....What is cleanup process is all about ???
It's simple. There are TWO things going on that is using the Thread object for notification. Your stuff, and the built in libraries, which is using it to do implement join(). And you two are stepping on each other.
My previous post was explaining the other thing -- not what your code is doing.