• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Code using notifyAll() runs indefinitely

 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I worked, it waits for the notification, and change it as bellow, it works as expected!
 
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 !!!


 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic