Class NanoTime

java.lang.Object
org.eclipse.jetty.util.NanoTime

public class NanoTime extends Object

Utility class with methods that deal with System.nanoTime().

  • Method Summary

    Modifier and Type
    Method
    Description
    static long
    elapsed(long beginNanoTime, long endNanoTime)
    Calculates the nanoseconds elapsed between two nanoTimes.
    static boolean
    isBefore(long nanoTime1, long nanoTime2)
    Returns whether the first nanoTime is strictly before the second nanoTime.
    static boolean
    isBeforeOrSame(long nanoTime1, long nanoTime2)
    Returns whether the first nanoTime is before or the same as the second nanoTime.
    static long
    millisElapsed(long beginNanoTime, long endNanoTime)
    Calculates the milliseconds elapsed between two nanoTimes.
    static long
    millisSince(long beginNanoTime)
    Calculates the milliseconds elapsed between a begin nanoTime and the current nanoTime.
    static long
    millisUntil(long endNanoTime)
    Calculates the milliseconds remaining between the current nanoTime and an end nanoTime.
    static long
    now()
     
    static long
    secondsElapsed(long beginNanoTime, long endNanoTime)
    Calculates the seconds elapsed between two nanoTimes.
    static long
    secondsSince(long beginNanoTime)
    Calculates the seconds elapsed between a begin nanoTime and the current nanoTime.
    static long
    secondsUntil(long endNanoTime)
    Calculates the seconds remaining between the current nanoTime and an end nanoTime.
    static long
    since(long beginNanoTime)
    Calculates the nanoseconds elapsed since a begin nanoTime and the current nanoTime.
    static void
    spinWait(long nanos)
    Spin waits for the specified number of nanoseconds.
    static long
    until(long endNanoTime)
    Calculates the nanoseconds remaining from the current nanoTime until an end nanoTime.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • now

      public static long now()
      Returns:
      the current nanoTime via System.nanoTime()
    • elapsed

      public static long elapsed(long beginNanoTime, long endNanoTime)

      Calculates the nanoseconds elapsed between two nanoTimes.

      Parameters:
      beginNanoTime - the begin nanoTime
      endNanoTime - the end nanoTime
      Returns:
      the nanoseconds elapsed
    • since

      public static long since(long beginNanoTime)

      Calculates the nanoseconds elapsed since a begin nanoTime and the current nanoTime.

      Parameters:
      beginNanoTime - the begin nanoTime
      Returns:
      the nanoseconds elapsed since the given begin nanoTime and the current nanoTime
    • until

      public static long until(long endNanoTime)

      Calculates the nanoseconds remaining from the current nanoTime until an end nanoTime.

      Parameters:
      endNanoTime - the end nanoTime
      Returns:
      the nanoseconds remaining from the current nanoTime until the given end nanoTime
    • millisElapsed

      public static long millisElapsed(long beginNanoTime, long endNanoTime)

      Calculates the milliseconds elapsed between two nanoTimes.

      Parameters:
      beginNanoTime - the begin nanoTime
      endNanoTime - the end nanoTime
      Returns:
      the milliseconds elapsed
    • millisSince

      public static long millisSince(long beginNanoTime)

      Calculates the milliseconds elapsed between a begin nanoTime and the current nanoTime.

      Parameters:
      beginNanoTime - the begin nanoTime
      Returns:
      the milliseconds elapsed between the given begin nanoTime and the current nanoTime
    • millisUntil

      public static long millisUntil(long endNanoTime)

      Calculates the milliseconds remaining between the current nanoTime and an end nanoTime.

      Parameters:
      endNanoTime - the end nanoTime
      Returns:
      the milliseconds remaining between the current nanoTime and the given end nanoTime
    • secondsElapsed

      public static long secondsElapsed(long beginNanoTime, long endNanoTime)

      Calculates the seconds elapsed between two nanoTimes.

      Parameters:
      beginNanoTime - the begin nanoTime
      endNanoTime - the end nanoTime
      Returns:
      the seconds elapsed
    • secondsSince

      public static long secondsSince(long beginNanoTime)

      Calculates the seconds elapsed between a begin nanoTime and the current nanoTime.

      Parameters:
      beginNanoTime - the begin nanoTime
      Returns:
      the seconds elapsed between the given begin nanoTime and the current nanoTime
    • secondsUntil

      public static long secondsUntil(long endNanoTime)

      Calculates the seconds remaining between the current nanoTime and an end nanoTime.

      Parameters:
      endNanoTime - the end nanoTime
      Returns:
      the seconds remaining between the current nanoTime and the given end nanoTime
    • isBefore

      public static boolean isBefore(long nanoTime1, long nanoTime2)

      Returns whether the first nanoTime is strictly before the second nanoTime.

      Reads as: "is nanoTime1 strictly before nanoTime2?".

      Avoids the common mistake of comparing the 2 nanoTimes with the less-than < operator, which cannot be used when comparing nanoTimes, as specified in System.nanoTime().

      Parameters:
      nanoTime1 - the first nanoTime
      nanoTime2 - the second nanoTime
      Returns:
      whether the first nanoTime is strictly before the second nanoTime
      See Also:
    • isBeforeOrSame

      public static boolean isBeforeOrSame(long nanoTime1, long nanoTime2)

      Returns whether the first nanoTime is before or the same as the second nanoTime.

      Parameters:
      nanoTime1 - the first nanoTime
      nanoTime2 - the second nanoTime
      Returns:
      whether the first nanoTime is before or the same as the second nanoTime
      See Also:
    • spinWait

      public static void spinWait(long nanos)

      Spin waits for the specified number of nanoseconds.

      Parameters:
      nanos - the amount of nanoseconds to spin wait