Class PlatformTimer

java.lang.Object
org.eclipse.jetty.toolchain.perf.PlatformTimer

public class PlatformTimer extends Object
Detects and reports the timer resolution of the current running platform.
Unfortunately, Thread.sleep(long) on many platforms has a resolution of 1 ms or even of 10 ms, so calling Thread.sleep(2) often results in a 10 ms sleep.
The same applies for Thread.sleep(long, int) and Object.wait(long, int): they are not accurate, especially on virtualized platforms (like Amazon EC2, where the resolution can be as high as 64 ms).
System.nanoTime() is precise enough, but we would need to loop continuously checking the nano time until the sleep period is elapsed; to avoid busy looping pegging the CPUs, Thread.yield() is called to attempt to reduce the CPU load.
Typical usage to impose a precise throughput to requests:
 PlatformTimer timer = PlatformTimer.detect();
 for (int i = 0; i < 100; ++i)
 {
     performRequest();
     timer.sleep(microseconds);
 }
 
  • Method Details

    • getNativeResolution

      public long getNativeResolution()
    • getEmulatedResolution

      public long getEmulatedResolution()
    • sleep

      public void sleep​(long micros)
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • detect

      public static PlatformTimer detect()