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 class
Stopwatch.BaseDurations
Abstract base class forStopwatch.Durations
implementations.static interface
Stopwatch.Durations
The average and total durations as measured by one or more stopwatches.private static class
Stopwatch.MultipleDurations
AStopwatch.Durations
implementation that accumulates alladded
durations.private static class
Stopwatch.SingleDuration
AStopwatch.Durations
implementation that only remembers the most recentlyadded
duration.static interface
Stopwatch.Statistics
The timing statistics for a recorded set of samples.static interface
Stopwatch.StopwatchSet
A 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 Stopwatch
accumulating()
Create a newStopwatch
that records all of the measured durations of the stopwatch.protected static String
asString(Duration duration)
Compute the readable string representation of the supplied duration.private static Stopwatch.Statistics
createStatistics(LongSummaryStatistics stats)
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.abstract Stopwatch.Durations
durations()
Get the total and average durations measured by this stopwatch.static Stopwatch.StopwatchSet
multiple()
Create a new set of stopwatches.static Stopwatch
reusable()
Create a newStopwatch
that can be reused.abstract Stopwatch
start()
Start the stopwatch.abstract Stopwatch
stop()
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 newStopwatch
that 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 newStopwatch
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 eachStopwatch
created 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
-
-