Here you can find the source of sleep_force(long timeToSleep)
Parameter | Description |
---|---|
timeToSleep | in milliseconds |
public static void sleep_force(long timeToSleep)
//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) { } } }