Professional Documents
Culture Documents
Java FAQ
Recent Topics
Register / Login
Author
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)
view plain c opy to c lipboard print ?
N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped
0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 . 1 1 . 1 2 . 1 3 . 1 4 . 1 5 . 1 6 . 1 7 . 1 8 . 1 9 . 2 0 . 2 1 . 2 2 . 2 3 . 2 4 . 2 5 . 2 6 . 2 7 . 2 8 . 2 9 . 3 0 .
Regardless of whether I use notify() or notifyAll() in lines 26-27, all three Reader threads get notified and print their messages:
www.coderanch.com/t/461261/java-programmer-SCJP/certification/notify-notifyAll-code
1/4
7/14/13
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
view plain c opy to c lipboard print ?
N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped
0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 . 1 1 . ; 1 2 . 1 3 . 1 4 . 1 5 . 1 6 . 1 7 . 1 8 . 1 9 . 2 0 . 2 1 . 2 2 . 2 3 . 2 4 . 2 5 . 2 6 . 2 7 . 2 8 . 2 9 . 3 0 . 3 1 . 3 2 . 3 3 . 3 4 . 3 5 . 3 6 . 3 7 . 3 8 . 3 9 . 4 0 . 4 1 . 4 2 . 4 3 .
p u b l i cc l a s sC a l c R e a d e r 1i m p l e m e n t sR u n n a b l e{ C a l c u l a t o rc ; p u b l i cC a l c R e a d e r 1 ( C a l c u l a t o rc a l c ){ c=c a l c ; } p u b l i cv o i dr u n ( ){ s y n c h r o n i z e d( c ){ t r y{ S y s t e m . o u t . p r i n t l n ( " W a i t i n gf o rc a l c u l a t i o n . . . " ) ; c . w a i t ( ) ; S y s t e m . o u t . p r i n t l n ( T h r e a d . c u r r e n t T h r e a d ( ) . g e t N a m e ( )+"n o t i f i e d " ) }c a t c h( I n t e r r u p t e d E x c e p t i o ne ){ } S y s t e m . o u t . p r i n t l n ( " T o t a li s :"+c . t o t a l ) ; } } p u b l i cs t a t i cv o i dm a i n ( S t r i n g [ ]a r g s ){ C a l c u l a t o rc a l c u l a t o r=n e wC a l c u l a t o r ( ) ; T h r e a dt 1=n e wT h r e a d ( n e wC a l c R e a d e r 1 ( c a l c u l a t o r ) ) ; T h r e a dt 2=n e wT h r e a d ( n e wC a l c R e a d e r 1 ( c a l c u l a t o r ) ) ; T h r e a dt 3=n e wT h r e a d ( n e wC a l c R e a d e r 1 ( c a l c u l a t o r ) ) ; T h r e a dc a=n e wT h r e a d ( c a l c u l a t o r ) ; t 1 . s e t P r i o r i t y ( T h r e a d . M A X _ P R I O R I T Y ) ; t 2 . s e t P r i o r i t y ( T h r e a d . M A X _ P R I O R I T Y ) ; t 3 . s e t P r i o r i t y ( T h r e a d . M A X _ P R I O R I T Y ) ; c a . s e t P r i o r i t y ( T h r e a d . M I N _ P R I O R I T Y ) ; t 1 . s t a r t ( ) ; t 2 . s t a r t ( ) ; t 3 . s t a r t ( ) ; c a . s t a r t ( ) ; } } c l a s sC a l c u l a t o ri m p l e m e n t sR u n n a b l e{ i n tt o t a l ; p u b l i cv o i dr u n ( ){ s y n c h r o n i z e d( t h i s ){ f o r( i n ti=0 ;i<1 0 0 ;i + + ){ t o t a l+ =i ; } / / n o t i f y ( ) ; n o t i f y A l l ( ) ; } } }
cheers www.coderanch.com/t/461261/java-programmer-SCJP/certification/notify-notifyAll-code
2/4
7/14/13
cheers
[ SC JP 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: 780
I like...
@ Andre I agree with Neetish and also you can see this
SC JP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SC JP FAQ - SC JP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
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: 9216
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.
2
I like...
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)...
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.
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
1
I like...
SC JP 6 articles - SC JP 5/6 mock exams - SC JP Mocks - SC JP 5 Mock exam (Word document ) - SC JP 5 Mock exam in Java.Inquisition format
Granny's Programming Pearls "inside of every large program is a small program struggling to get out" JavaRanch.com/granny.jsp
www.coderanch.com/t/461261/java-programmer-SCJP/certification/notify-notifyAll-code
3/4
7/14/13
subject: notify() vs notifyAll() in this code
Similar Threads Notify and notifyAll Threads notify() and notifyAll() notify() and notifyAll() Code using notifyAll() runs indefinitely Doubt with notify/ notifyAll method
All times above are in your local time zone & format.T he current ranch time (not your local time) is Jul 14, 2013 08:38:49 .
www.coderanch.com/t/461261/java-programmer-SCJP/certification/notify-notifyAll-code
4/4