Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sierra's scjp book. Chapter#9 notifyAll() example

 
Shahfazal Mohammed
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello everyone-
This might have been asked some place else too. but could not find it. my question is in Kathy's SCJP book, in chapter 9, while talkng about threads, it's said that the notifyAll() method notifies all thread waiting on an object's lock. and i ran the Reader/Calculator example given in the book.
the Reader class -

and the Calculator class

when i run the program... i get three msgs that say "Waiting for calculation.." and nothing else .. meaning the Reader threads are still waiting on the Calculator thread, which does a notifyAll() .. am i missing somthing here?? any help would be greatly appreciated
 
Bijesh Krishnadas
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope I am right here. If u notice the "calculator" thread is started first, acquires a lock on the object, excecutes and does a notifyAll. The Reader threads start after this, print the message and do a wait(). Therefore the calculator issues a notifyAll(), even before any of the Reader threads have issued a wait(), and hence has no effect.
Hope it is right and clear to you.

Originally posted by scjp-to-be:
hello everyone-

and the Calculator class

 
Shahfazal Mohammed
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
muchas muchas gracias
 
Bob Chandler
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys...
had a look at Kathy & Bert to find more explanation
and noticed that their code starts the calculator thread
AFTER the Reader threads, which makes more sense.
The Reader threads are lined up waiting when calculator
starts, executes and calls notifyAll - thus notifying
them all that it's finished.
In your code, you have calculator starting before the
Reader threads which defeats the object of the example.
 
Yan Lee
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In short, rewriting the code in the main as follows makes it function as intented:
Calculator calculator = new Calculator();
new Reader(calculator).start();
new Reader(calculator).start();
new Reader(calculator).start();
calculator.start();
This is a mistake in the book and should be corrected.
 
Bob Chandler
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmmm....
yes, I see what you mean...
so are the Reader threads able to get to their wait code
before the calculator notifies them all?
Is is right that each of the threads calls start without
waiting for the last thread to complete it's run?
If there are no threads waiting when notifyAll is reached,
what happens then?
Confused Bob
Which one is the correct version? the book says:
public static void main (String [] args) {
Calculator calculator = new Calculator();
new Reader(calculator).start();
new Reader(calculator).start();
new Reader(calculator).start();
calculator.start();
}
[ February 25, 2004: Message edited by: Bob Chandler ]
[ February 25, 2004: Message edited by: Bob Chandler ]
 
Richard Quist
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It makes perfect sense (to me...for whatever that's worth) that you would want to start other threads before earlier ones completed (otherwise you really wouldn't be multi-threaded.....)
And if no threads are waiting when notifyAll is called then nothing happens...there's nothing to notify....calling notify/notifyAll just tells the scheduler to let one/all waiting threads become runnable.
Rich
 
JuanCa SP
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shahfazal,
Just insert between the line 33 and 34 the next sentence:

Got it?
 
Bob Chandler
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rich,
a very eloquent explanation....
you're right! what is the point of having a multithreaded
program if they have to queue up and behave like
regular code....
whatever was I thinking?!
Cheers mate
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic