public class Runner extends MonitoringTaskSubmitter implements ShutdownInfo, java.lang.AutoCloseable
ScheduledExecutorService alternative
that allows you to monitor the number of submitted, failed and succeeded task, plus has the
ability to wait until all tasks are completed (without the need of shutting it down)
= wait until there are no scheduled or running tasks.executor, latch, logger| Constructor and Description |
|---|
Runner(int threadCount)
Constructs a runner with specific number of executor thread.
|
| Modifier and Type | Method and Description |
|---|---|
Runner |
awaitTermination(long timeout,
java.util.concurrent.TimeUnit unit)
Blocks until all tasks have completed execution after a shutdown
request, or the timeout occurs, or the current thread is
interrupted, whichever happens first.
|
void |
close()
Sends a shutdownNow signal to the runner.
|
int |
failedToStartCount() |
protected void |
finalize() |
void |
resetCounters()
Resets failedSubmission, failedToStart, succeeded and failed counter.
|
<T> java.util.concurrent.Future<T> |
run(ShutdownAwareCallable<T> callable)
Schedules a
ShutdownAwareCallable for immediate execution. |
java.util.concurrent.Future<java.lang.Void> |
run(ShutdownAwareTask task)
Schedules a
ShutdownAwareTask for immediate execution
It might be executed later though if the ScheduledExecutorService has no available threads
or more tasks are queued for execution before this task. |
<T> java.util.concurrent.Future<T> |
runCallable(ShutdownAwareCallable<T> callable)
Schedules a
ShutdownAwareCallable for immediate execution. |
<T> java.util.concurrent.ScheduledFuture<T> |
runCallableIn(long delay,
java.util.concurrent.TimeUnit unit,
ShutdownAwareCallable<T> callable)
Schedules a
ShutdownAwareCallable for delayed execution. |
<T> java.util.concurrent.ScheduledFuture<T> |
runIn(long delay,
java.util.concurrent.TimeUnit unit,
ShutdownAwareCallable<T> callable)
Schedules a
ShutdownAwareCallable for delayed execution. |
java.util.concurrent.ScheduledFuture<java.lang.Void> |
runIn(long delay,
java.util.concurrent.TimeUnit unit,
ShutdownAwareTask task)
Schedules a
ShutdownAwareTask for delayed execution
The delay is defined using the delay and unit parameters. |
static Runner |
runner(int threadCount)
Factory method to create a
Runner. |
java.util.concurrent.Future<java.lang.Void> |
runTask(ShutdownAwareTask task)
Schedules a
ShutdownAwareTask for immediate execution
It might be executed later though if the ScheduledExecutorService has no available threads
or more tasks are queued for execution before this task. |
java.util.concurrent.ScheduledFuture<java.lang.Void> |
runTaskIn(long delay,
java.util.concurrent.TimeUnit unit,
ShutdownAwareTask task)
Schedules a
ShutdownAwareTask for delayed execution
The delay is defined using the delay and unit parameters. |
Runner |
shutdown()
Sends a shutdown signal to the runner.
|
Runner |
shutdownAndAwaitTermination()
Calls
shutdown() and awaitTermination(long, TimeUnit) methods in sequence
where the timeout is set to 5 seconds. |
Runner |
shutdownAndAwaitTermination(long timeout,
java.util.concurrent.TimeUnit unit)
Calls
shutdown() and awaitTermination(long, TimeUnit) methods in sequence |
Runner |
shutdownNow()
Sends a shutdownNow signal to the runner.
|
Runner |
shutdownNowAndAwaitTermination()
Calls
shutdownNow() and awaitTermination(long, TimeUnit) methods in sequence
where the timeout is set to 5 seconds. |
Runner |
shutdownNowAndAwaitTermination(long timeout,
java.util.concurrent.TimeUnit unit)
Calls
shutdownNow() and awaitTermination(long, TimeUnit) methods in sequence |
Runner |
waitTillDone()
Causes the current thread to wait until there are no unfinished tasks,
unless the thread is interrupted.
|
boolean |
wasShutdownTriggered()
Returns
true if any shutdown operation has been called on this Runner. |
failedCount, failedSubmissionCount, onWorkException, onWorkThrowable, run, run, runCallable, runCallableIn, runIn, runIn, runRunnable, runRunnableIn, runTask, runTaskIn, succeededCount, toBeCompletedCount, waitTillDonepublic Runner(int threadCount)
threadCount - number of executor threadspublic static Runner runner(int threadCount)
Runner.threadCount - number of executor threadsRunnerpublic <T> java.util.concurrent.Future<T> run(ShutdownAwareCallable<T> callable)
ShutdownAwareCallable for immediate execution.
It might be executed later though if the ScheduledExecutorService has no available threads
or more tasks are queued for execution before this task.T - type of return valuecallable - ShutdownAwareCallable to be executedFuture referring to the state of submitted ShutdownAwareCallablepublic java.util.concurrent.Future<java.lang.Void> run(ShutdownAwareTask task)
ShutdownAwareTask for immediate execution
It might be executed later though if the ScheduledExecutorService has no available threads
or more tasks are queued for execution before this task.task - ShutdownAwareTask to be executedFuture referring to the state of submitted ShutdownAwareTaskpublic <T> java.util.concurrent.ScheduledFuture<T> runIn(long delay,
java.util.concurrent.TimeUnit unit,
ShutdownAwareCallable<T> callable)
ShutdownAwareCallable for delayed execution.
The delay is defined using the delay and unit parameters.
It might be executed later though if the ScheduledExecutorService has no available threads
or more tasks are queued for execution before this task.T - type of return valuedelay - the value of the delayunit - the TimeUnit of the delay argumentcallable - ShutdownAwareCallable to be executedFuture referring to the state of submitted ShutdownAwareCallablepublic java.util.concurrent.ScheduledFuture<java.lang.Void> runIn(long delay,
java.util.concurrent.TimeUnit unit,
ShutdownAwareTask task)
ShutdownAwareTask for delayed execution
The delay is defined using the delay and unit parameters.
It might be executed later though if the ScheduledExecutorService has no available threads
or more tasks are queued for execution before this task.delay - the value of the delayunit - the TimeUnit of the delay argumenttask - ShutdownAwareTask to be executedFuture referring to the state of submitted ShutdownAwareTaskpublic <T> java.util.concurrent.Future<T> runCallable(ShutdownAwareCallable<T> callable)
ShutdownAwareCallable for immediate execution.
It might be executed later though if the ScheduledExecutorService has no available threads
or more tasks are queued for execution before this task.T - type of return valuecallable - ShutdownAwareCallable to be executedFuture referring to the state of submitted ShutdownAwareCallablepublic java.util.concurrent.Future<java.lang.Void> runTask(ShutdownAwareTask task)
ShutdownAwareTask for immediate execution
It might be executed later though if the ScheduledExecutorService has no available threads
or more tasks are queued for execution before this task.task - ShutdownAwareTask to be executedFuture referring to the state of submitted ShutdownAwareTaskpublic <T> java.util.concurrent.ScheduledFuture<T> runCallableIn(long delay,
java.util.concurrent.TimeUnit unit,
ShutdownAwareCallable<T> callable)
ShutdownAwareCallable for delayed execution.
The delay is defined using the delay and unit parameters.
It might be executed later though if the ScheduledExecutorService has no available threads
or more tasks are queued for execution before this task.T - type of return valuedelay - the value of the delayunit - the TimeUnit of the delay argumentcallable - ShutdownAwareCallable to be executedFuture referring to the state of submitted ShutdownAwareCallablepublic java.util.concurrent.ScheduledFuture<java.lang.Void> runTaskIn(long delay,
java.util.concurrent.TimeUnit unit,
ShutdownAwareTask task)
ShutdownAwareTask for delayed execution
The delay is defined using the delay and unit parameters.
It might be executed later though if the ScheduledExecutorService has no available threads
or more tasks are queued for execution before this task.delay - the value of the delayunit - the TimeUnit of the delay argumenttask - ShutdownAwareTask to be executedFuture referring to the state of submitted ShutdownAwareTaskpublic Runner waitTillDone()
MonitoringTaskSubmitter
If the current count of to be finished tasks is zero then this method returns immediately.
If the current count is greater than zero then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of two things happen:
If the current thread:
RuntimeException wrapping an InterruptedException is thrown
and the current thread's interrupted status is cleared.waitTillDone in class MonitoringTaskSubmitterMonitoringTaskSubmitter instancepublic Runner shutdown()
Runner instanceExecutorService.shutdown()public Runner shutdownNow()
Runner instanceExecutorService.shutdownNow()public void close()
throws java.lang.Exception
close in interface java.lang.AutoCloseablejava.lang.ExceptionExecutorService.shutdownNow()public boolean wasShutdownTriggered()
true if any shutdown operation has been called on this Runner.
In this case the Runner won't accept any new tasks for execution.wasShutdownTriggered in interface ShutdownInfotrue if shutdown on the Runner has been triggeredpublic Runner awaitTermination(long timeout, java.util.concurrent.TimeUnit unit)
timeout - the maximum time to waitunit - the time unit of the timeout argumentRunner instancejava.lang.RuntimeException - wrapping an InterruptedException if interrupted while waitingpublic Runner shutdownAndAwaitTermination(long timeout, java.util.concurrent.TimeUnit unit)
shutdown() and awaitTermination(long, TimeUnit) methods in sequencetimeout - the maximum time to waitunit - the time unit of the timeout argumentRunner instancejava.lang.RuntimeException - wrapping an InterruptedException if interrupted while waitingpublic Runner shutdownAndAwaitTermination()
shutdown() and awaitTermination(long, TimeUnit) methods in sequence
where the timeout is set to 5 seconds.Runner instancejava.lang.RuntimeException - wrapping an InterruptedException if interrupted while waitingpublic Runner shutdownNowAndAwaitTermination(long timeout, java.util.concurrent.TimeUnit unit)
shutdownNow() and awaitTermination(long, TimeUnit) methods in sequencetimeout - the maximum time to waitunit - the time unit of the timeout argumentRunner instancejava.lang.RuntimeException - wrapping an InterruptedException if interrupted while waitingpublic Runner shutdownNowAndAwaitTermination()
shutdownNow() and awaitTermination(long, TimeUnit) methods in sequence
where the timeout is set to 5 seconds.Runner instancejava.lang.RuntimeException - wrapping an InterruptedException if interrupted while waitingpublic int failedToStartCount()
public void resetCounters()
resetCounters in class MonitoringTaskSubmitterprotected void finalize()
throws java.lang.Throwable
finalize in class java.lang.Objectjava.lang.Throwable