• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Regarding a MasterKey question from the CD

 
Greenhorn
Posts: 21
Firefox Browser C++ Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 21
Firefox Browser C++ Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it. Thanks again.
 
If you send is by car it's a shipment, but if by ship it's cargo. This tiny ad told me:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic