Java Sleep sleepRandom(long floor, long ceiling)

Here you can find the source of sleepRandom(long floor, long ceiling)

Description

Sleeps between floor and ceiling milliseconds, chosen randomly

License

LGPL

Declaration

public static void sleepRandom(long floor, long ceiling) 

Method Source Code

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

public class Main {
    /** Sleeps between floor and ceiling milliseconds, chosen randomly */
    public static void sleepRandom(long floor, long ceiling) {
        if (ceiling - floor <= 0) {
            return;
        }/*from  w  w  w  .  j  a  v a  2 s.  c om*/
        long diff = ceiling - floor;
        long r = (int) ((Math.random() * 100000) % diff) + floor;
        sleep(r);
    }

    /** Returns a random value in the range [1 - range] */
    public static long random(long range) {
        return (long) ((Math.random() * range) % range) + 1;
    }

    /** Sleep for timeout msecs. Returns when timeout has elapsed or thread was interrupted */
    public static void sleep(long timeout) {
        try {
            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. sleepQuietly(long millis)
  2. sleepQuietly(long millis)
  3. sleepQuietly(long ms)
  4. sleepQuite(long millis)
  5. sleepRandMs(int ms)
  6. sleepRandom(long timeout)
  7. sleepSafely(final long millis)
  8. sleepSilently(int millis)
  9. sleepSilently(int millis)