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...