Anda di halaman 1dari 3

7/14/13

Doubt regarding working of wait() and notifyAll() in Threads (Threads forum at JavaRanch)

A friendly place for programming greenhorns!

Big Moose Saloon


Search

Java FAQ

Recent Topics

Register / Login

JavaRanch Java Forums Java Threads and Synchronization

Author

Doubt regarding working of wait() and notifyAll() in Threads


posted 8/16/2010 8:53:37 PM

Mansukhdeep Thind Ranch Hand Joined: Jul 27, 2010 Posts: 1156
I like...

Hi all Please have a look at the code below:

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 .

p a c k a g ec o m . m a n s u k h . t e s t ; 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 nt oc o m p l e t e . . ." ) ; 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 x ) { } 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 ) ; } }

1 4 . 1 5 . 1 6 . 1 7 . 1 8 . 1 9 . 2 0 . 2 1 . 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 ) { 2 2 . 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 ( ) ; www.coderanch.com/t/506799/threads/java/working-wait-notifyAll-Threads

1/3

7/14/13

Doubt regarding working of wait() and notifyAll() in Threads (Threads forum at JavaRanch)

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 .

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 ( ) ; } } }

According to my understanding, the execution of the code is as follows: 1> A separate stack is created for main() thread and it is loaded in to the JVM. 2> An instance of Calculator class is created. 3> This instance(calculator) is passed on to the overloaded constructor of the Reader class which initializes variable c and start() is called on that instance. 4> The overridden run() method runs. It acquires a lock on the newly created Thread instance (c) using synchronized(c) and prints "Waiting for calculation to complete..." 5> The currently executing thread (c) is placed on hold using c.wait(); 6> Similarly, two more times steps 3,4,5 are repeated and in Toto 3 new call stacks have been created and the 3 threads have been placed on them respectively. 6> Next, the calculator thread is started and the calculation is completed in the run() method of Calculator class. 7> Finally, after calculation has been completed, the calculator thread notifies the threads in runnable condition (all three threads created above) that not to wait anymore and execute the following: System.out.println("Total is : " + c.total); and we get the output:

Waiting for calculation to complete... Waiting for calculation to complete... Waiting for calculation to complete... Total is : 4950 Total is : 4950 Total is : 4950
www.coderanch.com/t/506799/threads/java/working-wait-notifyAll-Threads 2/3

7/14/13

Doubt regarding working of wait() and notifyAll() in Threads (Threads forum at JavaRanch)

Please correct me if I am wrong. Kindly suggest if I missed out on something or made a mistake as I have studied Threads for the first time. Regards Mansukhdeep Singh
~ Mansukh Ernest Friedman-Hill author and iconoclast Marshal Joined: Jul 08, 2003 Posts: 24093

posted 8/16/2010 8:57:51 PM

That is a good description of what happens in this program, yes. We have a forum for discussing Threads, and I am going to move this there.

16
I like...

[Jess in Action][AskingGoodQuestions]

Mansukhdeep Thind Ranch Hand Joined: Jul 27, 2010 Posts: 1156
I like...

posted 8/16/2010 10:25:31 PM


Ernest Friedman-Hill wrote:

That is a good description of what happens in this program, yes. We have a forum for discussing Threads, and I am going to move this there.

Thank you Ernest.

subject: Doubt regarding working of wait() and notifyAll() in Threads

Similar Threads Question on Thread example in Kathy Sierra 1.4 book Code using notifyAll() runs indefinitely Threads - wait() and notifyAll() 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:38:17 .

Contact Us | Powered by JForum |

C opyright 1998-2013 Paul W he aton

www.coderanch.com/t/506799/threads/java/working-wait-notifyAll-Threads

3/3

Anda mungkin juga menyukai