File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes notify() vs notifyAll() in this code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "notify() vs notifyAll() in this code" Watch "notify() vs notifyAll() in this code" New topic
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: 537

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: 784

@ 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
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

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: 537

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: 6657
    
    5

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 - More SCJP Mocks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: notify() vs notifyAll() in this code
 
Similar Threads
Doubt with notify/ notifyAll method
Code using notifyAll() runs indefinitely
notify() and notifyAll()
Threads notify() and notifyAll()
Notify and notifyAll