This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Do JVM notifies on waiting threads? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Do JVM notifies on waiting threads?" Watch "Do JVM notifies on waiting threads?" New topic
Author

Do JVM notifies on waiting threads?

Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
Please do consider the following Program

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??

Please help !!!



SCJP 6.0 96%

(Connecting the Dots ....)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18756
    
  40


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.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Thanks Henry, it's a good response! And Sahil have look on this regarding join() method.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
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 ???

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18756
    
  40

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.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Do JVM notifies on waiting threads?