Class Stopwatch


  • public final class Stopwatch
    extends Object
    A stop watch based either on a Supplier of nanoseconds, or a Clock.

    The accuracy of measurements depends on the precision of the time source, which likely depends on platform and configuration.

    Inspired by Guava's.

    • Method Detail

      • createStarted

        public static Stopwatch createStarted​(Supplier<Long> ticker)
        Returns:
        a running stop watch, using the supplied supplier.
      • createStarted

        public static Stopwatch createStarted​(Clock clock)
        Returns:
        a running stop watch, using the supplied clock.

        Note that only Clock.millis() will be used, thus the watch will have ms precision at most.

      • createUnstarted

        public static Stopwatch createUnstarted​(Supplier<Long> ticker)
        Returns:
        a non-running stop watch, using the supplied supplier.
      • start

        public Stopwatch start()
        Starts the stop watch, will fail when running.
        Returns:
        the stop watch
      • stop

        public Stopwatch stop()
        Stops the stop watch, will fail when not running.
        Returns:
        the stop watch
      • reset

        public Stopwatch reset()
        Resets the stop watch, and puts it into stopped state.
        Returns:
        the stop watch
      • isRunning

        public boolean isRunning()
        Returns:
        whether the stop watch is running
      • elapsed

        public long elapsed​(TimeUnit timeunit)
        Gets elapsed time using the supplied TimeUnit.
        Parameters:
        timeunit - time unit
        Returns:
        elapsed time in the specified unit
      • elapsed

        public Duration elapsed()
        Gets elapsed time as Duration.
        Returns:
        elapsed time