StopWatch
- public class org.apache.commons.lang3.time.StopWatch[source]
StopWatchprovides a convenient API for timings.To start the watch, call
#start()orStopWatch#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 twice#unsplit()may only be called if the watch has been#split()#resume()may only be called if the watch has been#suspend()
This class is not thread-safe.
- See also:
- Since:
2.0
Constructors
Constructs a new instance. |
|
Constructs a new instance. |
Methods
Creates a StopWatch. |
|
Creates and starts a StopWatch. |
|
Formats the split time with |
|
Formats the time formatted with |
|
Gets the result of |
|
Gets the Duration on this StopWatch. |
|
Gets the message for string presentation. |
|
Gets the elapsed time in nanoseconds. |
|
Gets the split Duration on this StopWatch. |
|
Gets the split time in nanoseconds. |
|
Deprecated. Gets the split time on this StopWatch. |
|
Gets the split list. |
|
Gets the Instant this StopWatch was started, between the current time and midnight, January 1, 1970 UTC. |
|
Deprecated. Gets the time this StopWatch was started 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. |
|
Deprecated. Gets the time this StopWatch was stopped in milliseconds, between the current time and midnight, January 1, 1970 UTC. |
|
Gets the result of |
|
Gets the time on this StopWatch. |
|
Gets the time in the specified TimeUnit. |
|
Tests whether this StopWatch is started. |
|
Tests whether StopWatch is stopped. |
|
Tests whether this StopWatch is suspended. |
|
Resets this StopWatch. |
|
Resumes this StopWatch after a suspend. |
|
Delegates to |
|
Delegates to |
|
Splits the time. |
|
Splits the time with a label. |
|
Starts this StopWatch. |
|
Stops this StopWatch. |
|
Suspends this StopWatch for later resumption. |
|
Gets a summary of the last split time that this StopWatch recorded as a string. |
|
Gets a summary of the time that this StopWatch recorded as a string. |
|
Removes the split. |
- public static StopWatch create()[source]
Creates a StopWatch.
- Returns:
StopWatch a StopWatch.
- Since:
3.10
- public static StopWatch createStarted()[source]
Creates and starts a StopWatch.
- Returns:
StopWatch a started StopWatch.
- Since:
3.5
- public String formatSplitTime()[source]
Formats the split time with
DurationFormatUtils#formatDurationHMS.- Returns:
The split time formatted by
DurationFormatUtils#formatDurationHMS.- Since:
3.10
- public String formatTime()[source]
Formats the time formatted with
DurationFormatUtils#formatDurationHMS.- Returns:
The time formatted by
DurationFormatUtils#formatDurationHMS.- Since:
3.10
- public <T> T get(Supplier<T> supplier)[source]
Gets the result of
Supplier#get()while recording the duration of the call.- Type parameters:
<T> – the type of results supplied by this supplier.
- Parameters:
supplier – The supplier to
Supplier#get().- Returns:
A result from the given Supplier.
- Since:
3.18.0
- public Duration getDuration()[source]
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.
- Returns:
The Duration.
- Since:
3.16.0
- public String getMessage()[source]
Gets the message for string presentation.
- Returns:
The message for string presentation.
- Since:
3.10
- public long getNanoTime()[source]
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.
- Returns:
The elapsed time in nanoseconds.
- See also:
- Since:
3.0
- public Duration getSplitDuration()[source]
Gets the split Duration on this StopWatch.
This is the Duration between start and latest split.
- Returns:
The split Duration.
- Throws:
java.lang.IllegalStateException – Thrown if this StopWatch has not yet been split.
- Since:
3.16.0
- public long getSplitNanoTime()[source]
Gets the split time in nanoseconds.
This is the time between start and latest split.
- Returns:
The split time in nanoseconds.
- Throws:
java.lang.IllegalStateException – Thrown if this StopWatch has not yet been split.
- Since:
3.0
- @Deprecated public long getSplitTime()[source]
Deprecated: Use
#getSplitDuration().Gets the split time on this StopWatch.
This is the time between start and latest split.
- Returns:
The split time in milliseconds.
- Throws:
java.lang.IllegalStateException – Thrown if this StopWatch has not yet been split.
- Since:
2.1
- public List<StopWatch.Split> getSplits()[source]
Gets the split list.
- Returns:
The list of splits.
- Since:
3.20.0
- public Instant getStartInstant()[source]
Gets the Instant this StopWatch was started, between the current time and midnight, January 1, 1970 UTC.
- Returns:
The Instant this StopWatch was started, between the current time and midnight, January 1, 1970 UTC.
- Throws:
java.lang.IllegalStateException – Thrown if this StopWatch has not been started.
- Since:
3.16.0
- @Deprecated public long getStartTime()[source]
Deprecated: Use
#getStartInstant().Gets the time this StopWatch was started in milliseconds, between the current time and midnight, January 1, 1970 UTC.
- Returns:
The time this StopWatch was started in milliseconds, between the current time and midnight, January 1, 1970 UTC.
- Throws:
java.lang.IllegalStateException – Thrown if this StopWatch has not been started.
- Since:
2.4
- public Instant getStopInstant()[source]
Gets the Instant this StopWatch was stopped, between the current time and midnight, January 1, 1970 UTC.
- Returns:
The Instant this StopWatch was stopped in milliseconds, between the current time and midnight, January 1, 1970 UTC.
- Throws:
java.lang.IllegalStateException – Thrown if this StopWatch has not been started.
- Since:
3.16.0
- @Deprecated public long getStopTime()[source]
Deprecated: Use
#getStopInstant().Gets the time this StopWatch was stopped in milliseconds, between the current time and midnight, January 1, 1970 UTC.
- Returns:
The time this StopWatch was stopped in milliseconds, between the current time and midnight, January 1, 1970 UTC.
- Throws:
java.lang.IllegalStateException – Thrown if this StopWatch has not been started.
- Since:
3.12.0
- public <T, E extends Throwable> T getT(FailableSupplier<T, E> supplier) throws Throwable[source]
Gets the result of
FailableSupplier#get()while recording the duration of the call.- Type parameters:
<T> – the type of results supplied by this supplier.
<E> – The kind of thrown exception or error.
- Parameters:
supplier – The supplier to
Supplier#get().- Returns:
A result from the given Supplier.
- Throws:
java.lang.Throwable – Thrown if the supplier fails.
- Since:
3.18.0
- public long getTime()[source]
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.
- Returns:
The time in milliseconds.
- See also:
- public long getTime(TimeUnit timeUnit)[source]
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
TimeUnit.HOURSand this StopWatch time is 59 minutes, then the result returned will be0.- Parameters:
timeUnit – The unit of time, not null.
- Returns:
The time in the specified TimeUnit, rounded down.
- Since:
3.5
- public boolean isStarted()[source]
Tests whether this StopWatch is started. A suspended StopWatch is also started watch.
- Returns:
boolean If this StopWatch is started.
- Since:
3.2
- public boolean isStopped()[source]
Tests whether StopWatch is stopped. this StopWatch which’s not yet started and explicitly stopped StopWatch is considered as stopped.
- Returns:
boolean If this StopWatch is stopped.
- Since:
3.2
- public boolean isSuspended()[source]
Tests whether this StopWatch is suspended.
- Returns:
boolean If this StopWatch is suspended.
- Since:
3.2
- public void reset()[source]
Resets this StopWatch. Stops it if need be.
This method clears the internal values to allow the object to be reused.
- public void resume()[source]
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.
- Throws:
java.lang.IllegalStateException – Thrown if this StopWatch has not been suspended.
- public void run(Runnable runnable)[source]
Delegates to
Runnable#run()while recording the duration of the call.- Parameters:
runnable – The runnable to
Runnable#run().- Since:
3.18.0
- public <E extends Throwable> void runT(FailableRunnable<E> runnable) throws Throwable[source]
Delegates to
FailableRunnable#run()while recording the duration of the call.- Type parameters:
<E> – The kind of
Throwable.- Parameters:
runnable – The runnable to
FailableRunnable#run().- Throws:
java.lang.Throwable – Thrown by
FailableRunnable#run().- Since:
3.18.0
- public void split()[source]
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
#unsplit()to continue the timing from the original start point.- Throws:
java.lang.IllegalStateException – Thrown if this StopWatch is not running.
- public void split(String label)[source]
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
#unsplit()to continue the timing from the original start point.- Parameters:
label – A message for string presentation.
- Throws:
java.lang.IllegalStateException – Thrown if the StopWatch is not running.
- Since:
3.20.0
- public void start()[source]
Starts this StopWatch.
This method starts a new timing session, clearing any previous values.
- Throws:
java.lang.IllegalStateException – Thrown if this StopWatch is already running.
- public void stop()[source]
Stops this StopWatch.
This method ends a new timing session, allowing the time to be retrieved.
- Throws:
java.lang.IllegalStateException – Thrown if this StopWatch is not running.
- public void suspend()[source]
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.
- Throws:
java.lang.IllegalStateException – Thrown if this StopWatch is not currently running.
- public String toSplitString()[source]
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.
- Returns:
The split time as a String.
- Since:
3.10 Returns the prefix
"message "if the message is set.