Class OffsetEpochNanoClock

  • All Implemented Interfaces:
    EpochNanoClock

    public class OffsetEpochNanoClock
    extends java.lang.Object
    implements EpochNanoClock
    An accurate, zero-gc, pure-java, EpochNanoClock that calculates an initial epoch nano time based on System.currentTimeMillis() and then uses that offset to adjust the return value of System.nanoTime() to the UNIX epoch.

    The sample() method can be used in order to reset these initial values if your system clock gets updated.

    This class can be used from multiple threads safely.

    See Also:
    SystemEpochNanoClock
    • Field Detail

      • DEFAULT_MAX_MEASUREMENT_RETRIES

        private static final int DEFAULT_MAX_MEASUREMENT_RETRIES
        See Also:
        Constant Field Values
      • DEFAULT_MEASUREMENT_THRESHOLD_NS

        private static final long DEFAULT_MEASUREMENT_THRESHOLD_NS
        See Also:
        Constant Field Values
      • DEFAULT_RESAMPLE_INTERVAL_NS

        private static final long DEFAULT_RESAMPLE_INTERVAL_NS
      • maxMeasurementRetries

        private final int maxMeasurementRetries
      • measurementThresholdNs

        private final long measurementThresholdNs
      • resampleIntervalNs

        private final long resampleIntervalNs
    • Constructor Detail

      • OffsetEpochNanoClock

        public OffsetEpochNanoClock()
        Constructs the clock with default configuration.
      • OffsetEpochNanoClock

        public OffsetEpochNanoClock​(int maxMeasurementRetries,
                                    long measurementThresholdNs,
                                    long resampleIntervalNs)
        Constructs the clock with custom configuration parameters.
        Parameters:
        maxMeasurementRetries - the maximum number of times that this clock will attempt to re-sample the initial time values.
        measurementThresholdNs - the desired accuracy window for the initial clock samples.
        resampleIntervalNs - the desired interval before the samples are automatically recalculated. The seed recalculation enables the system to minimise clock drift if the system clock is updated.
    • Method Detail

      • sample

        public void sample()
        Explicitly resample the initial seeds.
      • nanoTime

        public long nanoTime()
        Time in nanoseconds since 1 Jan 1970 UTC.
        Specified by:
        nanoTime in interface EpochNanoClock
        Returns:
        the number of nanoseconds since 1 Jan 1970 UTC.
      • isWithinThreshold

        public boolean isWithinThreshold()
        Gets whether the clock sampled the initial time offset accurately.
        Returns:
        true if the clock sampled the initial time offset accurately.