• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

wait/notifyAll excersice problem

 
kaku kohli
Greenhorn
Posts: 6
Firefox Browser Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Heena Agarwal
Ranch Hand
Posts: 262
4
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic