Package org.agrona.concurrent
Class OffsetEpochNanoClock
- java.lang.Object
-
- org.agrona.concurrent.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 onSystem.currentTimeMillis()
and then uses that offset to adjust the return value ofSystem.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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
OffsetEpochNanoClock.TimeFields
-
Field Summary
Fields Modifier and Type Field Description private static int
DEFAULT_MAX_MEASUREMENT_RETRIES
private static long
DEFAULT_MEASUREMENT_THRESHOLD_NS
private static long
DEFAULT_RESAMPLE_INTERVAL_NS
private int
maxMeasurementRetries
private long
measurementThresholdNs
private long
resampleIntervalNs
private OffsetEpochNanoClock.TimeFields
timeFields
-
Constructor Summary
Constructors Constructor Description OffsetEpochNanoClock()
Constructs the clock with default configuration.OffsetEpochNanoClock(int maxMeasurementRetries, long measurementThresholdNs, long resampleIntervalNs)
Constructs the clock with custom configuration parameters.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
isWithinThreshold()
Gets whether the clock sampled the initial time offset accurately.long
nanoTime()
Time in nanoseconds since 1 Jan 1970 UTC.void
sample()
Explicitly resample the initial seeds.
-
-
-
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
-
timeFields
private volatile OffsetEpochNanoClock.TimeFields timeFields
-
-
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 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.
-
-