Announcement
iPhone DZone: Fresh Links On Your iPhone by matt at Sat Jul 05 14:09:51 EDT 2008
Reading DZone on your iPhone just got a little bit easier. Visit http://dzone.com/iphone from your iPhone to try out our beta iPhone support. You can view stories, filter by tags, and login. We'd appreciate your feedback at feedback@dzone.com.
Voters make better lovers! Login and vote now.
By jj83777
via tutorials.jenkov.com
Published: May 07 2008 / 14:29
This text is no. 10 in a series on Java concurrency. This text describes how threads can send signals to each other, and wait for each other's signals, using wait(), notify(), and notifyAll(). The text also describes the problems of busy wait, missed signals and spurious wakups, and prescribes solutions to these problems.
Comments
gk75330 replied ago:
This is not a "fresh link for developers".
Jakob Jenkov replied ago:
Well, the topic isn't new, but the concrete text is, and 1-2 months down the line this text is still valid, contrary to many other links posted here.
tgautier replied ago:
Voted down - the first example is incorrect it doesn't have proper synchronization! The next example covers wait/notify. It takes the author 6 full paragraphs (who reads?) to get to the punchline without ever letting on there ever was one. Let me summarize it for you:
ALWAYS WAIT NOTIFY IN A WHILE LOOP
The author barely manages to point this out yet it is vitally important. You cannot cover concurrency (specifically wait/notify) and fail to cover that point.
Jakob Jenkov replied ago:
@ tgautier: Your statement is wonderfully short, but fails to explain *why* it is important to call wait() from inside a loop. First of all, a while loop doesn't even make sense unless you are waiting for a signal to be come "true".
To explain why you are storing a signal and not just running right after waking up from wait(), first you need to learn the issues of Missed Signals. The text does that.
Then you need to know about Spurious Wakeups, as these are the primary reason we call wait() inside a while loop.
Finally the text explains that using a while loop is important when multiple threads are awaiting the same signal, but only one should respond to the signal at a time.
The text is not meant as a summary for developers who already know thread signaling, but as a short tutorial for developers who don't know it. Therefore it is important to explain what we do, and why. The reason the text doesn't give you the impression that *always call wait() inside a while loop* is the punchline, is because that *isn't* the punchline. There are already too many developers having learned to "always do this, always do that" without understanding why. I don't want to create more of them, by posting such a short statement as you did.
Now, the punchlines are busy wait, wait(), notify(), notifyAll(), missed signals and how to avoid them, spurious wakeups and how to deal with them, and multiple threads waiting for the same signal. The text covers all of those. And, if after reading these explanations you still don't understand why it is important to call wait() inside a while loop, then perhaps the text has failed. Otherwise it serves exactly the purpose it intended.
Jakob Jenkov replied ago:
@tgautier: About the first example being incorrect... you are right. The "synchronized" keyword seems to have slipped from the setHasDataToProcess(...) method declaration. I'll fix that right away. Thanks for pointing it out!
... however, setting a boolean value is actually an atomic operation thread-wise in Java, so the code there might not even have caused problems... until perhaps it runs on a multi CPU machine, in which the missing synchronization could cause the CPU cache not to be flushed to main memory for other threads to read.
Voters For This Link (15)
Voters Against This Link (4)