Professional Documents
Culture Documents
File APIs for Java Developers Manipulate DOC, XLS, PPT, PDF and many others from your application. http://aspose.com/file-tools
Java FAQ
Recent Topics
Register / Login
Author
notifyAll() example
posted 1/19/2011 8:38:51 AM
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
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
7/14/13
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 .
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
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.)
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
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.
Sunny Jain Ranch Hand Joined: Jul 23, 2007 Posts: 433
I like...
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
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
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 .
www.coderanch.com/t/524056/java/java/notifyAll
4/4