public class UnboundScheduledExecutorService extends Object implements ScheduledExecutorService, ReportingExecutorService
SilentScheduledExecutorService that accepts only a pre-defined amount of tasks that
can be queued or executed at the same time. All tasks exceeding the defined limit will be ignored (instead of being
queued) by either throwing a RejectedExecutionException or returning null depending on the method we
call (non-silent vs silent).Future), it makes space
for a new task. This is useful for classes like the com.iota.iri.utils.log.interval.IntervalLogger that want
to delay an action if and only if there is no other delayed action queued already.| Constructor and Description |
|---|
UnboundScheduledExecutorService()
Creates an executor service that that accepts only a pre-defined amount of tasks that can be queued and run at
the same time.
All tasks exceeding the defined limit will be ignored (instead of being queued) by either throwing a RejectedExecutionException or returning null depending on the method we call (non-silent vs
silent). |
| Modifier and Type | Method and Description |
|---|---|
boolean |
awaitTermination(long timeout,
TimeUnit unit) |
void |
execute(Runnable task)
|
<T> List<Future<T>> |
invokeAll(Collection<? extends Callable<T>> tasks)
|
<T> List<Future<T>> |
invokeAll(Collection<? extends Callable<T>> tasks,
long timeout,
TimeUnit unit)
|
<T> T |
invokeAny(Collection<? extends Callable<T>> tasks)
|
<T> T |
invokeAny(Collection<? extends Callable<T>> tasks,
long timeout,
TimeUnit unit)
|
boolean |
isShutdown() |
boolean |
isTerminated() |
void |
onCancelTask(TaskDetails taskDetails)
This method gets called whenever a task is cancelled through its
Future or through
the shutdown methods of the ExecutorService.It only gets called once for every task. |
void |
onCompleteTask(TaskDetails taskDetails,
Throwable error)
This method gets called whenever a task completes.
This can be through either raising an exception, cancelling from the outside or by simply terminating in a normal manner. |
void |
onFinishTask(TaskDetails taskDetails,
Throwable error)
This method gets called whenever a task is finished.
For recurring tasks it is called multiple times. |
void |
onScheduleTask(TaskDetails taskDetails)
This method gets called whenever a new task is scheduled to run.
In contrast to ReportingExecutorService.onStartTask(TaskDetails) this method gets called only once for "recurring" tasks that are
scheduled to run in pre-defined intervals. |
void |
onStartTask(TaskDetails taskDetails)
This method gets called whenever a task is started.
For recurring tasks it is called multiple times. |
<V> ScheduledFuture<V> |
schedule(Callable<V> task,
long delay,
TimeUnit unit)
Note: Since the ScheduledFuture returned by this method allows to cancel jobs without their unwinding
logic being executed, we wrap the returned ScheduledFuture AND the Callable to correctly
free the resources if its Future.cancel(boolean) method is called. |
ScheduledFuture<?> |
schedule(Runnable task,
long delay,
TimeUnit unit)
Note: Since the ScheduledFuture returned by this method allows to cancel jobs without their unwinding
logic being executed, we wrap the returned ScheduledFuture AND the Runnable to correctly
free the resources if its Future.cancel(boolean) method is called. |
ScheduledFuture<?> |
scheduleAtFixedRate(Runnable task,
long initialDelay,
long period,
TimeUnit unit)
Note: Since the ScheduledFuture returned by this method allows to cancel jobs without their unwinding
logic being executed, we wrap the returned ScheduledFuture AND the Runnable to correctly
free the resources if its Future.cancel(boolean) method is called. |
ScheduledFuture<?> |
scheduleWithFixedDelay(Runnable task,
long initialDelay,
long delay,
TimeUnit unit)
Note: Since the ScheduledFuture returned by this method allows to cancel jobs without their unwinding
logic being executed, we wrap the returned ScheduledFuture AND the Runnable to correctly
free the resources if its Future.cancel(boolean) method is called. |
void |
shutdown()
In addition to delegating the method call to the internal ScheduledExecutorService, we call the cancel
logic for recurring tasks because shutdown prevents them from firing again. |
List<Runnable> |
shutdownNow()
Before delegating the method call to the internal ScheduledExecutorService, we call the
onCancelTask(TaskDetails) callback for all scheduled tasks and fire the
onCompleteTask(TaskDetails, Throwable) callback for all tasks that are not being executed right now
(otherwise this will be fired inside the wrapped task). |
<T> Future<T> |
submit(Callable<T> task)
Note: Since the Future returned by this method allows to cancel jobs without their unwinding logic being
executed, we wrap the returned Future AND the Callable to correctly free the resources if
its Future.cancel(boolean) method is called. |
Future<?> |
submit(Runnable task)
Note: Since the Future returned by this method allows to cancel jobs without their unwinding logic being
executed, we wrap the returned Future AND the Runnable to correctly free the resources if
its Future.cancel(boolean) method is called. |
<T> Future<T> |
submit(Runnable task,
T result)
Note: Since the Future returned by this method allows to cancel jobs without their unwinding logic being
executed, we wrap the returned Future AND the Runnable to correctly free the resources if
its Future.cancel(boolean) method is called. |
public UnboundScheduledExecutorService()
RejectedExecutionException or returning null depending on the method we call (non-silent vs
silent).capacity - the amount of tasks that can be scheduled simultaneouslypublic void onScheduleTask(TaskDetails taskDetails)
ReportingExecutorService.onStartTask(TaskDetails) this method gets called only once for "recurring" tasks that are
scheduled to run in pre-defined intervals.onScheduleTask in interface ReportingExecutorServicetaskDetails - object containing details about this taskpublic void onStartTask(TaskDetails taskDetails)
ReportingExecutorServiceonStartTask in interface ReportingExecutorServicetaskDetails - object containing details about this taskpublic void onFinishTask(TaskDetails taskDetails, Throwable error)
ReportingExecutorServiceonFinishTask in interface ReportingExecutorServicetaskDetails - object containing details about this taskerror - Exception that caused the task to completepublic void onCancelTask(TaskDetails taskDetails)
ReportingExecutorServiceFuture or through
the shutdown methods of the ExecutorService.onCancelTask in interface ReportingExecutorServicetaskDetails - object containing details about this taskpublic void onCompleteTask(TaskDetails taskDetails, Throwable error)
scheduledTasksCounter and removing the task from the
scheduledTasks set.onCompleteTask in interface ReportingExecutorServicetaskDetails - object containing details about this taskerror - Exception that caused the task to completepublic ScheduledFuture<?> schedule(Runnable task, long delay, TimeUnit unit)
ScheduledFuture returned by this method allows to cancel jobs without their unwinding
logic being executed, we wrap the returned ScheduledFuture AND the Runnable to correctly
free the resources if its Future.cancel(boolean) method is called.schedule in interface ScheduledExecutorServicepublic <V> ScheduledFuture<V> schedule(Callable<V> task, long delay, TimeUnit unit)
ScheduledFuture returned by this method allows to cancel jobs without their unwinding
logic being executed, we wrap the returned ScheduledFuture AND the Callable to correctly
free the resources if its Future.cancel(boolean) method is called.schedule in interface ScheduledExecutorServicepublic ScheduledFuture<?> scheduleAtFixedRate(Runnable task, long initialDelay, long period, TimeUnit unit)
ScheduledFuture returned by this method allows to cancel jobs without their unwinding
logic being executed, we wrap the returned ScheduledFuture AND the Runnable to correctly
free the resources if its Future.cancel(boolean) method is called.scheduleAtFixedRate in interface ScheduledExecutorServicepublic ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, long initialDelay, long delay, TimeUnit unit)
ScheduledFuture returned by this method allows to cancel jobs without their unwinding
logic being executed, we wrap the returned ScheduledFuture AND the Runnable to correctly
free the resources if its Future.cancel(boolean) method is called.scheduleWithFixedDelay in interface ScheduledExecutorServicepublic <T> Future<T> submit(Callable<T> task)
Future returned by this method allows to cancel jobs without their unwinding logic being
executed, we wrap the returned Future AND the Callable to correctly free the resources if
its Future.cancel(boolean) method is called.submit in interface ExecutorServicepublic Future<?> submit(Runnable task)
Future returned by this method allows to cancel jobs without their unwinding logic being
executed, we wrap the returned Future AND the Runnable to correctly free the resources if
its Future.cancel(boolean) method is called.submit in interface ExecutorServicepublic <T> Future<T> submit(Runnable task, T result)
Future returned by this method allows to cancel jobs without their unwinding logic being
executed, we wrap the returned Future AND the Runnable to correctly free the resources if
its Future.cancel(boolean) method is called.submit in interface ExecutorServicepublic <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException
Futures will all be finished (and cannot be cancelled anymore) when the underlying method
returns, we only wrap the Callables to correctly free the resources and omit wrapping the returned
Futures.invokeAll in interface ExecutorServiceInterruptedExceptionpublic <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException
Futures will all be finished (and cannot be cancelled anymore) when the underlying method
returns, we only wrap the Callables to correctly free the resources and omit wrapping the returned
Futures.invokeAll in interface ExecutorServiceInterruptedExceptionpublic <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException
Futures are not passed to the caller, we only wrap the Callables to correctly
free the resources and omit wrapping the Futures as well.invokeAny in interface ExecutorServiceInterruptedExceptionExecutionExceptionpublic <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
Futures are not passed to the caller, we only wrap the Callables to correctly
free the resources and omit wrapping the related Futures as well.invokeAny in interface ExecutorServiceInterruptedExceptionExecutionExceptionTimeoutExceptionpublic void execute(Runnable task)
public void shutdown()
ScheduledExecutorService, we call the cancel
logic for recurring tasks because shutdown prevents them from firing again. If these "cancelled" jobs are
scheduled for execution (and not running right now), we also call their
onCompleteTask(TaskDetails, Throwable) callback to "report" that hey have finished (otherwise this will
be fired inside the wrapped task).shutdown in interface ExecutorServicepublic List<Runnable> shutdownNow()
ScheduledExecutorService, we call the
onCancelTask(TaskDetails) callback for all scheduled tasks and fire the
onCompleteTask(TaskDetails, Throwable) callback for all tasks that are not being executed right now
(otherwise this will be fired inside the wrapped task).shutdownNow in interface ExecutorServicepublic boolean isShutdown()
isShutdown in interface ExecutorServicepublic boolean isTerminated()
isTerminated in interface ExecutorServicepublic boolean awaitTermination(long timeout,
TimeUnit unit)
throws InterruptedException
awaitTermination in interface ExecutorServiceInterruptedExceptionCopyright © 2019. All rights reserved.