• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

wait() question

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to be sure I got it right, is this correct ?

The flow of execution continues after a wait(long timeout) when:

(the lock has been released) && (the timeout has passed || notify() has been called after wait() was called || the thread has died after wait() was called)
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check it with API here
 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remember that wait()/notify() must be called on the same object. Also,
the thread does not continue execution, but rather becomes runnable.
The scheduler must decide when execution is to continue. And your last
comment, "...thread has died after wait() was called." I don't understand
this statement.

Jim ... ...
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this thread is too difficult for "beginning". Moving.
 
Andrea Chiavazza
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Jim

I understand the points you make, and my question used some wrong wordings.
However I defend my last phrase and I will try to explain it:
If the object being waited on is not a Thread, wait() will wait forever unless a notify() is called.
However if the object being waited on is a Thread, wait() will return, besides if notify() is called, also if the Thread object is already dead at the time when wait() is called.
If my explanation is not clear, how do you explain the following code ? Why the line t.start() causes the wait() not to wait ?

 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Andrea : Let's talk about threads a bit. I see that you create a Thread object but do not override
its run() method. When you t.start() the Thread, the scheduler will call its default run() method
which does nothing. So the thread just dies. If you comment the t.start() method the thread does
not run at all. Remember that a dead thread does not mean that the Thread object disappears.
It still can be accessed with getName() or isAlive(), for example.

Jim ... ...
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happen to a waiting thread if notify() is not called on the locked object anytime ( due to some deadlock ) . is there any mechanism to release the lock forcefully ?

-Shyam Ramath
 
Ranch Hand
Posts: 443
3
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wait is also allowed to wake up spuriously eg coz the OS says so ;-)
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic