Accurate Sleep for Java on Windows

To improve granularity of sleep you can try the following from this Thread.sleep page.

Bugs with Thread.sleep() under Windows

If timing is crucial to your
application, then an inelegant but
practical way to get round these bugs
is to leave a daemon thread running
throughout the duration of your
application that simply sleeps for a
large prime number of milliseconds
(Long.MAX_VALUE will do). This way,
the interrupt period will be set once
per invocation of your application,
minimising the effect on the system
clock, and setting the sleep
granularity to 1ms even where the
default interrupt period isn’t 15ms.

The page also mentions that it causes a system-wide change to Windows which may cause the user’s clock to run fast due to this bug.

EDIT

More information about this is available
here and an associated bug report from Sun.

Leave a Comment