This week's book giveaways are in the iOS and Features new in Java 8 forums. We're giving away four copies each of Barcodes with iOS: Bringing together the digital and physical worlds and Core Java for the Impatient and have the authors on-line! See this thread and this one for details.
This program assumes that waitFor(0) will be called before notifyTo(0), but there's nothing at all that guarantees this will be the case. As written, the two methods could be called in either order, because there's no guarantee which of the two threads will start running first.
If notifyTo() is called first, then the program will hang, as the waitFor() will never return.
OK, well, now that you mention it, there is another problem: each Thread has its own TestWaitNotify object, and each TestWaitNotify object has its own array of five Objects -- so that the Object that wait() is called on is never notified; it's another object altogether that is notified, but nothing is waiting.
To fix just use one single TestWaitNotify object to pass to both Thread constructors. [ August 03, 2004: Message edited by: Ernest Friedman-Hill ]
Joined: Nov 01, 2003
Great ! It works !
Thanks Ernest, It also cleared few things on the way...