Class Stopwatch

java.lang.Object
io.debezium.util.Stopwatch

@ThreadSafe public abstract class Stopwatch extends Object
A stopwatch for measuring durations. All NewStopwatch implementations are threadsafe, although using a single stopwatch object across threads requires caution and care.
Author:
Randall Hauch
  • Constructor Details

    • Stopwatch

      public Stopwatch()
  • Method Details

    • start

      public abstract Stopwatch start()
      Start the stopwatch. Calling this method on an already-started stopwatch has no effect.
      Returns:
      this object to enable chaining methods
      See Also:
    • stop

      public abstract Stopwatch stop()
      Stop the stopwatch. Calling this method on an already-stopped stopwatch has no effect.
      Returns:
      this object to enable chaining methods
      See Also:
    • durations

      public abstract Stopwatch.Durations durations()
      Get the total and average durations measured by this stopwatch.
      Returns:
      the durations; never null
    • createStatistics

      private static Stopwatch.Statistics createStatistics(LongSummaryStatistics stats)
    • reusable

      public static Stopwatch reusable()
      Create a new Stopwatch that can be reused. The resulting durations(), however, only reflect the most recently completed stopwatch interval.

      For example, the following code shows this behavior:

       Stopwatch sw = Stopwatch.reusable();
       sw.start();
       sleep(3000); // sleep 3 seconds
       sw.stop();
       print(sw.durations()); // total and average duration are each 3 seconds
      
       sw.start();
       sleep(2000); // sleep 2 seconds
       sw.stop();
       print(sw.durations()); // total and average duration are each 2 seconds
       
      Returns:
      the new stopwatch; never null
    • accumulating

      public static Stopwatch accumulating()
      Create a new Stopwatch that records all of the measured durations of the stopwatch.

      For example, the following code shows this behavior:

       Stopwatch sw = Stopwatch.accumulating();
       sw.start();
       sleep(3000); // sleep 3 seconds
       sw.stop();
       print(sw.durations()); // total and average duration are each 3 seconds
      
       sw.start();
       sleep(2000); // sleep 2 seconds
       sw.stop();
       print(sw.durations()); // total duration is now 5 seconds, average is 2.5 seconds
       
      Returns:
      the new stopwatch; never null
    • multiple

      public static Stopwatch.StopwatchSet multiple()
      Create a new set of stopwatches. The resulting object is threadsafe, and each Stopwatch created by Stopwatch.StopwatchSet.create() is also threadsafe.
      Returns:
      the stopwatches set; never null
    • createWith

      protected static Stopwatch createWith(Stopwatch.BaseDurations duration, Runnable uponStart, Runnable uponStop)
      Create a new stopwatch that updates the given duration, and optionally has functions to be called after the stopwatch is started and stopped.

      The resulting stopwatch is threadsafe.

      Parameters:
      duration - the duration that should be updated; may not be null
      uponStart - the function that should be called when the stopwatch is successfully started (after not running); may be null
      uponStop - the function that should be called when the stopwatch is successfully stopped (after it was running); may be null
      Returns:
      the new stopwatch
    • asString

      protected static String asString(Duration duration)
      Compute the readable string representation of the supplied duration.
      Parameters:
      duration - the duration; may not be null
      Returns:
      the string representation; never null