public class StopWatch extends Object
StopWatch provides a convenient API for timings.
To start the watch, call start() or createStarted(). At this point you can:
split() the watch to get the time whilst the watch continues in the background. unsplit() will remove the effect of the split. At this
point, these three options are available again.suspend() the watch to pause it. resume() allows the watch to continue. Any time between the suspend and resume will not be counted in
the total. At this point, these three options are available again.stop() the watch to complete the timing session.
It is intended that the output methods toString() and getTime() should only be called after stop, split or suspend, however a suitable
result will be returned at other points.
NOTE: As from v2.1, the methods protect against inappropriate calls. Thus you cannot now call stop before start, resume before suspend or unsplit before split.
split(), suspend(), or stop() cannot be invoked twiceunsplit() may only be called if the watch has been split()resume() may only be called if the watch has been suspend()start() cannot be called twice without calling reset()This class is not thread-safe
DurationUtils.of(FailableRunnable),
DurationUtils.of(FailableConsumer)| Constructor and Description |
|---|
StopWatch()
Constructs a new instance.
|
StopWatch(String message)
Constructs a new instance.
|
| Modifier and Type | Method and Description |
|---|---|
static StopWatch |
create()
Creates a StopWatch.
|
static StopWatch |
createStarted()
Creates and starts a StopWatch.
|
String |
formatSplitTime()
Formats the split time with
DurationFormatUtils.formatDurationHMS(long). |
String |
formatTime()
Formats the time formatted with
DurationFormatUtils.formatDurationHMS(long). |
<T> T |
get(Supplier<T> supplier)
Delegates to
Supplier.get() while recording the duration of the call. |
Duration |
getDuration()
Gets the Duration on this StopWatch.
|
String |
getMessage()
Gets the message for string presentation.
|
long |
getNanoTime()
Gets the elapsed time in nanoseconds.
|
Duration |
getSplitDuration()
Gets the split Duration on this StopWatch.
|
long |
getSplitNanoTime()
Gets the split time in nanoseconds.
|
long |
getSplitTime()
Deprecated.
Use
getSplitDuration(). |
Instant |
getStartInstant()
Gets the Instant this StopWatch was started, between the current time and midnight, January 1, 1970 UTC.
|
long |
getStartTime()
Deprecated.
Use
getStartInstant(). |
Instant |
getStopInstant()
Gets the Instant this StopWatch was stopped, between the current time and midnight, January 1, 1970 UTC.
|
long |
getStopTime()
Deprecated.
Use
getStopInstant(). |
<T,E extends Throwable> |
getT(FailableSupplier<T,E> supplier)
Delegates to
FailableSupplier.get() while recording the duration of the call. |
long |
getTime()
Gets the time on this StopWatch.
|
long |
getTime(TimeUnit timeUnit)
Gets the time in the specified TimeUnit.
|
boolean |
isStarted()
Tests whether this StopWatch is started.
|
boolean |
isStopped()
Tests whether StopWatch is stopped.
|
boolean |
isSuspended()
Tests whether this StopWatch is suspended.
|
void |
reset()
Resets this StopWatch.
|
void |
resume()
Resumes this StopWatch after a suspend.
|
void |
run(Runnable runnable)
Delegates to
Runnable.run() while recording the duration of the call. |
<E extends Throwable> |
runT(FailableRunnable<E> runnable)
Delegates to
FailableRunnable.run() while recording the duration of the call. |
void |
split()
Splits the time.
|
void |
start()
Starts this StopWatch.
|
void |
stop()
Stops this StopWatch.
|
void |
suspend()
Suspends this StopWatch for later resumption.
|
String |
toSplitString()
Gets a summary of the split time that this StopWatch recorded as a string.
|
String |
toString()
Gets a summary of the time that this StopWatch recorded as a string.
|
void |
unsplit()
Removes the split.
|
public StopWatch()
public StopWatch(String message)
message - A message for string presentation.public static StopWatch create()
public static StopWatch createStarted()
public String formatSplitTime()
DurationFormatUtils.formatDurationHMS(long).DurationFormatUtils.formatDurationHMS(long).public String formatTime()
DurationFormatUtils.formatDurationHMS(long).DurationFormatUtils.formatDurationHMS(long).public <T> T get(Supplier<T> supplier)
Supplier.get() while recording the duration of the call.T - the type of results supplied by this supplier.supplier - The supplier to Supplier.get().public Duration getDuration()
This is either the Duration between the start and the moment this method is called, or the Duration between start and stop.
public String getMessage()
public long getNanoTime()
This is either the time between the start and the moment this method is called, or the amount of time between start and stop.
System.nanoTime()public Duration getSplitDuration()
This is the Duration between start and latest split.
IllegalStateException - if this StopWatch has not yet been split.public long getSplitNanoTime()
This is the time between start and latest split.
IllegalStateException - if this StopWatch has not yet been split.@Deprecated public long getSplitTime()
getSplitDuration().This is the time between start and latest split.
IllegalStateException - if this StopWatch has not yet been split.public Instant getStartInstant()
IllegalStateException - if this StopWatch has not been started@Deprecated public long getStartTime()
getStartInstant().IllegalStateException - if this StopWatch has not been startedpublic Instant getStopInstant()
IllegalStateException - if this StopWatch has not been started@Deprecated public long getStopTime()
getStopInstant().IllegalStateException - if this StopWatch has not been startedpublic <T,E extends Throwable> T getT(FailableSupplier<T,E> supplier) throws Throwable
FailableSupplier.get() while recording the duration of the call.T - the type of results supplied by this supplier.E - The kind of thrown exception or error.supplier - The supplier to Supplier.get().Throwable - if the supplier fails.public long getTime()
This is either the time between the start and the moment this method is called, or the amount of time between start and stop.
getDuration()public long getTime(TimeUnit timeUnit)
This is either the time between the start and the moment this method is called, or the amount of time between start and stop. The resulting time will be
expressed in the desired TimeUnit with any remainder rounded down. For example, if the specified unit is TimeUnit.HOURS and this StopWatch time
is 59 minutes, then the result returned will be 0.
timeUnit - the unit of time, not nullpublic boolean isStarted()
public boolean isStopped()
public boolean isSuspended()
public void reset()
This method clears the internal values to allow the object to be reused.
public void resume()
This method resumes the watch after it was suspended. The watch will not include time between the suspend and resume calls in the total time.
IllegalStateException - if this StopWatch has not been suspended.public void run(Runnable runnable)
Runnable.run() while recording the duration of the call.runnable - The runnable to Runnable.run().public <E extends Throwable> void runT(FailableRunnable<E> runnable) throws Throwable
FailableRunnable.run() while recording the duration of the call.E - The kind of Throwable.runnable - The runnable to FailableRunnable.run().Throwable - Thrown by FailableRunnable.run().public void split()
This method sets the stop time of the watch to allow a time to be extracted. The start time is unaffected, enabling unsplit() to continue the
timing from the original start point.
IllegalStateException - if this StopWatch is not running.public void start()
This method starts a new timing session, clearing any previous values.
IllegalStateException - if this StopWatch is already running.public void stop()
This method ends a new timing session, allowing the time to be retrieved.
IllegalStateException - if this StopWatch is not running.public void suspend()
This method suspends the watch until it is resumed. The watch will not include time between the suspend and resume calls in the total time.
IllegalStateException - if this StopWatch is not currently running.public String toSplitString()
The format used is ISO 8601-like, [message ]hours:minutes:seconds.milliseconds.
"message " if the message is set.public String toString()
The format used is ISO 8601-like, [message ]hours:minutes:seconds.milliseconds.
public void unsplit()
This method clears the stop time. The start time is unaffected, enabling timing from the original start point to continue.
IllegalStateException - if this StopWatch has not been split.Copyright © 2001–2025 The Apache Software Foundation. All rights reserved.