Search code examples
javamultithreadingeventstimeouttimedelay

Add a decimal millisecond delay to Java code


I want to add a delay of 0.488 ms to my java code. but thread.sleep() and Timer functions only allow a granularity of millisecond. How do I specify a delay amount below that level ?


Solution

  • Since 1.5 you can use this nice method java.util.concurrent.TimeUnit.sleep(long timeout):

    TimeUnit.SECONDS.sleep(1);
    TimeUnit.MILLISECONDS.sleep(1000);
    TimeUnit.MICROSECONDS.sleep(1000000);
    TimeUnit.NANOSECONDS.sleep(1000000000);