wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes no effect with notifyAll() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "no effect with notifyAll()" Watch "no effect with notifyAll()" New topic
Author

no effect with notifyAll()

Harry Tuttle
Greenhorn

Joined: Apr 08, 2010
Posts: 24
I try to run the example Reader / Calculator on page 753 of the scjp book. here is the code for the Reader class


and the Calculator class

When I run these classes only the waiting prompt is shown but not the total. What's wrong?

I found some explanations here
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

Harry Tuttle wrote:I found some explanations here


That link provides good information, but in addition to that, for some reason you put the Total report inside your catch(InterruptedException) clause. notifyAll() does not interrupt wait(), it notifies it that it should stop normally, ergo the Total report should not get called (unless some other interruption occurred). You do not want the report in the catch clause, you want it either just after the wait() (at the end of the try clause) or just after the catch clause.


Steve
Harry Tuttle
Greenhorn

Joined: Apr 08, 2010
Posts: 24
Steve Luke wrote:
Harry Tuttle wrote:I found some explanations here


That link provides good information, but in addition to that, for some reason you put the Total report inside your catch(InterruptedException) clause. notifyAll() does not interrupt wait(), it notifies it that it should stop normally, ergo the Total report should not get called (unless some other interruption occurred). You do not want the report in the catch clause, you want it either just after the wait() (at the end of the try clause) or just after the catch clause.


Thank you. In this case I must say, you can learn from errors!
 
jQuery in Action, 2nd edition
 
subject: no effect with notifyAll()
 
Similar Threads
notifyAll() example
Code using notifyAll() runs indefinitely
blocking and waiting
Thread question in an example of Kathy and Bert Book
blocking and waiting