You are on page 1of 4

7/14/13

notifyAll() example (Java in General forum at JavaRanch)

File APIs for Java Developers Manipulate DOC, XLS, PPT, PDF and many others from your application. http://aspose.com/file-tools

A friendly place for programming greenhorns!

Big Moose Saloon


Search

Java FAQ

Recent Topics

Register / Login

JavaRanch Java Forums Java Java in General

Author

notifyAll() example
posted 1/19/2011 8:38:51 AM

Swerrgy Smith Ranch Hand Joined: Mar 26, 2010 Posts: 52

Dear all, In the book of K&B, there is an example about how to use notifyAll() for thread communication. However, in the class Calculator below, if I remove the notifyAll(), the program stills works well. Can anybody tell me why?

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 .
www.coderanch.com/t/524056/java/java/notifyAll

p u b l i cc l a s sR 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 ; p u b l i cR 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 ( " W a i t i n gf o rc a l c u l a t i o n . . . " ) ; c . w a i t ( ) ;


1/4

7/14/13

notifyAll() example (Java in General forum at JavaRanch)

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 .

}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 ( ) ; n e wR e a d e r ( c a l c u l a t o r ) . s t a r t ( ) ; n e wR e a d e r ( c a l c u l a t o r ) . s t a r t ( ) ; n e wR 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 ( ) ; } } }

And here is the result, it's the same with or without notifyAll().
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 .

W a i t i n gf o rc a l c u l a t i o n . . . W a i t i n gf o rc a l c u l a t i o n . . . W a i t i n gf o rc a l c u l a t i o n . . . T o t a li s :4 9 5 0 T o t a li s :4 9 5 0 T o t a li s :4 9 5 0

Bert Bates author Sheriff Joined: Oct 14, 2002 Posts: 8742

posted 1/19/2011 9:39:25 AM

This topic is no longer on the SCJP 6 exam. It might still be on the SCJP 5 exam, but really, it's hard to imagine a good reason to take the SCJP 5 exam at this point, so let's move it and the discussion can be continued in Java in General.

Eliminate fossil fuel subsidies. (If you're not on the edge, you're taking up too much room.)

Darren Littlepage Ranch Hand

posted 1/20/2011 1:14:54 AM

Specifically, what part of this is not on the exam? Threads, synchronized, wait()
2/4

www.coderanch.com/t/524056/java/java/notifyAll

7/14/13

notifyAll() example (Java in General forum at JavaRanch)

Joined: Dec 27, 2010 Posts: 35

and notifyAll()? I understood serialization was not longer on the exam and I am familiar with the updated objectives. Is there anything new on the exam that is not covered in K&B SCJP 6? Sorry if this is out of scope of the topic. I have been studying threads a lot for the exam...regardless I know it will be helpful in the real world but my focus is to pass the exam.

OC PJP for Java 6 on 01/2011

Sunny Jain Ranch Hand Joined: Jul 23, 2007 Posts: 433
I like...

posted 1/20/2011 8:37:34 AM

This is actually a very simple example. 3 threads are running all on same instance, which means - synchronized(this) means same for all threads. Lets say we have T-1, T-2,T-3 as threads and C-1 is calculator instance. Let assume that > T-2 got the lock over C-1, this leads to T-1 and T-3 both on waiting stage. > T-2 finishes the calculation and released the lock over C-1. Now you can do either of two things1) Notify all the other threads waiting for C-1, by calling C-1.notifyAll( ) or this.notifyAll( ). order of T-1 and T-3 will be random. could be either T-1 then T-3 or vice versa. 2) Leave it as it is, in this case JVM will schedule the running of threads by its own. order of T-1 and T-3 will be random. could be either T-1 then T-3 or vice versa.

Thanks,

Thanks and Regards, SC JP 1.5 (90%), SC WC D 1.5 (85%), The Jovial Java, java.util.concurrent tutorial

Granny's Programming Pearls "inside of every large program is a small program struggling to get out" JavaRanch.com/granny.jsp

subject: notifyAll() example

Similar Threads doubt in thread: notifyAll() Question on Thread example in Kathy Sierra 1.4 book
www.coderanch.com/t/524056/java/java/notifyAll 3/4

7/14/13

notifyAll() example (Java in General forum at JavaRanch)

Code using notifyAll() runs indefinitely notify() vs notifyAll() in this code 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:36:21 .

Contact Us | Powered by JForum |

C opyright 1998-2013 Paul W he aton

www.coderanch.com/t/524056/java/java/notifyAll

4/4

You might also like