Java Utililty Methods Sleep

List of utility methods to do Sleep

Description

The list of methods to do Sleep are organized into topic(s).

Method

voidsleepMillis(int milliseconds)
sleep Millis
try {
    Thread.sleep(milliseconds);
} catch (InterruptedException e) {
    Thread.currentThread().interrupt();
    throw new IllegalStateException("Interrupted sleeping", e);
voidsleepMillis(long millis)
Sleep for the specific number of milliseconds
try {
    Thread.sleep(millis);
} catch (InterruptedException ex) {
voidsleepMillis(long millis)
sleep Millis
try {
    Thread.sleep(millis);
} catch (InterruptedException ignored) {
voidsleepMilliseconds(long ms)

Sleeps for the specified time--in milliseconds.

try {
    Thread.sleep(ms);
} catch (Exception e) {
voidsleepMilliseconds(long ms)
sleep Milliseconds
try {
    Thread.sleep(ms);
} catch (InterruptedException e) {
    e.printStackTrace();
voidsleepMs(final int millis)
sleep Ms
try {
    Thread.sleep(millis);
} catch (InterruptedException e) {
    e.printStackTrace();
voidsleepMSInChunks(long ms)
Sleeps in chunks of 10ms, to prevent the risk of a GC eating the whole sleeping duration, and not letting program enough duration to make progress.
final long chunkMS = 10;
while (ms >= chunkMS) {
    try {
        Thread.sleep(chunkMS);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    ms -= chunkMS;
...
voidsleepNanos(final long nanoDuration)
Sleeps for specified amount of time in nanoseconds.
final long end = System.nanoTime() + nanoDuration;
long timeLeft = nanoDuration;
do {
    if (timeLeft > SLEEP_PRECISION) {
        Thread.sleep(1);
    } else if (timeLeft > SPIN_YIELD_PRECISION) {
        Thread.yield();
    timeLeft = end - System.nanoTime();
} while (timeLeft > 0);
voidsleepNanos(long nanoseconds)
sleep Nanos
long startTime = System.nanoTime();
long endTime = startTime + nanoseconds;
while (endTime >= System.nanoTime()) {
voidsleepNoException(int ms)
sleep No Exception
try {
    Thread.sleep(ms);
} catch (InterruptedException e) {
    Thread.currentThread().interrupt();
    throw new RuntimeException("Sleep was interrupted");