| Author |
Regarding a MasterKey question from the CD
|
Nikhil Pujari
Greenhorn
Joined: Oct 31, 2011
Posts: 21
|
|
This is the question I am talking about:
This is the explanation offered in the answers section:
"The first call to wait() blocks forever. It's waiting for a notify(), but the notify() is never called because it's blocked on the wait(). In order for a notify() to be useful, it must be sent from a separate thread so that it can be called while the first thread is still waiting at the wait() statement."
Why and how do the two methods block on each other?
AFAIK, this is how it works: upon encountering the wait() method, the currently running thread releases its lock, which is reacquired. Then we encounter the notify() method (why is it waiting for the wait() method)? This is confusing...
Thanks
|
Einstein: A problem cannot be solved in the same state of mind in which it was created.
More motivation: http://www.youtube.com/watch?v=_7xfyDjCFqs
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 5895
|
|
The whole point of wait() is "stop this thread until somebody calls notify() on it (or notifyAll())." A thread can't notify() itself, because it's stopped--it won't execute any other code until after notify() has been called.
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 5895
|
|
Nikhil Pujari wrote:
AFAIK, this is how it works: upon encountering the wait() method, the currently running thread releases its lock,
Yes.
which is reacquired.
Nope. That can't happen until the thread starts executing again, and the thread can't execute until notify() has been called.\
|
 |
Nikhil Pujari
Greenhorn
Joined: Oct 31, 2011
Posts: 21
|
|
|
Got it. Thanks again.
|
 |
 |
|
|
subject: Regarding a MasterKey question from the CD
|
|
|