@Test(timeout=60000) public void testSleepWithoutInterrupt() throws InterruptedException { Thread sleeper = new Thread(new Runnable() { @Override public void run() { LOG.debug("Sleeper thread: sleeping for " + SLEEP_TIME_MS); Threads.sleepWithoutInterrupt(SLEEP_TIME_MS); LOG.debug("Sleeper thread: finished sleeping"); wasInterrupted.set(Thread.currentThread().isInterrupted()); } }); LOG.debug("Starting sleeper thread (" + SLEEP_TIME_MS + " ms)"); sleeper.start(); long startTime = System.currentTimeMillis(); LOG.debug("Main thread: sleeping for 200 ms"); Threads.sleep(200); LOG.debug("Interrupting the sleeper thread and sleeping for 500 ms"); sleeper.interrupt(); Threads.sleep(500); LOG.debug("Interrupting the sleeper thread and sleeping for 800 ms"); sleeper.interrupt(); Threads.sleep(800); LOG.debug("Interrupting the sleeper thread again"); sleeper.interrupt(); sleeper.join(); assertTrue("sleepWithoutInterrupt did not preserve the thread's " + "interrupted status", wasInterrupted.get()); long timeElapsed = System.currentTimeMillis() - startTime; assertTrue("Elapsed time " + timeElapsed + " ms is out of the expected " + " sleep time of " + SLEEP_TIME_MS, SLEEP_TIME_MS - timeElapsed < TOLERANCE_MS); LOG.debug("Target sleep time: " + SLEEP_TIME_MS + ", time elapsed: " + timeElapsed); }
public static void sleepWithoutInterrupt(final long msToWait) { long timeMillis = System.currentTimeMillis(); long endTime = timeMillis + msToWait; boolean interrupted = false; while (timeMillis < endTime) { try { Thread.sleep(endTime - timeMillis); } catch (InterruptedException ex) { interrupted = true; } timeMillis = System.currentTimeMillis(); } if (interrupted) { Thread.currentThread().interrupt(); } }
Threads { public static void sleepWithoutInterrupt(final long msToWait) { long timeMillis = System.currentTimeMillis(); long endTime = timeMillis + msToWait; boolean interrupted = false; while (timeMillis < endTime) { try { Thread.sleep(endTime - timeMillis); } catch (InterruptedException ex) { interrupted = true; } timeMillis = System.currentTimeMillis(); } if (interrupted) { Thread.currentThread().interrupt(); } } }
Threads { public static void sleepWithoutInterrupt(final long msToWait) { long timeMillis = System.currentTimeMillis(); long endTime = timeMillis + msToWait; boolean interrupted = false; while (timeMillis < endTime) { try { Thread.sleep(endTime - timeMillis); } catch (InterruptedException ex) { interrupted = true; } timeMillis = System.currentTimeMillis(); } if (interrupted) { Thread.currentThread().interrupt(); } }  }
Threads { public static void sleepWithoutInterrupt(final long msToWait) { long timeMillis = System.currentTimeMillis(); long endTime = timeMillis + msToWait; boolean interrupted = false; while (timeMillis < endTime) { try { Thread.sleep(endTime - timeMillis); } catch (InterruptedException ex) { interrupted = true; } timeMillis = System.currentTimeMillis(); } if (interrupted) { Thread.currentThread().interrupt(); } }  static Thread setDaemonThreadRunning(final Thread t); static Thread setDaemonThreadRunning(final Thread t,
    final String name); static Thread setDaemonThreadRunning(final Thread t,
    final String name, final UncaughtExceptionHandler handler); static void shutdown(final Thread t); static void shutdown(final Thread t, final long joinwait); static void threadDumpingIsAlive(final Thread t); static void sleep(long millis); static void sleepWithoutInterrupt(final long msToWait); static ThreadPoolExecutor getBoundedCachedThreadPool(
      int maxCachedThread, long timeout, TimeUnit unit,
      ThreadFactory threadFactory); static ThreadFactory getNamedThreadFactory(final String prefix); static ThreadFactory newDaemonThreadFactory(final String prefix); static ThreadFactory newDaemonThreadFactory(final String prefix,
      final UncaughtExceptionHandler handler); }
Threads { public static void sleepWithoutInterrupt(final long msToWait) { long timeMillis = System.currentTimeMillis(); long endTime = timeMillis + msToWait; boolean interrupted = false; while (timeMillis < endTime) { try { Thread.sleep(endTime - timeMillis); } catch (InterruptedException ex) { interrupted = true; } timeMillis = System.currentTimeMillis(); } if (interrupted) { Thread.currentThread().interrupt(); } }  static Thread setDaemonThreadRunning(final Thread t); static Thread setDaemonThreadRunning(final Thread t,
    final String name); static Thread setDaemonThreadRunning(final Thread t,
    final String name, final UncaughtExceptionHandler handler); static void shutdown(final Thread t); static void shutdown(final Thread t, final long joinwait); static void threadDumpingIsAlive(final Thread t); static void sleep(long millis); static void sleepWithoutInterrupt(final long msToWait); static ThreadPoolExecutor getBoundedCachedThreadPool(
      int maxCachedThread, long timeout, TimeUnit unit,
      ThreadFactory threadFactory); static ThreadFactory getNamedThreadFactory(final String prefix); static ThreadFactory newDaemonThreadFactory(final String prefix); static ThreadFactory newDaemonThreadFactory(final String prefix,
      final UncaughtExceptionHandler handler);  }