1

I have an application that uses Thread.sleep(msUntilActionNeeded);. The variable is set to the number of ms until I want the program to begin an operation which coincides with the time of day, so it is not critical. Even being off by 5000ms would not be a problem, so a 10ms resolution is overkill.

The (minor) problem is that Windows reports A program or service has requested a timer resolution smaller than the platform maximum timer resolution.

There is no need to have the timer resolution so small. I have rounded the ms to the nearest multiple of 10ms with no apparent effect.

How can I change my program or JVM settings to make it so the requested timer resolution is not so small?

Dale
  • 4,421
  • 2
  • 37
  • 68
  • Above, I said "I have rounded the ms to the nearest multiple of 10ms", but I found an instance where I didn't do that. I changed that one, and the other Thread.sleep(), to round to the nearest 100ms (long msUntilWake = ((getMsUntilNextEvent() - LEAD_TIME_MS + 50)/100)*100;) and the "resolution too small" error was gone! – Dale Apr 20 '15 at 19:59

2 Answers2

0

You can't control the timer resolution directly from Java.

The JVM and/or other applications sometimes reduce the platform timer resolution, for smoother animations and other reasons. Java doesn't give you control over the platform timer resolution.

Are you sure that the Java Runtime is responsible for increasing the timer resolution? Have you tried using another JRE?

If you want to change the timer resolution, you need JNI. But here's an interesting blogpost by Bruce Dawson about the reasons why you wouldn't want to increase the timer resolution (wasting electrical and computing power).

Simon
  • 1,586
  • 17
  • 29
0

A bit baffled. Maybe an int overflow yielding negative numbers? Maybe too small?

if (msUntilActionNeeded < 0) {
    throw new IllegalStateException();
} else if (msUntilActionNeeded > 100L) {
    Thread.sleep(msUntilActionNeeded);
}
Joop Eggen
  • 96,344
  • 7
  • 73
  • 121