Java Sleep sleep_force(long timeToSleep)

Here you can find the source of sleep_force(long timeToSleep)

Description

Sleep and ignore InterruptedException Goto sleep again if wake 1ms ealier

License

Apache License

Parameter

Parameter Description
timeToSleep in milliseconds

Declaration

public static void sleep_force(long timeToSleep) 

Method Source Code

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

public class Main {
    /**/*from w  w w .  j ava  2 s .  co  m*/
     * Sleep and ignore InterruptedException
     * Goto sleep again if wake 1ms ealier
     * @param timeToSleep in milliseconds
     */
    public static void sleep_force(long timeToSleep) {
        if (timeToSleep <= 0)
            return;
        long target = System.currentTimeMillis() + timeToSleep;
        while (true)
            try {
                Thread.sleep(timeToSleep);
                timeToSleep = target - System.currentTimeMillis();
                if (timeToSleep < 2)
                    return;
            } catch (InterruptedException e) {
                timeToSleep = target - System.currentTimeMillis();
            }
    }

    /**
     * Sleep and wake on InterruptedException
     * @param timeToSleep in milliseconds
     */
    public static void sleep(long timeToSleep) {
        if (timeToSleep <= 0)
            return;
        try {
            Thread.sleep(timeToSleep);
        } catch (InterruptedException e) {
        }
    }
}

Related

  1. sleep(long time)
  2. sleep(long time)
  3. sleep(long time)
  4. sleep(long timeToSleep)
  5. sleep4Subscription()
  6. sleepABit(long millis)
  7. sleepABitMore()
  8. sleepAfterListItems()
  9. sleepAndIgnoreInterrupts(int millis)