Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6995364

System.nanoTime() does not work properly

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 6u22
    • core-libs
    • x86
    • windows_xp

      FULL PRODUCT VERSION :
      java version "1.6.0_20"
      Java(TM) SE Runtime Environment (build 1.6.0_20-b02)
      Java HotSpot(TM) Client VM (build 16.3-b01, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
        From the javadocs for System.nanoTime():

           * The value returned represents nanoseconds since some fixed but
           * arbitrary time (perhaps in the future, so values may be
           * negative).

      However, in this case, System.nanoTime() does not return the number of nanoseconds since ANY fixed time.

      If two calls to System.nanoTime() are made, the difference between the two values does not represent the number of nano-seconds that actually elapsed.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      The problem is 100% reproducible on certain machines and can be seen by running the test program below.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The program prints out two columns of numbers. The first column is the time from System.currentTimeMillis(). The second colum is the number of ms returned from System.nanoTime(). I would expect that the numbers in each column would increase by roughly 1000.

      (Note that I'm intentionally printing ms of nano-seconds.)
      ACTUAL -
      Here is the actual data printed:

      1288199116375 61530042
      11288199117375 61530438
      11288199118375 61530808
      11288199119375 61531183
      11288199120375 61531559
      11288199121375 61531946
      11288199122375 61532337
      11288199123375 61532750

      As you can see, the numbers in the first column, from System.currentTimeMillis(), do in fact increase by 1000. But the numbers from System.nanoTime() in the second column increase only by an average of 387.

      Since our program uses System.nanoTime() to time certain events, this causes a major problem.

      Note that this output is seen only on certain machines. On other machines, the expected output is observed.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public class test {
      public static void main(String[] args) {
      while( true ) {
      try {
      Thread.sleep(1000);
      }
      catch (InterruptedException e) {
      }

      System.out.println( System.currentTimeMillis() + "\t" + nanoTimeMillis() );
      }
      }

      static long nanoTimeMillis() {
      return Math.round(System.nanoTime() / 1000000.0);
      }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      The workaround is to use System.currentTimeMillis().

            mduigou Mike Duigou
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: