| Author |
notify() vs notifyAll() in this code
|
Andre Enimot
Ranch Hand
Joined: Jul 29, 2009
Posts: 31
|
|
I know the difference between those two was already discussed not once, but still don't really understand it in practice, for example here (K&B book, Chapter 9)
Regardless of whether I use notify() or notifyAll() in lines 26-27, all three Reader threads get notified and print their messages:
Thread[Thread-1,5,main]: Waiting for calculation...
Thread[Thread-3,5,main]: Waiting for calculation...
Thread[Thread-2,5,main]: Waiting for calculation...
Thread[Thread-2,5,main]: Total is: 4950
Thread[Thread-3,5,main]: Total is: 4950
Thread[Thread-1,5,main]: Total is: 4950
What is it that I am missing?
|
 |
Nitish Bangera
Ranch Hand
Joined: Jul 15, 2009
Posts: 536
|
|
Well the calculator thread will notify only one thread if notify() is used, which thread its unpredictable. But when the calculator thread completes, it calls its own notifyall() method. So if you have some thread which are joined(calculator.join()) to the end of calculator thread will be notified when the calculator thread completes. notifyAll() is called as soon as the run method ends. I suppose you can try this behaviour commenting out both notify and notifyall. Well as found out this is an unexpected behaviour of the thread. It can be avoided and to really check the notify and notifyall, implement Runnable as it is advised to that in most cases using threads. Make CalcReader and Calculator implementing the Runnable interface and start these runnables using new Thread(runnable).start() . May be you will see the expected behaviour then. Well there is problem here if the calculator runs first calling the notifyAll and then the other 3 threads, then again this program will no end. Well then you can use setPriority() and make use of MAX_PRIORITY, MIN_PRIORITY and NORM_PRIORITY over using numbers from 1 to 10 more like this
cheers
|
[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
|
 |
Ninad Kulkarni
Ranch Hand
Joined: Aug 31, 2007
Posts: 774
|
|
|
@ Andre I agree with Neetish and also you can see this
|
SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
|
 |
Andre Enimot
Ranch Hand
Joined: Jul 29, 2009
Posts: 31
|
|
|
Nitish, Ninad, thank you for explaining. Oooooo that's a big one! So notifyAll() is called automatically by any run() method after it completes! I don't think this is mentioned in the K&B book, is it.
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
Andre Enimot wrote:Nitish, Ninad, thank you for explaining. Oooooo that's a big one! So notifyAll() is called automatically by any run() method after it completes! I don't think this is mentioned in the K&B book, is it.
No it isn't, and you don't need to know that for the exam as it is an implementation detail of the API which can change (actually this has been implemented like this because of the join method, search the forum for more details)...
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
Nitish Bangera
Ranch Hand
Joined: Jul 15, 2009
Posts: 536
|
|
|
Yes you don't need to know that.. Just can get the concept that's all. Also its based on how join works that's all which is not needed to know it detail. Also not by any run but only the thread's run method and not runnable's run method.
|
 |
Deepak Bala
Bartender
Joined: Feb 24, 2006
Posts: 6588
|
|
It is unlikely that the exam will try to trick you with that knowledge. Since the behavior is not documented, you will not be asked about it
|
SCJP 6 articles - SCJP 5/6 mock exams - SCJP Mocks - SCJP 5 Mock exam (Word document ) - SCJP 5 Mock exam in Java.Inquisition format
|
 |
 |
|
|
subject: notify() vs notifyAll() in this code
|
|
|