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!
Kathy