Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

wait() question  RSS feed

 
Andrea Chiavazza
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • 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)
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check it with API here
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • 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 ... ...
 
Campbell Ritchie
Sheriff
Posts: 55341
157
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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 ... ...
 
Shyam Ramath
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • 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
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wait is also allowed to wake up spuriously eg coz the OS says so ;-)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!