Package org.agrona.concurrent
Class OffsetEpochNanoClock
java.lang.Object
org.agrona.concurrent.OffsetEpochNanoClock
- All Implemented Interfaces:
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static final class
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final int
private static final long
private static final long
private final int
private final long
private final long
private OffsetEpochNanoClock.TimeFields
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs the clock with default configuration.OffsetEpochNanoClock
(int maxMeasurementRetries, long measurementThresholdNs, long resampleIntervalNs) Constructs the clock with custom configuration parameters. -
Method Summary
-
Field Details
-
DEFAULT_MAX_MEASUREMENT_RETRIES
private static final int DEFAULT_MAX_MEASUREMENT_RETRIES- See Also:
-
DEFAULT_MEASUREMENT_THRESHOLD_NS
private static final long DEFAULT_MEASUREMENT_THRESHOLD_NS- See Also:
-
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 -
timeFields
-
-
Constructor Details
-
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 Details
-
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 interfaceEpochNanoClock
- 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.
-