Vous êtes sur la page 1sur 4

7/14/13

notify() vs notifyAll() in this code (SCJP forum at JavaRanch)

A friendly place for programming greenhorns!

Big Moose Saloon


Search

Java FAQ

Recent Topics

Register / Login

JavaRanch Java Forums Certification Programmer Certification (SCJP/OCPJP)

Author

notify() vs notifyAll() in this code


posted 9/5/2009 11:01:51 AM

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)
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 .

c l a s sC a l c R e a d e re x t e n d sT h r e a d{ C a l c u l a t o rc ;/ /j u s tar e fv a r i a b l e p u b l i cC a l c R e a d e r ( 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 ( T h r e a d . c u r r e n t T h r e a d ( )+ " :W a i t i n gf o rc a l c u l a t i o n . . . " ) ; c . w a i t ( ) ; }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 h r e a d . c u r r e n t T h r e a d ( )+ " :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 ( ) ; n e wC a l c R e a d e r ( c a l c u l a t o r ) . s t a r t ( ) ; n e wC a l c R e a d e r ( c a l c u l a t o r ) . s t a r t ( ) ; n e wC a l c R e a d e r ( c a l c u l a t o r ) . s t a r t ( ) ; c a l c u l a t o r . s t a r t ( ) ; } } c l a s sC a l c u l a t o re x t e n d sT h r e a d{ 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 A l l ( ) ; n o t i f y ( ) ; } } }

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

notify() vs notifyAll() in this code (SCJP forum at JavaRanch)


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

posted 9/5/2009 12:28:47 PM

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

notify() vs notifyAll() in this code (SCJP forum at JavaRanch)

[ 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...

posted 9/5/2009 10:20:39 PM

@ 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

Andre Enimot Ranch Hand Joined: Jul 29, 2009 Posts: 31

posted 9/6/2009 1:32:26 PM

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

posted 9/6/2009 2:31:08 PM


A ndre 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.

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)...

SC JP 6 | SC WC D 5 | Javaranch SC JP FAQ | SC WC D Links

Nitish Bangera Ranch Hand Joined: Jul 15, 2009 Posts: 536

posted 9/7/2009 9:46:30 AM

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

posted 9/7/2009 10:34:47 AM

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

notify() vs notifyAll() in this code (SCJP forum at JavaRanch)

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 .

Contact Us | Powered by JForum |

C opyright 1998-2013 Paul W he aton

www.coderanch.com/t/461261/java-programmer-SCJP/certification/notify-notifyAll-code

4/4

Vous aimerez peut-être aussi