wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Sierra's scjp book. Chapter#9 notifyAll() example Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Sierra Watch "Sierra New topic
Author

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

Shahfazal Mohammed
Ranch Hand

Joined: Jan 08, 2004
Posts: 41
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


-peace.
Bijesh Krishnadas
Ranch Hand

Joined: Aug 08, 2002
Posts: 31
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

Joined: Jan 08, 2004
Posts: 41
muchas muchas gracias
Bob Chandler
Ranch Hand

Joined: Jan 13, 2004
Posts: 33
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

Joined: Sep 15, 2003
Posts: 94
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

Joined: Jan 13, 2004
Posts: 33
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

Joined: Feb 18, 2004
Posts: 96
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


Rich
SCJP 1.4
JuanCa SP
Greenhorn

Joined: Feb 05, 2004
Posts: 10
Hi Shahfazal,
Just insert between the line 33 and 34 the next sentence:

Got it?
Bob Chandler
Ranch Hand

Joined: Jan 13, 2004
Posts: 33
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Sierra's scjp book. Chapter#9 notifyAll() example
 
Similar Threads
doubt from scjp book on threads
Question on Thread example in Kathy Sierra 1.4 book
Code using notifyAll() runs indefinitely
Thread question in an example of Kathy and Bert Book
notify() vs notifyAll() in this code