Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How accurate is Thread.sleep?

Tags:

I'm studying computer hardware where we learn that using a hardware timer gets more accurate results than a software delay. I've written a software delay in assembly for 1 millisecond and I can start a process that repeats every millisecod using this delay and with a counter do something else every 100th millisecond, and this technique would be less accurate than using a hardware timer that I got builtin with my hardware that I'm going to use now.

So I wonder how accurate is the timing that is builtin with Java? We have System.currentTimeMillis and Thread.sleep and these might not use hardware timers, so how accurate are these builtin Java methods compared to a hardware timer?

like image 350
Niklas Rosencrantz Avatar asked Sep 11 '13 08:09

Niklas Rosencrantz


People also ask

Is thread sleep inaccurate?

Thread. sleep() is inaccurate. How inaccurate depends on the underlying operating system and its timers and schedulers.

How long is thread sleep 1000?

For example, with thread. sleep(1000), you intended 1,000 milliseconds, but it could potentially sleep for more than 1,000 milliseconds too as it waits for its turn in the scheduler. Each thread has its own use of CPU and virtual memory.

How accurate is sleep C++?

Another answer about the Sleep() accuracy is this. However, whether it is possible to obtain a Sleep() delay of precisely 5 ms depends on the systems hardware. Some systems allow you to operate at 1024 interrupts per second (set by the multimedia timer API). This corresponds to a period of 0.9765625 ms.

How long should a thread sleep?

If it is a UI worker thread, as long as they have some kind of progress indicator, anywhere up to half a second should be good enough. The UI should be responsive during the operation since its a background thread and you definitely have enough CPU time available to check every 500 ms.


2 Answers

Thread.sleep() is inaccurate. How inaccurate depends on the underlying operating system and its timers and schedulers. I've experienced that garbage collection going on in parallel can lead to excessive sleep.

When doing "real time" simulations you have to correct your main loop for the inaccuracies of sleep. This can be done by calculating the expected time of wake-up and comparing that to the actual time of wake-up and correcting for the difference in the next loop.

If you need better performance, have a look at the Java Real-Time System specification. I haven't used it myself so I can't provide further details.

like image 75
DeltaLima Avatar answered Nov 14 '22 18:11

DeltaLima


I did not find Thread.sleep to be accurate. In fact, I found that it seems to sleep less than what it promises.

Results when configured to sleep 1 millisecond:

********* Average elapsed time = 957 micro seconds 

Below is my test:

public static void main(String[] args) throws InterruptedException {     List<Long> list = new ArrayList<Long>();      int i= 0;     while(i <=100){          long authStartTime = System.nanoTime();         Thread.sleep(1);         long elapsedTime = System.nanoTime() - authStartTime;         System.out.println("*****"+elapsedTime/1000+ " mics");         list.add(elapsedTime);         i++;     }      long sum=0;     for(long l : list){         sum = sum + l;     }     System.out.println("********* Average elapsed time = "+sum/list.size()/1000+" micro seconds");     System.exit(0); } 
like image 33
mario Avatar answered Nov 14 '22 17:11

mario