java.lang/77: sleep(0) sleeps forever

mike@pillim.demon.co.uk mike@pillim.demon.co.uk
Sun Oct 24 03:16:00 GMT 1999


>Number:         77
>Category:       java.lang
>Synopsis:       sleep(0) sleeps forever
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    tromey
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Sun Oct 24 03:16:00 PDT 1999
>Closed-Date:
>Last-Modified:
>Originator:     Mike Moreton
>Release:        libgcj-2.95.1
>Organization:
>Environment:
Linux, posix threads
>Description:
If you run the progam attached in the "How-to-repeat"
section, the behavious is different under the Sun JDK and
libgcj.

Under Sun JDK 1.2.1, it's clear that sleep(0) means "sleep
for a very short time", while with libgcj the thread sleeps
until interrupted.
>How-To-Repeat:

public class TestSleep{
        public static void main(String[] args) {
                (new Doit()).start();
        }
}

class Doit extends Thread{
        public synchronized void run() {
                for (int i = 0; i < 10; i++) {
                        System.out.println("i is now "+i);
                        try {
                                if (i==5) {
                                        sleep(0);
                                } else {
                                        sleep(1000);
                                }
                        } catch (InterruptedException e) {}
                }
        }
}
>Fix:
In natThread.cc, in method java::lang::Thread::sleep (jlong millis, jint nanos)
add at the start of the method the following code

  if ((millis == 0) && (nanos == 0)) {
    return;
  }
>Release-Note:
>Audit-Trail:
>Unformatted:


More information about the Java-prs mailing list