Does Thread Sleep guarantee to sleep for at least n milliseconds, we're just playing with some timers on Windows ... now we're aware of issues with the underlying resolution of the Windows timers (10-16ms) native and that sleep may come back later than asked if the JVM has other things to do but we appeared to see it come back early (uninterruped). It seems occasionally to report its slept for 486 rather than 514 (now obviously double the inaccuracy in the timing resolution (two get's_ would cover it but I wouldn't have guessed this was allowed i.e. I would have guessed they would have said at least n milliseconds but how much over OS / circumstance dependent) . Anyone any experience in this area ?
Same tests on Solaris were fine and as predicted (always slept (measured)) at least as long as requested,
Colleague's code follows ..
"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
I wonder if the problem comes from the same inaccuracies in the clock making their appearance in the Syste.currentTimeMillis() calls (and so distorting your calculated ellapsed time)? Have you tried using the System.nanoTime() to see if that makes a difference?
Using system nano gave more accurate results but in the same way still earlier wakeups, tried Timer vis sleep and this gave averages around the time required Timer slightly earlier and Thread slightly higher still exhibiting too early on XP.