Class Clock

java.lang.Object
java.time.Clock
org.apache.jackrabbit.oak.stats.Clock
All Implemented Interfaces:
InstantSource
Direct Known Subclasses:
Clock.Fast, Clock.Virtual

public abstract class Clock extends Clock
Mechanism for keeping track of time at millisecond accuracy.

As of Oak 1.20, this extends from Clock.

  • Field Details

    • SIMPLE

      public static Clock SIMPLE
      Simple clock implementation based on System.currentTimeMillis(), which is known to be rather slow on some platforms.
    • ACCURATE

      public static Clock ACCURATE
      Accurate clock implementation that uses interval timings from the System.nanoTime() method to calculate an as accurate as possible time based on occasional calls to System.currentTimeMillis() to prevent clock drift.
  • Constructor Details

    • Clock

      public Clock()
  • Method Details

    • getTime

      public abstract long getTime()
      Returns the current time in milliseconds since the epoch.
      Returns:
      current time in milliseconds since the epoch
      See Also:
    • getTimeMonotonic

      public long getTimeMonotonic()
      Returns a monotonically increasing timestamp based on the current time. A call to this method will always return a value that is greater than or equal to a value returned by any previous call. This contract holds even across multiple threads and in cases when the system time is adjusted backwards. In the latter case the returned value will remain constant until the previously reported timestamp is again reached.
      Returns:
      monotonically increasing timestamp
    • getTimeIncreasing

      public long getTimeIncreasing() throws InterruptedException
      Returns a strictly increasing timestamp based on the current time. This method is like getTimeMonotonic(), with the exception that two calls of this method will never return the same timestamp. Instead this method will explicitly wait until the current time increases beyond any previously returned value. Note that the wait may last long if this method is called frequently from many concurrent thread or if the system time is adjusted backwards. The caller should be prepared to deal with an explicit interrupt in such cases.
      Returns:
      strictly increasing timestamp
      Throws:
      InterruptedException - if the wait was interrupted
    • getDate

      public Date getDate()
      Convenience method that returns the getTime() value as a Date instance.
      Returns:
      current time
    • getDateMonotonic

      public Date getDateMonotonic()
      Convenience method that returns the getTimeMonotonic() value as a Date instance.
      Returns:
      monotonically increasing time
    • getDateIncreasing

      public Date getDateIncreasing() throws InterruptedException
      Convenience method that returns the getTimeIncreasing() value as a Date instance.
      Returns:
      strictly increasing time
      Throws:
      InterruptedException
    • waitUntil

      public void waitUntil(long timestamp) throws InterruptedException
      Waits until the given point in time is reached. The current thread is suspended until the getTimeIncreasing() method returns a time that's equal or greater than the given point in time.
      Parameters:
      timestamp - time in milliseconds since epoch
      Throws:
      InterruptedException - if the wait was interrupted
    • getZone

      public ZoneId getZone()
      Specified by:
      getZone in class Clock
    • instant

      public Instant instant()
      Specified by:
      instant in interface InstantSource
      Specified by:
      instant in class Clock
    • withZone

      public Clock withZone(ZoneId zone)
      Specified by:
      withZone in interface InstantSource
      Specified by:
      withZone in class Clock