• 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

Confusion on Threads

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, the code on K&B book page 724-725 about wait() and notify() is not behaving as expected .



I just wrote the code on my IDE, and all the threads print total even when i commented the line with notifyAll(), please someone could explain this ?

Note: I'm running it on JRE 6 (dont know if it matters)

Thanks in advance
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have discovered an odd effect which is not going to be on the test. It turns out that in Java's Thread implementation, whenever a Thread finishes running, it calls its own notifyAll() method. This allows any other threads that are calling join() to receive a notification and resume running. Because Reader and Calculator both extend Thread directly, they have this behavior, which means they call notifyAll() as soon as their run method ends. Since the notifyAll() shown in the code runs just before run() ends, notifyAll() basically gets called twice in a row. When you comment out one of those calls, the second remains.

Here's an alternate version implementing Runnable rather than extending Thread. This way Calculator and Reader don't inherit the unexpected behavior of Thread. If you comment out the notifyAll(), you will see that no reader ever returns from the wait().

I also added a Thread.sleep() inside Calculator's run() method, to avoid the problem discussed in the next few pages ("Using wait() in a loop"). Without this, you may not always see all three Readers finish.
 
Valentino Bautista
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you Jim,

One more question, where can i find information about this strange behavior on java's Thread Implementation ?
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure - this is undocumented trivia, really. You can look at the source code for the Thread class and see that, for example, the join() method contains a wait(), which means that something must send a notify() or notifyAll() when the thread is done. That's how I learned of this. More generally, JLS 17.8.1 says that a thread can have a "spurious wake-up" - meaning it may return from wait() for no apparent reason. This is another reason why you always need to put wiat() methods inside loops, to check if the thing you are waiting for has really happened or not.

Again, this is not going to be on the exam. Except the part about putting wait inside a loop. You don't have to fully understand all the reason why it goes in a loop, but you do need to use a loop. The details of that are covered in the next section of the book.
reply
    Bookmark Topic Watch Topic
  • New Topic