Win a copy of Microservices in Action this week in the Web Services forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Misunderstanding with the wait() method.  RSS feed

 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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

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.
 
Bartender
Posts: 9501
184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
author
Posts: 23810
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to obtain a lock on an object rather than a thread.
 
Vadim Vararu
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 23810
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!