Java tutorial
//package com.java2s; //License from project: Apache License public class Main { /** * Ensures the current thread sleeps at least the given milliseconds before * returning from this method. */ public static void ensureSleepNanos(long nanos) throws InterruptedException { if (nanos < 1) return; long now = System.nanoTime(); final long targetTime = now + nanos; do { long millis = nanos / (1000 * 1000); int fractionalNanos = (int) (nanos % (1000 * 1000)); Thread.sleep(millis, fractionalNanos); nanos = targetTime - System.nanoTime(); } while (nanos > 0); } }