public class MonitoredRunnable extends Object implements Runnable
A Runnable wrapper that maintains one or more counters for monitoring the time
spent by the Runnable's run() method.
Specify a target Runnable via constructor, then execute the run()
method available in this wrapper. The target Runnable's run() method
will be executed and monitored. After the operation, call
printStatistics(System.out) to print the statistics to the system console or
getCounter(Counter.Type), then elapsedTime() to retrieve the elapsed
time for a particular counter. E.g.:
Counter cpuTime = monitoredRunnable.getCounter(Counter.Type.CPU_TIME); double elapsedTimeMillis = cpuTime.elapsedTime(TimeUnit.MILLISECONDS);
By default, all available counter types will be measured, if no specific counter types are passed to the constructor. If required, an additional constructor may be used to set up one or more specific counters to be maintained. E.g.:
new MonitoredRunnable(runnable); // maintains all available counter types new MonitoredRunnable(runnable, Counter.Type.WALL_CLOCK_TIME); // wall-clock time only
For a list of available counters, refer to Counter.Type.
Note: This class is not thread-safe. In a multi-thread context, different instances must be created for each thread.
Counter,
Counter.Type| Constructor and Description |
|---|
MonitoredRunnable(Runnable targetRunnable)
Builds this monitored operation with a given
Runnable. |
MonitoredRunnable(Runnable targetRunnable,
Counter.Type... types)
Builds this monitored operation with a given
Runnable and one or more specific
counter types to be maintained. |
| Modifier and Type | Method and Description |
|---|---|
Counter |
getCounter(Counter.Type type)
Returns the counter instance associated with a given type in this monitored operation.
|
Collection<Counter> |
getCounters()
Returns the counters maintained by this monitored operation.
|
void |
printStatistics(PrintStream printStream)
Prints operation statistics in the specified print stream.
|
void |
printStatistics(PrintStream printStream,
TimeUnit timeUnit)
Prints operation statistics in the specified print stream, with a custom time unit.
|
void |
run()
See
Runnable.run(). |
public MonitoredRunnable(Runnable targetRunnable)
Runnable. All available counter
types will be maintained.targetRunnable - the Runnable to be executed and profiledpublic MonitoredRunnable(Runnable targetRunnable, Counter.Type... types)
Runnable and one or more specific
counter types to be maintained.targetRunnable - the Runnable to be executed and profiledtypes - the counter types to be maintained with the operationpublic void run()
Runnable.run().public Collection<Counter> getCounters()
public Counter getCounter(Counter.Type type)
type - the counter type to be fetchedpublic void printStatistics(PrintStream printStream)
printStream - the print stream to which statistics will be sentpublic void printStatistics(PrintStream printStream, TimeUnit timeUnit)
printStream - the print stream to which statistics will be senttimeUnit - the time unit for the elapsed times to be displayedCopyright © 2020. All rights reserved.