Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Code using notifyAll() runs indefinitely

 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Henry Wong
author
Marshal
Pie
Posts: 21003
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On my system the code is exiting after displaying Total is: 4950 3 times...

[Edit: Henry gave the correct explanation]
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I worked, it waits for the notification, and change it as bellow, it works as expected!
 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !!!


 
Harshit Sethi
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
remember guys, although this is interesting stuff, it's no longer on the exam!

 
Harshit Sethi
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are there any tricky questions related to "DeadLock" in SCJP syllaybus?
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic