[concurrency-interest] Best way to cause short delays

Holger Hoffstätte holger.hoffstaette at googlemail.com
Mon Jan 18 07:10:07 EST 2010


Peter Veentjer wrote:
> The main back off implementation I use is an exponential one that
> delays from a few microseconds to a few milliseconds by calling the
> Thread.sleep(ms, ns). The problem is that a delay of a few
> microseconds is not possible;
> I automatically get a delay of a few milliseconds, so the accuracy is
> very low. So what is the best way to let a thread delay for such small
> amounts of time?

Required reading:
http://blogs.sun.com/dholmes/entry/inside_the_hotspot_vm_clocks

Another blog on the the Thread.sleep(ms, ns) misbehaviour:
http://www.sagui.org/~gustavo/blog/code/high-resolution-timer-in-java-5.html

So LockSupport.parkNanos should work better than sleep() as long as CPU,
JVM and OS all play along. After having a good time with rt-Linux and
low-latency messaging last year I can assure you that this is rather
unlikely. :-)

-h


More information about the Concurrency-interest mailing list