Date precision on Windows

Alan Bateman Alan.Bateman at Sun.COM
Mon Sep 14 13:38:23 UTC 2009


Max (Weijun) Wang wrote:
> Hi All
>
> Recently one of my Kerberos tests always reports "replay detected" 
> error. Turns out the time precision on Windows is quite low.
>
> I've written this tiny program. It calls new Date().getTime() as fast 
> as possible and try to see if the output is continuous:
>
> class A {
>   public static void main(String[] rags) throws Exception {
>     int[] x = new int[100];
>     long l = new java.util.Date().getTime();
>     while (true) {
>       long m = new java.util.Date().getTime();
>       if (m - l >= 100) break;
>       x[(int)(m-l)]++;
>     }
>     for (int i=0; i<100; i++) {
>       System.out.printf("%5d %d\n", i, x[i]);
>     }
>   }
> }
>
> On Linux and Solaris, there are only 2 to 5 zeros in the output, which 
> shows the time is quite continuous on these systems. However, on 
> Windows (a x64 system), only 1/16th are non zeros, which shows the 
> maximum precision on Windows looks like 16 millisec.
>
> Can this be enhanced?
>
> Thanks
> Max
>
David Holmes has a good write-up on this topic here:
  http://blogs.sun.com/dholmes/entry/inside_the_hotspot_vm_clocks

(the no-arg Date constructor uses System.currentTimeMillis)

-Alan.




More information about the core-libs-dev mailing list