Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a Thread sleep for less than half milli seconds in Java/ Other language?

[Edit]: After getting answer I understood its not specific to Java, its related to OS scheduler as well, so adding other tags

Is it possible in Java to make a thread sleep for a nano seconds.

Of course after looking the Thread api where we can pass nano seconds as well in sleep method, the answer could be yes.

But I doubt after looking the implementation/ source of sleep method in Thread class, which is:

public static void sleep(long millis, int nanos)
throws InterruptedException {
    if (millis < 0) {
        throw new IllegalArgumentException("timeout value is negative");
    }

    if (nanos < 0 || nanos > 999999) {
        throw new IllegalArgumentException(
                            "nanosecond timeout value out of range");
    }

    if (nanos >= 500000 || (nanos != 0 && millis == 0)) {
        millis++;
    }

    sleep(millis);
}

Now according to the logic it is increasing milli seconds by 1 if the passed nano seconds is more than half milli seconds. But this sounds illogical to me, lets say I have written a code where one of my thread is waiting for say some 40000 nano seconds (in practical scenario it might not be the case) which is less than half milli seconds that means my thread will not wait at all.

Can someone please comment on the same and why this design was decided to wait for milli seconds rather than nano seconds?

Also what would guarantee that the thread wakes up accurately?

like image 980
Vishrant Avatar asked Mar 08 '16 18:03

Vishrant


People also ask

What can be used instead of thread sleep in Java?

TimeUnit provides a human-readable version of the Thread. sleep() method which can be used in place of the former. For a long time Thread's sleep() method is a standard way to pause a Thread in Java and almost every Java programmer is familiar with that.

Is thread sleep in seconds or milliseconds?

thread sleep() method is used to suspend the current thread for a specified amount of time in milliseconds. If you look at Oracle docs, you will find that there are two overloaded sleep() methods of the Thread class. where millis is the time in milliseconds.

How do I make my thread sleep for 1 minute?

To make a thread sleep for 1 minute, you do something like this: TimeUnit. MINUTES. sleep(1);

How long is thread sleep 1000?

sleep time means more than what you really intended. 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.


1 Answers

You can use LockSupport's parkNanos

LockSupport.parkNanos(400_000);

however this is not the same as sleep (it is not interruptable) and all it does is pass the request to the OS. On Windows 8 for example, even parkNanos(1) might sleep for 1 milli-second.

As biziclop pointed out, there Javadoc mentions

The call spuriously (that is, for no reason) returns.

This happens rarely in my experience, but it does happen.


However, you are right that Thread.sleep() will always sleep for at least 1 ms. On Win XP it might sleep for 16 ms (1/60th of a second)

Also what would guarantee that the thread wakes up accurately?

Using a real time operating system.

What I do, is don't go to sleep but rather busy wait. This way I can stop for a give amount of time with more accuracy. If you run your thread on an isolated CPU (In Linux) you can reduce the variation to about 10 micro-seconds

An example of busy waiting

long end = System.nanoTime() + delay;
while (System.nanoTime() < end) { /* busy waiting */ }

or if you want to be slightly more friendly

while (System.nanoTime() < end) 
     Thread.yield();
like image 189
Peter Lawrey Avatar answered Sep 18 '22 12:09

Peter Lawrey