Java Sleep sleep(long msecs, boolean busy_sleep)

Here you can find the source of sleep(long msecs, boolean busy_sleep)

Description

On most UNIX systems, the minimum sleep time is 10-20ms.

License

LGPL

Declaration

public static void sleep(long msecs, boolean busy_sleep) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

public class Main {
    /** Sleep for timeout msecs. Returns when timeout has elapsed or thread was interrupted */
    public static void sleep(long timeout) {
        try {/*from  www.  j a v  a 2 s  . c  om*/
            Thread.sleep(timeout);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }

    public static void sleep(long timeout, int nanos) {
        try {
            Thread.sleep(timeout, nanos);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }

    /**
     * On most UNIX systems, the minimum sleep time is 10-20ms. Even if we specify sleep(1), the thread will
     * sleep for at least 10-20ms. On Windows, sleep() seems to be implemented as a busy sleep, that is the
     * thread never relinquishes control and therefore the sleep(x) is exactly x ms long.
     */
    public static void sleep(long msecs, boolean busy_sleep) {
        if (!busy_sleep) {
            sleep(msecs);
            return;
        }

        long start = System.currentTimeMillis();
        long stop = start + msecs;

        while (stop > start) {
            start = System.currentTimeMillis();
        }
    }
}

Related

  1. sleep(long milliTime)
  2. sleep(long ms)
  3. sleep(long ms)
  4. sleep(long ms)
  5. sleep(long msecs, boolean busy_sleep)
  6. sleep(long sleepTime)
  7. sleep(long sleepTime)
  8. sleep(long time)
  9. sleep(long time)