IntelliJ Java IDE
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Misunderstanding with the wait() method. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Professional Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Misunderstanding with the wait() method." Watch "Misunderstanding with the wait() method." New topic
Author

Misunderstanding with the wait() method.

Vadim Vararu
Ranch Hand

Joined: Jan 03, 2009
Posts: 144


Could you please explain, how it comes that i get printed the actual thread's value in the end, if, normally, the current thread should wait the invocation thread.notify() or thread.notifyAll() and it is not in the code? Thank you!


If you think you've done too much, usually it means you've done too few.
John Stark
Ranch Hand

Joined: Jul 19, 2011
Posts: 165
Hm, for wait() it says 'Causes the current thread to wait until ...'. I guess it is the main thread which is waiting in your code. With the wait() in your code the printout is 999999, so the main thread waits until the Thread 'thread' has finished. If you remove the wait() then the printout is something like '143' on my computer, so execution of the main thread immediately continues after doing thread.start(). Not sure why there is no notify() necessary.

John
Vadim Vararu
Ranch Hand

Joined: Jan 03, 2009
Posts: 144
John Stark wrote:Hm, for wait() it says 'Causes the current thread to wait until ...'. I guess it is the main thread which is waiting in your code. With the wait() in your code the printout is 999999, so the main thread waits until the Thread 'thread' has finished. If you remove the wait() then the printout is something like '143' on my computer, so execution of the main thread immediately continues after doing thread.start(). Not sure why there is no notify() necessary.

John


That's the problem. Documentations says that it will wait until any thread owning the block on the "thread" object will notify.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 2771

Documentation also says that wait() may return at any moment, subject to spurious wakeups. It seems that another thread terminating often causes such a spurious wakeup.

This behaviour is not guaranteed to happen though, so the results of that program are undefined.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 14613

Vadim Vararu wrote:
John Stark wrote:Hm, for wait() it says 'Causes the current thread to wait until ...'. I guess it is the main thread which is waiting in your code. With the wait() in your code the printout is 999999, so the main thread waits until the Thread 'thread' has finished. If you remove the wait() then the printout is something like '143' on my computer, so execution of the main thread immediately continues after doing thread.start(). Not sure why there is no notify() necessary.

John


That's the problem. Documentations says that it will wait until any thread owning the block on the "thread" object will notify.



What is happening is you are colliding with some other thread that is doing notification -- basically, don't call wait() on the thread object as that is being used internally to implement the join() mechanism.

Use a different object for notifications, and it should work as expected.

Henry

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Mallik Kumar
Greenhorn

Joined: Nov 26, 2011
Posts: 12
You have to obtain a lock on an object rather than a thread.
Vadim Vararu
Ranch Hand

Joined: Jan 03, 2009
Posts: 144
Mallik Kumar wrote:You have to obtain a lock on an object rather than a thread.


Isn't a thread an object?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 14613

Vadim Vararu wrote:
Mallik Kumar wrote:You have to obtain a lock on an object rather than a thread.


Isn't a thread an object?


Yes. The Thread instance is an object. And as already mentioned, the problem is that that object is already being used by the library internally for notifications.

Henry
 
 
subject: Misunderstanding with the wait() method.
 
Threads others viewed
Thread question
Question on Thread/sychronized
another question from nikos
Thread-doubt on wait
Problem in Threading - 2
IntelliJ Java IDE