• 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
  • Himai Minh
  • Carey Brown
  • salvin francis

Question on Thread example in Kathy Sierra 1.4 book

Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello --
When running the Calculator Thread example from the Kathy Sierra 1.4 book (see below, p. 533), it hangs with the following output when the calculator thread is started first [1].

I believe the reason is that the calculator thread starts first and grabs its object lock first. This allows the notifyAll() call to occur *before* any of the other threads are waiting. Therefore, notifyAll() does nothing.
If I modify the code to start the calculator thread *after* the Reader threads, the code appears to work correctly and generates the following output:

Q. Is the example really incorrect?

Cowgirl and Author
Posts: 1589
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Um, can we call it an embedded "learning opportunity"? Just think, if it had actually been a *correct* example, it would have robbed you of the valuable chance to discover the problem and fix it, thus deepening and enhancing your learning.
Not buying it?
First, yes you ARE definitely right that the example is incorrect.
And actually it's just a really bad example all the way around, even without the obvious error. We shouldn't have kept that example; I don't think it's a good idea to try to simplify a wait/notifyAll like that, but we never got around to rewriting it to be more robust with waiting in a loop, etc.
Your fix does make it work (changing the order of the threads), but even *that* is not necessarily a 100% solution, since we might not even be guaranteed that the last thread started (calc thread) will NOT be the first one to run...
There is still a possibility (although VERY remote) that the calc thread could still run first. Start order does not necessarily guarantee run order.
So we'll have to revise that example completely for next time. In the meantime, thanks for pointing this out and for bothering to try it, and make the changes!
Since you made it all the way through the last chapter of SCJP, does this mean you're planning to take the exam soon? Let us know!
cheers and thanks and good catch!
Mark Jackson
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much for the learning opportunity
Yes, I made it through the last chapter of the SCJP material, but I'm actually working *backwards* through your book (chapter by chapter). I had started with the Heller / Roberts book.
After reading on javaranch that the thread questions are some of the more difficult exam questions, I jumped right to chapter 9 in your book.
I plan to schedule my exam once I see how I'm doing on the mock exams (current results are confidential ).
Thanks for your well-written study guide!
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I'm actually working *backwards* through your book

Now that's a quote for the next edition!
I promise I will be the best, most loyal friend ever! All for this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic