JDK-4143354 : java.lang.Thread.sleep(millis) sleeps less than specified.
  • Type: Bug
  • Component: hotspot
  • Sub-Component: runtime
  • Affected Version: 1.2.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_nt
  • CPU: x86
  • Submitted: 1998-05-28
  • Updated: 1998-06-08
  • Resolved: 1998-06-08
Related Reports
Duplicate :  
Relates :  
Description

Name: akC45999			Date: 05/28/98




On win32 version of jdk1.2beta4G,
the sleep(millis) method can sleep less time than the parameter specified.

Such behaviour contradicts the specification.
 
------------------------------------- file sleep0101.java
//File: @(#)sleep0101.java 1.2 97/12/08 
//Copyright 12/08/97 Sun Microsystems, Inc.  All Rights Reserved

//package javasoft.sqe.tests.api.java.lang.Thread.sleep0101;

import java.io.PrintStream;
//import javasoft.sqe.harness.Status;
//import javasoft.sqe.harness.Test;

class Status {
  public static  int failed(String msg) {
	System.out.println("Failed. "+msg);
	return 97;
  }

  public static int passed(String msg) {
	System.out.println("Passed. "+msg);
	return 97;
  }
}
public class sleep0101 {// implements Test {

  public int run(String argv[], PrintStream log, PrintStream out) {
        Thread me=Thread.currentThread();
        int[] data={3162,1000,316,100,32,10,3,1,0};
        for (int k=0; k<data.length; k++) {
                int delay=data[k];
                long time=System.currentTimeMillis();
                try {
                        me.sleep(delay);
                } catch (Throwable e) {
                        return Status.failed("Unexpected exception in sleep("+delay+"): "+e.toString());
                }
                long realDelay=System.currentTimeMillis()-time; 
                if (realDelay<delay) 
                        return Status.failed("sleep("+delay+") actually sleeped "+realDelay+" msec");
        }
    return Status.passed(""); 
  }

  public static void main(String args[]) {
//   (new sleep0101()).run(args, System.err, System.out).exit();
     System.exit((new sleep0101()).run(args, System.err, System.out));
  }

}
------------------------------------- end of file sleep0101.java

Intermittenly, it failed with results:
Failed. sleep(316) actually sleeped 312 msec
Failed. sleep(32) actually sleeped 31 msec

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

Comments
EVALUATION I don't believe it is a real bug. For now, the Sleep() is carried on system level timer, which has 1ms accuracy. However the System.currentTimeMillis() has an acurracy of 10ms. The result is kind of dubious. ###@###.### 1998-06-05 Because of the accuracy of Timer is 10ms on NT, 50ms on 95, the test itself is not valid. Close as duplicate of 4123961. ###@###.### 1998-06-08
05-06-1998