• 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/notifyAll excersice problem

 
Greenhorn
Posts: 6
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
Here is a wait/notifyAll code I wrote getting idea from SCJP Kathy Sierra book.




I used while loop with boolean for ensuring "if notifyAll() executes before wait(), threads doesn't keep on waiting."

Lets assume 3rd Reader thread executes before calculator thread and enters wait on calc.
then calculator executes.
then why Reader thread not keep waiting due to wait() ?

Reply asap if possible. thanks
 
Ranch Hand
Posts: 262
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kaku kohli wrote:


Lets assume 3rd Reader thread executes before calculator thread and enters wait on calc.
then calculator executes.
then why Reader thread not keep waiting due to wait() ?



If the assumption is that the 3rd Reader starts to execute before the calculator thread and it enters wait on calculator and then the calculator thread executes and completes and sends the notifications, then why should the Reader thread keep waiting? The scope of the synchronization block is only till the notifications are sent. Hence after notifying, the synchronization lock would be released.
The reader thread would have received the notification after calculator thread completed and returned from the wait state after acquiring the synchronization lock again.

Why do you think it should keep waiting?
 
kaku kohli
Greenhorn
Posts: 6
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah its correct.
If reader thread enters wait on calculator first and then calculator executes, it notifies reader to stop waiting.
got it now. thanks
i guess i needed to think a little more
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic