Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Do JVM notifies on waiting threads?

 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !!!

 
Henry Wong
author
Marshal
Pie
Posts: 21385
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry, it's a good response! And Sahil have look on this regarding join() method.
 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21385
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic