• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

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

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
muchas muchas gracias
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
this is supposed to be a surprise, but it smells like a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic