Package io.debezium.util
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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classStopwatch.BaseDurationsAbstract base class forStopwatch.Durationsimplementations.static interfaceStopwatch.DurationsThe average and total durations as measured by one or more stopwatches.private static classStopwatch.MultipleDurationsAStopwatch.Durationsimplementation that accumulates alladdeddurations.private static classStopwatch.SingleDurationAStopwatch.Durationsimplementation that only remembers the most recentlyaddedduration.static interfaceStopwatch.StatisticsThe timing statistics for a recorded set of samples.static interfaceStopwatch.StopwatchSetA set of stopwatches whose durations are combined.
-
Constructor Summary
Constructors Constructor Description Stopwatch()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description static Stopwatchaccumulating()Create a newStopwatchthat records all of the measured durations of the stopwatch.protected static StringasString(Duration duration)Compute the readable string representation of the supplied duration.private static Stopwatch.StatisticscreateStatistics(LongSummaryStatistics stats)protected static StopwatchcreateWith(Stopwatch.BaseDurations duration, Runnable uponStart, Runnable uponStop)Create a new stopwatch that updates the givenduration, and optionally has functions to be called after the stopwatch is started and stopped.abstract Stopwatch.Durationsdurations()Get the total and average durations measured by this stopwatch.static Stopwatch.StopwatchSetmultiple()Create a new set of stopwatches.static Stopwatchreusable()Create a newStopwatchthat can be reused.abstract Stopwatchstart()Start the stopwatch.abstract Stopwatchstop()Stop the stopwatch.
-
-
-
Method Detail
-
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()
-
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:
start()
-
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 newStopwatchthat can be reused. The resultingdurations(), 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 newStopwatchthat 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 eachStopwatchcreated byStopwatch.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 givenduration, 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 nulluponStart- the function that should be called when the stopwatch is successfully started (after not running); may be nulluponStop- the function that should be called when the stopwatch is successfully stopped (after it was running); may be null- Returns:
- the new stopwatch
-
-