StopWatch provides a convenient API for timings.
To start the watch, call start() or StopWatch#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)
| Modifier and Type | Class and Description |
|---|---|
| public static class | StopWatch.
Stores a split as a label and duration. |
| private static enum | StopWatch.
Enumeration type which indicates the split status of a StopWatch. |
| private static enum | StopWatch.
Enumeration type which indicates the status of a StopWatch. |
| Modifier and Type | Field and Description |
|---|---|
| private final String | message
A message for string presentation. |
| private static final long | |
| private StopWatch. | runningState
The current running state of this StopWatch. |
| private final List | splits
The split list. |
| private StopWatch. | splitState
Whether this StopWatch has a split time recorded. |
| private Instant | startInstant
The start Instant. |
| private long | startTimeNanos
The start time in nanoseconds. |
| private Instant | stopInstant
The end Instant. |
| private long | stopTimeNanos
The stop time in nanoseconds. |
| Access | Constructor and Description |
|---|---|
| public | |
| public |
| Modifier and Type | Method and Description |
|---|---|
| public static StopWatch | |
| public static StopWatch | |
| public String | Returns: the split time formatted byDurationFormatUtils#formatDurationHMS.Formats the split time with |
| public String | Returns: the time formatted byDurationFormatUtils#formatDurationHMS.Formats the time formatted with |
| public < the type of results supplied by this supplier. T> T | Returns: a result from the given Supplier.The supplier to supplier)Supplier#get().Delegates to |
| public Duration | |
| public String | |
| public long | |
| public Duration | |
| public long | |
| public List | |
| public long | Returns: the split time in milliseconds.
Deprecated
Use
getSplitDuration().
Gets the split time on this StopWatch. |
| public Instant | Returns: the Instant this StopWatch was started, between the current time and midnight, January 1, 1970 UTC.Gets the Instant this StopWatch was started, between the current time and midnight, January 1, 1970 UTC. |
| public long | Returns: the time this StopWatch was started in milliseconds, between the current time and midnight, January 1, 1970 UTC.
Deprecated
Use
getStartInstant().
Gets the time this StopWatch was started in milliseconds, between the current time and midnight, January 1, 1970 UTC. |
| public Instant | Returns: the Instant this StopWatch was stopped in milliseconds, between the current time and midnight, January 1, 1970 UTC.Gets the Instant this StopWatch was stopped, between the current time and midnight, January 1, 1970 UTC. |
| public long | Returns: the time this StopWatch was stopped in milliseconds, between the current time and midnight, January 1, 1970 UTC.
Deprecated
Use
getStopInstant().
Gets the time this StopWatch was stopped in milliseconds, between the current time and midnight, January 1, 1970 UTC. |
| public < the type of results supplied by this supplier. T, The kind of thrown exception or error. E extends Throwable> T | Returns: a result from the given Supplier.The supplier to supplier)Supplier#get().Delegates to |
| public long | |
| public long | |
| public boolean | |
| public boolean | |
| public boolean | Returns: boolean If this StopWatch is suspended.Tests whether this StopWatch is suspended. |
| private long | Returns: milliseconds conversion result.nanoseconds to convert. nanos)Converts nanoseconds to milliseconds. |
| public void | |
| public void | |
| public void | run(final Runnable
The runnable to runnable)Runnable#run().Delegates to |
| public < The kind of E extends Throwable> voidThrowable. | runT(final FailableRunnable<E>
The runnable to runnable)FailableRunnable#run().Delegates to |
| public void | |
| public void | |
| public void | |
| private void | |
| public void | |
| public void | |
| public String | Returns: the split time as a String.Gets a summary of the last split time that this StopWatch recorded as a string. |
| public String | Returns: the time as a String.Overrides java. Gets a summary of the time that this StopWatch recorded as a string. |
| public void |
| message | back to summary |
|---|---|
| private final String message A message for string presentation.
| |
| NANO_2_MILLIS | back to summary |
|---|---|
| private static final long NANO_2_MILLIS | |
| runningState | back to summary |
|---|---|
| private StopWatch. The current running state of this StopWatch. | |
| splits | back to summary |
|---|---|
| private final List<StopWatch. The split list. | |
| splitState | back to summary |
|---|---|
| private StopWatch. Whether this StopWatch has a split time recorded. | |
| startInstant | back to summary |
|---|---|
| private Instant startInstant The start Instant. nanoTime is only for elapsed time so we need to also store the currentTimeMillis to maintain the old getStartTime API. On Java 8, Instant has millisecond precision, later versions use nanoseconds. | |
| startTimeNanos | back to summary |
|---|---|
| private long startTimeNanos The start time in nanoseconds. This field can be removed once we move off of Java 8. | |
| stopInstant | back to summary |
|---|---|
| private Instant stopInstant The end Instant. nanoTime is only for elapsed time so we need to also store the currentTimeMillis to maintain the old getStartTime API. On Java 8, Instant has millisecond precision, later versions use nanoseconds. | |
| stopTimeNanos | back to summary |
|---|---|
| private long stopTimeNanos The stop time in nanoseconds. This field can be removed once we move off of Java 8. | |
| StopWatch | back to summary |
|---|---|
| public StopWatch() Constructs a new instance. | |
| StopWatch | back to summary |
|---|---|
| public StopWatch(final String message) Constructs a new instance.
| |
| create | back to summary |
|---|---|
| public static StopWatch create() Creates a StopWatch.
| |
| createStarted | back to summary |
|---|---|
| public static StopWatch createStarted() Creates and starts a StopWatch.
| |
| formatSplitTime | back to summary |
|---|---|
| public String formatSplitTime() Formats the split time with
| |
| formatTime | back to summary |
|---|---|
| public String formatTime() Formats the time formatted with
| |
| get | back to summary |
|---|---|
| public <T> T get(final Supplier<T> supplier) Delegates to
| |
| getDuration | back to summary |
|---|---|
| public Duration getDuration() Gets the Duration on this StopWatch. This is either the Duration between the start and the moment this method is called, or the Duration between start and stop.
| |
| getMessage | back to summary |
|---|---|
| public String getMessage() Gets the message for string presentation.
| |
| getNanoTime | back to summary |
|---|---|
| public long getNanoTime() Gets the elapsed time in nanoseconds. This is either the time between the start and the moment this method is called, or the amount of time between start and stop.
| |
| getSplitDuration | back to summary |
|---|---|
| public Duration getSplitDuration() Gets the split Duration on this StopWatch. This is the Duration between start and latest split.
| |
| getSplitNanoTime | back to summary |
|---|---|
| public long getSplitNanoTime() Gets the split time in nanoseconds. This is the time between start and latest split.
| |
| getSplits | back to summary |
|---|---|
| public List Gets the split list. | |
| getSplitTime | back to summary |
|---|---|
| public long getSplitTime()
Deprecated Use Gets the split time on this StopWatch. This is the time between start and latest split.
| |
| getStartInstant | back to summary |
|---|---|
| public Instant getStartInstant() Gets the Instant this StopWatch was started, between the current time and midnight, January 1, 1970 UTC.
| |
| getStartTime | back to summary |
|---|---|
| public long getStartTime()
Deprecated Use Gets the time this StopWatch was started in milliseconds, between the current time and midnight, January 1, 1970 UTC.
| |
| getStopInstant | back to summary |
|---|---|
| public Instant getStopInstant() Gets the Instant this StopWatch was stopped, between the current time and midnight, January 1, 1970 UTC.
| |
| getStopTime | back to summary |
|---|---|
| public long getStopTime()
Deprecated Use Gets the time this StopWatch was stopped in milliseconds, between the current time and midnight, January 1, 1970 UTC.
| |
| getT | back to summary |
|---|---|
| public <T, E extends Throwable> T getT(final FailableSupplier<T, E> supplier) throws Throwable Delegates to
| |
| getTime | back to summary |
|---|---|
| public long getTime() Gets the time on this StopWatch. This is either the time between the start and the moment this method is called, or the amount of time between start and stop.
| |
| getTime | back to summary |
|---|---|
| public long getTime(final TimeUnit timeUnit) Gets the time in the specified 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
| |
| isStarted | back to summary |
|---|---|
| public boolean isStarted() Tests whether this StopWatch is started. A suspended StopWatch is also started watch.
| |
| isStopped | back to summary |
|---|---|
| public boolean isStopped() Tests whether StopWatch is stopped. this StopWatch which's not yet started and explicitly stopped StopWatch is considered as stopped.
| |
| isSuspended | back to summary |
|---|---|
| public boolean isSuspended() Tests whether this StopWatch is suspended.
| |
| nanosToMillis | back to summary |
|---|---|
| private long nanosToMillis(final long nanos) Converts nanoseconds to milliseconds.
| |
| reset | back to summary |
|---|---|
| public void reset() Resets this StopWatch. Stops it if need be. This method clears the internal values to allow the object to be reused. | |
| resume | back to summary |
|---|---|
| public void resume() Resumes this StopWatch after a suspend. 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.
| |
| run | back to summary |
|---|---|
| public void run(final Runnable runnable) Delegates to
| |
| runT | back to summary |
|---|---|
| public <E extends Throwable> void runT(final FailableRunnable<E> runnable) throws Throwable Delegates to
| |
| split | back to summary |
|---|---|
| public void split() Splits the time.
This method sets the stop time of the watch to allow a time to be extracted. The start time is unaffected, enabling
| |
| split | back to summary |
|---|---|
| public void split(final String label) Splits the time with a label.
This method sets the stop time of the watch to allow a time to be extracted. The start time is unaffected, enabling
| |
| start | back to summary |
|---|---|
| public void start() Starts this StopWatch. This method starts a new timing session, clearing any previous values.
| |
| startResume | back to summary |
|---|---|
| private void startResume() Starts or resumes this StopWatch. | |
| stop | back to summary |
|---|---|
| public void stop() Stops this StopWatch. This method ends a new timing session, allowing the time to be retrieved.
| |
| suspend | back to summary |
|---|---|
| public void suspend() Suspends this StopWatch for later resumption. 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.
| |
| toSplitString | back to summary |
|---|---|
| public String toSplitString() Gets a summary of the last split time that this StopWatch recorded as a string. The format used is ISO 8601-like, [message ]hours:minutes:seconds.milliseconds.
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Gets a summary of the time that this StopWatch recorded as a string. The format used is ISO 8601-like, [message ]hours:minutes:seconds.milliseconds. | |
| unsplit | back to summary |
|---|---|
| public void unsplit() Removes the split. This method clears the stop time. The start time is unaffected, enabling timing from the original start point to continue.
| |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public Duration | |
| public String | |
| public String | Returns: this instance to a string.Overrides org. Converts this instance to a string. |
| Split | back to summary |
|---|---|
| public Split(String label, Duration duration) Constructs a Split object with label and duration. | |
| getDuration | back to summary |
|---|---|
| public Duration getDuration() Gets the duration of this split.
| |
| getLabel | back to summary |
|---|---|
| public String getLabel() Gets the label of this split.
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides org. Converts this instance to a string. | |
| Modifier and Type | Field and Description |
|---|---|
| public static final StopWatch. | |
| public static final StopWatch. |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static StopWatch. | |
| public static StopWatch. |
| SPLIT | back to summary |
|---|---|
| public static final StopWatch. | |
| UNSPLIT | back to summary |
|---|---|
| public static final StopWatch. | |
| SplitState | back to summary |
|---|---|
| private SplitState() | |
| valueOf | back to summary |
|---|---|
| public static StopWatch. | |
| values | back to summary |
|---|---|
| public static StopWatch. | |
| Modifier and Type | Field and Description |
|---|---|
| public static final StopWatch. | |
| public static final StopWatch. | |
| public static final StopWatch. | |
| public static final StopWatch. |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv abstract boolean | |
| pack-priv abstract boolean | |
| pack-priv abstract boolean | Returns: boolean If this StopWatch is suspended.Tests whether this StopWatch is suspended. |
| public static StopWatch. | |
| public static StopWatch. |
| RUNNING | back to summary |
|---|---|
| public static final StopWatch. | |
| STOPPED | back to summary |
|---|---|
| public static final StopWatch. | |
| SUSPENDED | back to summary |
|---|---|
| public static final StopWatch. | |
| UNSTARTED | back to summary |
|---|---|
| public static final StopWatch. | |
| State | back to summary |
|---|---|
| private State() | |
| isStarted | back to summary |
|---|---|
| pack-priv abstract boolean isStarted() Tests whether this StopWatch is started. A suspended StopWatch is also started.
| |
| isStopped | back to summary |
|---|---|
| pack-priv abstract boolean isStopped() Tests whether this StopWatch is stopped. A StopWatch which is not yet started and explicitly stopped is considered stopped.
| |
| isSuspended | back to summary |
|---|---|
| pack-priv abstract boolean isSuspended() Tests whether this StopWatch is suspended.
| |
| valueOf | back to summary |
|---|---|
| public static StopWatch. | |
| values | back to summary |
|---|---|
| public static StopWatch. | |