Add a decimal millisecond delay to Java code

Aditya picture Aditya · Nov 27, 2012 · Viewed 23k times · Source

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 ?

Answer

user381105 picture user381105 · Nov 27, 2012

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);