V - the result type of method callpublic class MonitoredCallable<V> extends Object implements Callable<V>
A Callable wrapper that maintains one or more counters for monitoring the time
spent by the Callable's call() method.
Specify a target Callable via constructor, then execute the call()
method available in this wrapper. The target Callable's call() method
will be executed and monitored. After the operation, call
printStatistics(System.out) to print the statistics to the system console or
elapsedTime(Counter.Type), to retrieve the elapsed time duration for a
particular counter. E.g.:
double cpuTimeNanos = monitoredRunnable.elapsedTime(Counter.Type.CPU_TIME, TimeUnit.NANOSECONDS);
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 MonitoredCallable(callable); // maintains all available counter types new MonitoredCallable(callable, 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 |
|---|
MonitoredCallable(Callable<V> targetCallable)
Builds this monitored operation with a given
Callable. |
MonitoredCallable(Callable<V> targetCallable,
Counter.Type... types)
Builds this monitored operation with a given
Callable and one or more specific
counter types to be maintained. |
| Modifier and Type | Method and Description |
|---|---|
V |
call()
See
Callable.call(). |
Duration |
elapsedTime(Counter.Type type)
A convenient method that returns the elapsed time of a specific counter.
|
double |
elapsedTime(Counter.Type type,
TimeUnit timeUnit)
A convenient method that returns the elapsed time of a specific counter, in the
specified time unit.
|
double |
elapsedTime(Counter.Type type,
TimeUnit timeUnit,
ConversionMode conversionMode)
A convenient method that returns the elapsed time of a specific counter, in the
specified time unit, by applying a custom
ConversionMode. |
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.
|
public MonitoredCallable(Callable<V> targetCallable)
Callable. All available counter
types will be maintained.targetCallable - the Callable to be executedpublic MonitoredCallable(Callable<V> targetCallable, Counter.Type... types)
Callable and one or more specific
counter types to be maintained.targetCallable - the Callable to be executedtypes - the counter types to be maintained with the operationpublic V call() throws Exception
Callable.call().public Collection<Counter> getCounters()
public Counter getCounter(Counter.Type type)
type - the counter type to be fetchedIllegalArgumentException - if the specified type is not available in this
operationpublic Duration elapsedTime(Counter.Type type)
This has the same effect as calling: operation.getCounter(type).elapsedTime()
type - the counter type to be fetchedIllegalArgumentException - if the specified type is not available in this
operationpublic double elapsedTime(Counter.Type type, TimeUnit timeUnit)
This has the same effect as calling:
operation.getCounter(type).elapsedTime(timeUnit)
type - the counter type to be fetchedtimeUnit - the time unit to which the elapsed time will be convertedIllegalArgumentException - if the specified type is not available in this
operationpublic double elapsedTime(Counter.Type type, TimeUnit timeUnit, ConversionMode conversionMode)
ConversionMode.
This has the same effect as calling:
operation.getCounter(type).elapsedTime(timeUnit, conversionMode)
type - the counter type to be fetchedtimeUnit - the time unit to which the elapsed time will be convertedconversionMode - the ConversionMode to be appliedIllegalArgumentException - if the specified type is not available in this
operationpublic 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.