I want to perform a simple math function with a very small elapse time between each calculation. I am trying to use TimeUnit with Nanosecond sleep, but the elapse time for 10 iterations is almost 50 millisecond. If I remove the sleep the loop runs in less than 1 millisecond. I would appreciate any suggestions.
Output run: elapse time for this test was 47.0 [ August 25, 2008: Message edited by: Larry Frissell ]
Causes the currently executing thread to sleep (cease execution) for the specified number of milliseconds plus the specified number of nanoseconds, subject to the precision and accuracy of system timers and schedulers. The thread does not lose ownership of any monitors.
Now in Windows, sleep actions can easily take 10ms, even though you only want to sleep 1. Therefore, you should never rely on sleep actions for timing issues.
Okay, thank you for the response. I was reading this API under TimeUnit:
sleep public void sleep(long timeout) throws InterruptedExceptionPerforms a Thread.sleep using this unit. This is a convenience method that converts time arguments into the form required by the Thread.sleep method.