Print

Print


Hi all,

The following code throws some light on the problem I raised here a
while ago:

public final class SleepTimer
{
     public static void main(String[] args)
     {
         long startTime = System.currentTimeMillis();
         try {
             for(int x=0; x<100; x++)
             {
                 Thread.sleep(10);
             }
         } catch (Exception e) {};
         long endTime = System.currentTimeMillis();

         System.out.println(endTime - startTime);
}

On my x86 Linux machine, this consistently prints 1990+. On a colleagues
Windows 2000 machine (almost exactly the same hardware) it prints about
1550. The sleep(millis, nanos) version behaves the same.



By way of explanation, from http://www.jsresources.org/faq_performance.html:

******
6) What precision can I expect from Thread.sleep()?

The fundamental problem with short sleeps is that a call to sleep
finishes the current scheduling time slice. Only after all other
threads/process finished, the call can return.

For the Sun JDK, Thread.sleep(1) is reported to be quite precise on
Windows. For Linux, it depends on the timer interrupt of the kernel. If
the kernel is compiled with HZ=1000 (the default on alpha), the
precision is reported to be good. For HZ=100 (the default on x86) it
typically sleeps for 20 ms.

Using Thread.sleep(millis, nanos) doesn't improve the results. In the
Sun JDK, the nanosecond value is just rounded to the nearest
millisecond. (Matthias)
******



yours,
rob

--
Rob Alexander                   (E-mail: [log in to unmask])
Research Associate, Dept of Computer Science, The University of York,
York, YO10 5DD, UK
Tel: 01904 432792       Fax: 01904 432708