Anybody ever write a 711 page program? We'd like everyone to know that we've just posted Version 2 of our errata page on www.wickedlysmart.com. And... In the next few days, we'll be announcing a new Java contest (did we say this a few days ago?) !!! More prizes, more fun ! Stay tuned !
Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
20. calculator.start(); 21. new Reader(calculator).start(); 22. new Reader(calculator).start(); 23. new Reader(calculator).start();
20. new Reader(calculator).start(); 21. new Reader(calculator).start(); 22. new Reader(calculator).start(); 23. calculator.start();
Thank you very much for the errata, Bert! Here I have a question: The corrected code above worked find and I understand it. But why didn't the original code work well? I complied the both and the first one stuck after printed 3 lines of "waiting for calculation...". Why? Thank you.
Thus, each Reader checks to see if the calculation has already been completed, immediately before executing a wait. And even if it receives a notifyAll(), it rechecks to see if the calculation really is done. The recheck may be unnecessary if you are certain that the only code that could possibly call notify/notifyAll is the code that is run when the calculator is done. However it's good to get in the habit of putting this check in a loop, as on more complex systems there may be other reasons notify is called, and it's hard to keep track of everything. See Joshua Bloch's Effective Java, "Item 50: Never invoke wait() outside a loop", for more info. This book is highly recommended for the other 56 items as well. Of course, if the book example was not about a wait/notify protocol, you can just ignore this post. [ February 15, 2003: Message edited by: Jim Yingst ]
"I'm not back." - Bill Harding, Twister
Joined: Sep 17, 2002
Jim, Well spotted, exactly like what you said. Now the picture is clear to me. Thank you very much for your respond! Ignore? Oh no...there's usually bonus for me to read your posts answering questions submitted by others which had nothing to do with my own work, there's not only simple fact but more...no way ignore To Jasper, No, several exams are dominating me concurrently now, I'm just like a CPU doing Round-Robin time slicing...have done with Chapter 9 and 1 of Kathy and Bert's book, planned to take this exam in late March. Regards, Ellen
Joined: Jan 10, 2003
Originally posted by Ellen Zhao: several exams are dominating me concurrently now, I'm just like a CPU doing Round-Robin time slicing...
well, that certainly implies that you are in touch with more than just your own personal inner-JVM ... in the sense that the issue of whether a system performs time-slicing is jvm-independent. good luck with the tough german exams!