Interface MonotonicClock

All Superinterfaces:
AutoCloseable

public interface MonotonicClock extends AutoCloseable
Provides a clock providing the current time in milliseconds, microseconds and instant since 1970-01-01-00:00:00.000. The returned timestamp values increase monotonically.

The functions provide nanosecond/microsecond/millisecond precision, but not necessarily the same resolution (how frequently the value changes) - no guarantees are made.

Implementation may adjust to wall clocks advancing faster than the real time. If and how exactly depends on the implementation, as long as none of the time values available via this interface "goes backwards".

Implementer notes: System.nanoTime() does not guarantee that the values will be monotonically increasing when invocations happen from different CPUs/cores/threads.

A default implementation of MonotonicClock can be injected as an application scoped bean in CDI.

  • Method Summary

    Modifier and Type
    Method
    Description
    void
     
    Current instant with nanosecond precision, can be used as a monotonically increasing wall clock.
    long
    Current timestamp as microseconds since epoch, can be used as a monotonically increasing wall clock.
    long
    Current timestamp as milliseconds since epoch, can be used as a monotonically increasing wall clock.
    long
    Monotonically increasing timestamp with nanosecond precision, not related to wall clock.
    void
    sleepMillis(long millis)
     
    void
     
  • Method Details

    • currentTimeMicros

      long currentTimeMicros()
      Current timestamp as microseconds since epoch, can be used as a monotonically increasing wall clock.
    • currentTimeMillis

      long currentTimeMillis()
      Current timestamp as milliseconds since epoch, can be used as a monotonically increasing wall clock.
    • currentInstant

      Instant currentInstant()
      Current instant with nanosecond precision, can be used as a monotonically increasing wall clock.
    • nanoTime

      long nanoTime()
      Monotonically increasing timestamp with nanosecond precision, not related to wall clock.
    • sleepMillis

      void sleepMillis(long millis)
    • close

      void close()
      Specified by:
      close in interface AutoCloseable
    • waitUntilTimeMillisAdvanced

      void waitUntilTimeMillisAdvanced()