Package com.gengoai
Class MultithreadedStopwatch
- java.lang.Object
-
- com.gengoai.MultithreadedStopwatch
-
- All Implemented Interfaces:
Serializable
public class MultithreadedStopwatch extends Object implements Serializable
A specialized version of a Stopwatch that is thread safe. Tracks start and ending times per thread to determine total time taken across threads.
Normal usage of a Stopwatch is a follows:
var sw = new MultithreadedStopwatch("MyStopwatch"); //Perform some activity new Thread(() -> { sw.start(); // do something.... sw.stop(); }).start(); new Thread(() -> { sw.start(); // do something.... sw.stop(); }).start(); new Thread(() -> { sw.start(); // do something.... sw.stop(); }).start(); //Wait until all threads are complete System.out.println(sw); // Output the sum of times spent in the three threads and the average time spent per start/stop.
- Author:
- David B. Bracewell
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description MultithreadedStopwatch(String name)
Instantiates a new Multithreaded stopwatch with the given name.MultithreadedStopwatch(String name, @NonNull Level level)
Instantiates a new Multithreaded stopwatch with given name and logging at given level.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description long
elapsed(TemporalUnit timeUnit)
Gets the elapsed time in given time unitsDuration
getElapsedTime()
Gets elapsed time in nano secondsString
getElapsedTimeAsString()
Gets elapsed time as string.void
reset()
Reset the stopwatch.void
start()
Start the stopwatch.void
stop()
Stop the stopwatch.String
toString()
-
-
-
Constructor Detail
-
MultithreadedStopwatch
public MultithreadedStopwatch(String name)
Instantiates a new Multithreaded stopwatch with the given name.- Parameters:
name
- the name of the stopwatch
-
MultithreadedStopwatch
public MultithreadedStopwatch(String name, @NonNull @NonNull Level level)
Instantiates a new Multithreaded stopwatch with given name and logging at given level. The stopwatch will log the current stopwatch status at the given log level on a fixed interval of every 30 seconds.- Parameters:
name
- the name of the stopwatchlevel
- the log level
-
-
Method Detail
-
elapsed
public long elapsed(TemporalUnit timeUnit)
Gets the elapsed time in given time units- Parameters:
timeUnit
- the time unit- Returns:
- the elapsed time in the given time unit
-
getElapsedTime
public Duration getElapsedTime()
Gets elapsed time in nano seconds- Returns:
- the elapsed time in nano seconds
-
getElapsedTimeAsString
public String getElapsedTimeAsString()
Gets elapsed time as string.- Returns:
- the elapsed time as string
-
reset
public void reset()
Reset the stopwatch.
-
start
public void start()
Start the stopwatch.
-
stop
public void stop()
Stop the stopwatch.
-
-