Doubt regarding working of wait() and notifyAll() in Threads (Threads forum at JavaRanch)
Java FAQ
Recent Topics
Register / Login
Author
Mansukhdeep Thind Ranch Hand Joined: Jul 27, 2010 Posts: 1156
I like...
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 . 1 3 .
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 .
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
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...
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.
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 .
www.coderanch.com/t/506799/threads/java/working-wait-notifyAll-Threads
3/3