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

voidsleepTight(final long millis)
Sleep the current thread, ignoring any Exception that might occur.
try {
    Thread.sleep(millis);
} catch (Exception e) {
voidsleepUninterruptedly(long millis)
Utility method that sleeps for a given time without interruption.
try {
    Thread.sleep(millis);
} catch (InterruptedException ie) {
voidsleepUninterruptibly(long msecs)
Sleeps for a given set of milliseconds, uninterruptibly.
long now = System.currentTimeMillis();
long sleepUntil = now + msecs;
long remaining;
while ((remaining = sleepUntil - now) > 0) {
    try {
        Thread.sleep(remaining);
    } catch (InterruptedException e) {
    now = System.currentTimeMillis();
booleansleepUntil(long time)
Sleep until a particular time.
if (time == 0) {
    return true;
} else if (time < 0) {
    throw new IllegalArgumentException("negative: " + time);
long curTime = System.currentTimeMillis();
for (int i = 0; (i < 100) && (curTime < time); i++) {
    try {
...
voidsleepUntil(long timestamp)
sleep Until
long currentTime;
while ((currentTime = System.currentTimeMillis()) < timestamp) {
    try {
        Thread.sleep(timestamp - currentTime);
    } catch (InterruptedException e) {
intsleepUpTo(final long millis)
Method to add a random delay or if none to yeild to give any waiting threads a chance This helps make test threaded code more random to track synchronized issues
try {
    if (millis > 0) {
        int realSleep = (int) Math.floor(Math.random() * (millis + 1));
        if (realSleep > 0) {
            Thread.sleep(realSleep);
            return realSleep;
        } else {
            Thread.yield();
...
voidsleepWithoutInterrupt(final long msToWait)
Sleeps for the given amount of time even if interrupted.
long timeMillis = System.currentTimeMillis();
long endTime = timeMillis + msToWait;
boolean interrupted = false;
while (timeMillis < endTime) {
    try {
        Thread.sleep(endTime - timeMillis);
    } catch (InterruptedException ex) {
        interrupted = true;
...
voidsleepWithoutInterruptions(long milliseconds)
sleep Without Interruptions
try {
    Thread.sleep(milliseconds);
} catch (final InterruptedException e) {
    throw new IllegalStateException(e);
voidsleepyMillis(long millis)
Sleeps for the given count of milli seconds (1000ms = 1sec).
try {
    Thread.sleep(millis);
} catch (InterruptedException e) {