aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Code using notifyAll() runs indefinitely Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Code using notifyAll() runs indefinitely" Watch "Code using notifyAll() runs indefinitely" New topic
Author

Code using notifyAll() runs indefinitely

Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
Hello,

Sierra/Bates, Chapter 9 code compiles, but runs indefinitely. Below is the code and output.




Output:

Waiting for calculation...
Waiting for calculation...
Waiting for calculation...
Total is: 4950
Total is: 4950


Marriage Made in Heaven
http://www.youtube.com/user/RohitWaliaWedsSonia
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19073
    
  40


Basically, what is happening is... In the beginning, you have four threads trying to grab the same lock. And the system granted the lock to the thread that does the notifyAll() third. So, when the last thread goes to a wait state, it doesn't get it, because it wasn't waiting when the notification was sent.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

On my system the code is exiting after displaying Total is: 4950 3 times...

[Edit: Henry gave the correct explanation]


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

When I worked, it waits for the notification, and change it as bellow, it works as expected!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
If notification is done on an Object before the other threads started waiting on it, those threads would keep on waiting as there is no subsequent notification....

But JVM sometiimes notifies , so i think the waiting threads would eventually becomes runnable and then running. Although this fact is written in the book, i have never seen JVM notifying waiting threads. Rest God knows !!!



SCJP 6.0 96%

(Connecting the Dots ....)
Harshit Sethi
Ranch Hand

Joined: Jul 16, 2010
Posts: 75
According to me this is how the program execution is taking place:

First the two threads are started and then see a wait call thus started waiting for notificationin the wait set and then the third thread executes upto this statement:

System.out.println("Waiting for calculation...");


and then the main thread begins executing which then calls the calculator that notifies all the thread that are waiting.Because only the two threads are in wait set they get notified and print total, now the main thread terminates and the third waiting thread encounters the wait call and since it does not get any notification now,the program runs Indefinitely.


I tried my best to explain.I hope this helps.
Harshit Sethi
Ranch Hand

Joined: Jul 16, 2010
Posts: 75
To avoid this kind of situation you should use the wait call within loop, turn few more pages in kathy Sierra and you will get how that should be done.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
remember guys, although this is interesting stuff, it's no longer on the exam!


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Harshit Sethi
Ranch Hand

Joined: Jul 16, 2010
Posts: 75
Are there any tricky questions related to "DeadLock" in SCJP syllaybus?
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
Ankit Garg wrote:On my system the code is exiting after displaying Total is: 4950 3 times...

[Edit: Henry gave the correct explanation]



Originally I tried the same code on Mac OS. Then I tried it on Ubuntu and it executes to completion, i.e. Total is: 4950 3 times

Still looking into this thread (pun unintended ) even though it won't be on exam.
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
Sahil Kapoor wrote:If notification is done on an Object before the other threads started waiting on it, those threads would keep on waiting as there is no subsequent notification....

But JVM sometiimes notifies , so i think the waiting threads would eventually becomes runnable and then running. Although this fact is written in the book, i have never seen JVM notifying waiting threads. Rest God knows !!!





That was my same reaction when i read on in Sierra/Bates
Unmesh Chowdhury
Ranch Hand

Joined: Jun 20, 2010
Posts: 45
The output of the program is JVM depended, that is, in which sequence the JVM starts the threads will dominate the output of the program.


M.Sc. in CS, OCPJP6 93%
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Code using notifyAll() runs indefinitely