Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Thread.sleep for minimum time [duplicate]

The TimeUnit.sleep(long timeout) documentation describes its argument thus:

timeout - the minimum time to sleep.

However, I'm finding that — at least on Windows 7 64-bit with Java 8 update 141 — the thread often sleeps for less than the minimum:

public static void main(String[] args) throws InterruptedException {
    final long from = TimeUnit.MILLISECONDS.toNanos(100);
    final long to   = TimeUnit.MILLISECONDS.toNanos(1000);
    final long step = TimeUnit.MILLISECONDS.toNanos(100);
    for (long requestedSleepDuration = from; requestedSleepDuration < to; requestedSleepDuration += step) {
        long sleepStartTime = System.nanoTime();
        TimeUnit.NANOSECONDS.sleep(requestedSleepDuration);
        long sleepEndTime = System.nanoTime();
        System.out.printf(
                "requested=%9d  actual=%9d  %s%n",
                requestedSleepDuration,
                sleepEndTime - sleepStartTime,
                (sleepEndTime - sleepStartTime >= requestedSleepDuration ? "OK" : " Slept less than minimum!"));
    }
}

Typical output:

requested=100000000  actual= 99534864  Slept less than minimum!
requested=200000000  actual=200063646  OK
requested=300000000  actual=299223086  Slept less than minimum!
requested=400000000  actual=399598620  Slept less than minimum!
requested=500000000  actual=499910360  Slept less than minimum!
requested=600000000  actual=600028523  OK
requested=700000000  actual=699604816  Slept less than minimum!
requested=800000000  actual=799230602  Slept less than minimum!
requested=900000000  actual=899490648  Slept less than minimum!

This seems to contradict the documentation. However, the TimeUnit doc also states that TimeUnit.sleep() is a convenience wrapper for Thread.sleep, and the latter doesn't say if it guarantees to sleep at least the specified amount.

Is this an API implementation error, or is TimeUnit.sleep and/or Thread.sleep designed to only sleep for approximately, rather than at least, the specified duration?

like image 316
Klitos Kyriacou Avatar asked Jul 31 '17 15:07

Klitos Kyriacou


People also ask

Why thread sleep is not recommended?

Thread. sleep is bad! It blocks the current thread and renders it unusable for further work.

Do you use thread sleep () frequently?

You need to write sleep() method whenever we need to make webdriver wait. So if you want to wait for two web elements, you need to write Thread. sleep() twice just before you locate web elements. It is not good programming practice.

What is the difference between sleep () and wait () methods?

Wait() method releases lock during Synchronization. Sleep() method does not release the lock on object during Synchronization. Wait() should be called only from Synchronized context. There is no need to call sleep() from Synchronized context.

How do you make a thread wait for some time?

In between, we have also put the main thread to sleep by using TimeUnit. sleep() method. So the main thread can wait for some time and in the meantime, T1 will resume and complete its execution.


1 Answers

TimeUnit.sleep() delegates to Thread.sleep().

Thread.sleep() is subject to the precision and accuracy of system timers and schedulers, thus TimeUnit.sleep() will not be as accurate as you may need.

like image 173
UserF40 Avatar answered Oct 13 '22 04:10

UserF40