@FunctionalInterface public interface TimeProvider
This interface specifies methods to retrieve the current time with varying degrees of precision,
namely in milliseconds, microseconds, and nanoseconds. Implementations of this interface are expected
to provide time values with the highest accuracy and precision feasible. Key implementations include
PosixTimeProvider and SystemTimeProvider. The PosixTimeProvider is often
preferred for its enhanced speed, accuracy, and stability, though it relies on native code and thus
may have platform-specific dependencies.
This interface is crucial in contexts where precise time measurements are vital, such as in performance monitoring, timestamping events, or handling time-sensitive operations.
PosixTimeProvider,
SystemTimeProvider| Modifier and Type | Method and Description |
|---|---|
default long |
currentTimeMicros()
Retrieves the current time in microseconds.
|
long |
currentTimeMillis()
Retrieves the current time in milliseconds.
|
default long |
currentTimeNanos()
Retrieves the current time in nanoseconds.
|
long currentTimeMillis()
This method returns the current time with millisecond precision, measured from the Unix epoch (00:00:00 UTC on 1 January 1970). It is expected to be implemented by all subclasses, providing the baseline precision for time measurements.
default long currentTimeMicros()
throws IllegalStateException
This default implementation offers microsecond precision by scaling the millisecond value from
currentTimeMillis() by a factor of 1000. Implementations may override this for higher
accuracy if available.
IllegalStateException - if the time value cannot be accurately determined or converted.default long currentTimeNanos()
throws IllegalStateException
This default method provides nanosecond precision by further scaling the microsecond value
from currentTimeMicros() by 1000. Implementations may provide more precise or direct
measurements if their underlying system supports it.
IllegalStateException - if the time value cannot be accurately determined or converted.Copyright © 2024. All rights reserved.