Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes wait/notifyAll excersice problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "wait/notifyAll excersice problem" Watch "wait/notifyAll excersice problem" New topic
Author

wait/notifyAll excersice problem

kaku kohli
Greenhorn

Joined: Oct 02, 2012
Posts: 6

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

Joined: Dec 25, 2013
Posts: 261
    
    4
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

Joined: Oct 02, 2012
Posts: 6

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
 
 
subject: wait/notifyAll excersice problem