Name: jl125535 Date: 05/03/2004
FULL PRODUCT VERSION :
java version "1.4.2_04"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_04-b05)
Java HotSpot(TM) Client VM (build 1.4.2_04-b05, mixed mode)
Does NOT occur on:
java version "1.3.1"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1-b24)
Java HotSpot(TM) Client VM (build 1.3.1-b24, mixed mode)
FULL OS VERSION :
Fedora core 1 (Linux) (i686 athlon i386 GNU/Linux)
Does NOT occur on: SunOS (5.8 Generic_108528-27 sun4u sparc SUNW,Sun-Blade-1000)
A DESCRIPTION OF THE PROBLEM :
Thread.sleep sleeps for an extra 10ms. For example Thread.sleep(1000) sleeps 1010ms.
And this is the case with all sleeptimes except 0.
The resolution of sleep on my configuration is 10ms. 31-39ms are rounded to 40ms, 101-109ms are rounded to 110ms etc.
I also tried the program with sleeptime being 91ms, which is rounded up to 100ms -> sleeps for 110ms.
System.currentTimeMillis() seems to be accurate since I get everything starting from a single sleep(10) measured (1x10ms results ~20ms, 1x20ms results ~30ms etc). Measuring loops without sleep I get around 0.
The problem can't be in System.currentTimeMillis() because if it gave an extra 10ms, I would be getting 100*100m -> 10010, not 11000.
I tried on the "Does NOT occur" configuration shown above and everything worked as presumed
(100ms * 100 -> 10000ms).
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the program with different sleeptimes.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
You should receive approximation of the sleeptime used multiplied with 100.
Sleep(100) -> 10 000
Sleep(1000) -> 100 000
ACTUAL -
But I receive:
Sleep(100) -> 11 000
Sleep(1000) -> 101 000
And by calculating only single sleepcalls:
Sleep(30) -> 40
Sleep(100) -> 110
Sleep(1000) -> 1010
Sleep(0) -> 0
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
public class Test
{
public static void main(String[] args)
{
long start = System.currentTimeMillis();
for(int i = 0; i < 100; i++) {
try {
Thread.sleep(100);
} catch( InterruptedException e) {}
}
long stop = System.currentTimeMillis();
System.out.println("Time: " + (stop - start));
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Obviously reduce 10ms from each sleepcall (with at least 10ms as sleeptime).
(Incident Review ID: 244625)
======================================================================