JDK-4777391 : unexpected behaviour of Object.wait method
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 1.4.2
  • Priority: P5
  • Status: Closed
  • Resolution: Not an Issue
  • OS: solaris_8
  • CPU: sparc
  • Submitted: 2002-11-12
  • Updated: 2003-01-22
  • Resolved: 2003-01-22
Description

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.

======================================================================

Comments
EVALUATION The wait call is not being used properly in the example code. It should *always* be used inside a loop (see "Effective Java Programming Language Guide", Item 50. This provides robustness in the face of unexpected notifies, including "spurious wakeups" (The spec is curiously silent on the topic of spurious wakeups. It should explicitly acknowledge the possibility as per bug 4308396.) In this case, the program is probably *not* observing a spurious wakeup: it is perfectly acceptable for the system to call notify or notifyAll on a thread object in the normal course of its life cycle. ###@###.### 2002-11-15
15-11-2002