Name: ipR10067 Date: 11/12/2002
The following example shows unexpected behaviour of Object.wait method:
public class hang extends Thread {
public static void main(String argv[]) {
Thread thread = new hang();
System.out.println("prepare to hang !!");
try {
synchronized (thread) {
thread.start(); /*1*/
thread.wait(); /*2*/
}
System.out.println("test should be hung !!!");
} catch (InterruptedException e) {
System.out.println("InterruptedException" + e);
}
}
}
logs:
>java -version
java version "1.4.2-beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2-beta-b06)
Java HotSpot(TM) Client VM (build 1.4.2-beta-b06, mixed mode)
>java -cp . hang
prepare to hang !!
test should be hung !!!
-------------------------------------------------------------
According to description of the Object.wait method
"Causes current thread to wait until another thread invokes
the notify() method or the notifyAll() method for this object."
the test should hang up at the point /*2*/ because there are no invocation of
the notify() method nor the notifyAll() method for the object "thread".
It looks like that some hidden thread invokes the notify() or
notifyAll() methods for the object "thread".
If the line /*1*/ is commented out then the test hangs as expected.
======================================================================