The problem arises because your
t1.startMe() method is being invoked before your t1 thread runs. Consequently, the call to
notifyAll() is invoked
before the call to
wait(). If this happens (i.e.
notify is called before
wait), the thread could wait forever unless another
notify call is made on the same object.
It is good practice therefore to put in a
test condition before calling
wait to avoid the problem. I have ammended your code to provide an example of such a test condition.
Alternatively, if you want your program to run as is AND output "Notified", you could invoke the Thread.sleep() method on your main thread (i.e. the one that calls
t1.startMe()) with a time parameter, directly after the call to
t1.start(). This would give thread t1 a chance to run and call
wait() before the call to
notifyAll().
Hope this helps.
[ April 23, 2003: Message edited by: Rory French ]
[ April 23, 2003: Message edited by: Rory French ]
[ April 23, 2003: Message edited by: Rory French ]