Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion core/src/main/java/com/uber/m3/tally/Stopwatch.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,16 @@
* relies on values being recorded as nanosecond-level timestamps. There is no
* assumption that {@code startNanos} is related to the current time, but successive recordings
* of the stopwatch are comparable with one another.
* <p>
* This class implements {@link AutoCloseable}, so it can be used with try-with-resources to
* automatically call {@link #stop()} when the try block exits.
* <pre>
* try (Stopwatch stopwatch = timer.start()) {
* // Do something
* } // stopwatch.stop() is automatically called here
* </pre>
*/
public class Stopwatch {
public class Stopwatch implements AutoCloseable {
private final long startNanos;
private final StopwatchRecorder recorder;

Expand Down Expand Up @@ -56,4 +64,13 @@ public void stop() {
public void Stop() {
stop();
}

/**
* Closes this stopwatch by calling {@link #stop()}.
* This method is automatically called when used in a try-with-resources block.
*/
@Override
public void close() {
stop();
}
}