public class Stopwatch extends Object
Instant.now()
.
sinceStart()
can be used to retrieve the duration since the
stopwatch started.
checkpoint(String)
adds a new checkpoint. It might be used for split
or lap times. sinceLast()
can be used to retrieve the duration since
the last checkpoint was created.
Modifier and Type | Class and Description |
---|---|
static class |
Stopwatch.Checkpoint
Implementation of a stopwatches checkpoint.
|
Constructor and Description |
---|
Stopwatch() |
Modifier and Type | Method and Description |
---|---|
Stopwatch.Checkpoint |
checkpoint(String name)
Adds a new checkpoint referenced by
name . |
List<Stopwatch.Checkpoint> |
getCheckpoints()
List of checkpoints
|
Optional<Stopwatch.Checkpoint> |
getLastCheckpoint()
The last created
Stopwatch.Checkpoint or empty if no checkpoint was created |
Instant |
getLastInstant()
Instant of the last created
Stopwatch.Checkpoint or the stopwatches starting
time if no checkpoint was created |
Instant |
getStartInstant()
Instant at the stopwatches start
|
Duration |
sinceLast()
Duration since the last created
Stopwatch.Checkpoint or the stopwatches
starting time if no checkpoint was created |
Duration |
sinceStart()
Duration since the stopwatches start
|
Stream<Stopwatch.Checkpoint> |
stream()
Sequential
Stream with the checkpoints as its source |
String |
toString() |
boolean |
waitFor(Duration duration,
Duration timeoutSinceStart)
Waits for
duration using Thread#sleep(long) . |
boolean |
waitFor(Duration duration,
Duration timeoutSinceStart,
Consumer<Duration> wait)
Waits for
duration using wait . |
public Stopwatch.Checkpoint checkpoint(String name)
name
.
name
does not need to be unique. Multiple checkpoints with the same
name might exist.
name
- name to reference the checkpointpublic List<Stopwatch.Checkpoint> getCheckpoints()
public Optional<Stopwatch.Checkpoint> getLastCheckpoint()
Stopwatch.Checkpoint
or empty if no checkpoint was createdpublic Instant getLastInstant()
Stopwatch.Checkpoint
or the stopwatches starting
time if no checkpoint was createdpublic Duration sinceLast()
Stopwatch.Checkpoint
or the stopwatches
starting time if no checkpoint was createdpublic Duration sinceStart()
public boolean waitFor(Duration duration, Duration timeoutSinceStart) throws InterruptedException
duration
using Thread#sleep(long)
. If reached, it
times out at timeoutSinceStart
after the Stopwatch
start.duration
- duration to waittimeoutSinceStart
- timeout duration since the Stopwatch
starttrue
if the timeout was not reached and false
if the
timeout has been reachedInterruptedException
- if any thread has interrupted the current threadpublic boolean waitFor(Duration duration, Duration timeoutSinceStart, Consumer<Duration> wait)
duration
using wait
. If reached, it times out at
timeoutSinceStart
after the Stopwatch
start.
Note: The Duration
given to wait
might be less than
duration
to handle timeoutSinceStart
more precisely.
duration
- duration to waittimeoutSinceStart
- timeout duration since the Stopwatch
startwait
- method to use for waitingtrue
if the timeout was not reached and false
if the
timeout has been reachedpublic Stream<Stopwatch.Checkpoint> stream()
Stream
with the checkpoints as its sourceStream
over the checkpointspublic Instant getStartInstant()
Copyright © 2025. All rights reserved.