Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confusion on Threads

 
Valentino Bautista
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic