public class BoundedScheduledExecutorService extends Object implements SilentScheduledExecutorService, 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 |
|---|
BoundedScheduledExecutorService(int capacity)
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 command) |
<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> callable,
long delay,
TimeUnit unit) |
ScheduledFuture<?> |
schedule(Runnable command,
long delay,
TimeUnit unit) |
ScheduledFuture<?> |
scheduleAtFixedRate(Runnable command,
long initialDelay,
long period,
TimeUnit unit) |
ScheduledFuture<?> |
scheduleWithFixedDelay(Runnable command,
long initialDelay,
long delay,
TimeUnit unit) |
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). |
void |
silentExecute(Runnable task)
Does the same as
Executor.execute(Runnable) but doesn't throw a
RejectedExecutionException if this task cannot be accepted for execution. |
<T> List<Future<T>> |
silentInvokeAll(Collection<? extends Callable<T>> tasks)
Does the same as
ExecutorService.invokeAll(Collection) but returns null instead of
throwing a RejectedExecutionException if any task cannot be scheduled for execution. |
<T> List<Future<T>> |
silentInvokeAll(Collection<? extends Callable<T>> tasks,
long timeout,
TimeUnit unit)
Does the same as
ExecutorService.invokeAll(Collection, long, TimeUnit) but returns null
instead of throwing a RejectedExecutionException if any task cannot be scheduled for execution. |
<T> T |
silentInvokeAny(Collection<? extends Callable<T>> tasks)
Does the same as
ExecutorService.invokeAny(Collection) but returns null instead of
throwing a RejectedExecutionException if tasks cannot be scheduled for execution. |
<T> T |
silentInvokeAny(Collection<? extends Callable<T>> tasks,
long timeout,
TimeUnit unit)
Does the same as
ExecutorService.invokeAny(Collection, long, TimeUnit) but returns null
instead of throwing a RejectedExecutionException if tasks cannot be scheduled for execution. |
<V> ScheduledFuture<V> |
silentSchedule(Callable<V> task,
long delay,
TimeUnit unit)
Does the same as
ScheduledExecutorService.schedule(Callable, long, TimeUnit) but returns null
instead of throwing a RejectedExecutionException if the task cannot be scheduled for
execution. |
ScheduledFuture<?> |
silentSchedule(Runnable task,
long delay,
TimeUnit unit)
Does the same as
ScheduledExecutorService.schedule(Runnable, long, TimeUnit) but returns null
instead of throwing a RejectedExecutionException if the task cannot be scheduled for execution. |
ScheduledFuture<?> |
silentScheduleAtFixedRate(Runnable task,
long initialDelay,
long period,
TimeUnit unit)
Does the same as
ScheduledExecutorService.scheduleAtFixedRate(Runnable, long, long, TimeUnit) but returns
null instead of throwing a RejectedExecutionException if the task cannot be scheduled for
execution. |
ScheduledFuture<?> |
silentScheduleWithFixedDelay(Runnable task,
long initialDelay,
long delay,
TimeUnit unit)
Does the same as
ScheduledExecutorService.scheduleWithFixedDelay(Runnable, long, long, TimeUnit) but returns null
instead of throwing a RejectedExecutionException if the task cannot be scheduled for execution. |
<T> Future<T> |
silentSubmit(Callable<T> task)
Does the same as
ExecutorService.submit(Callable) but returns null instead of throwing a
RejectedExecutionException if the task cannot be scheduled for execution. |
Future<?> |
silentSubmit(Runnable task)
Does the same as
ExecutorService.submit(Runnable) but returns null instead of throwing a
RejectedExecutionException if the task cannot be scheduled for execution. |
<T> Future<T> |
silentSubmit(Runnable task,
T result)
Does the same as
ExecutorService.submit(Runnable, Object) but returns null instead of
throwing a RejectedExecutionException if the task cannot be scheduled for execution. |
<T> Future<T> |
submit(Callable<T> task) |
Future<?> |
submit(Runnable task) |
<T> Future<T> |
submit(Runnable task,
T result) |
public BoundedScheduledExecutorService(int capacity)
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<?> silentSchedule(Runnable task, long delay, TimeUnit unit)
ScheduledExecutorService.schedule(Runnable, long, TimeUnit) but returns null
instead of throwing a RejectedExecutionException if the task cannot be scheduled for execution.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.silentSchedule in interface SilentScheduledExecutorServicetask - the task to executedelay - the time from now to delay executionunit - the time unit of the delay parameterget() method will return
null upon completion / null if the task cannot be scheduled for executionpublic <V> ScheduledFuture<V> silentSchedule(Callable<V> task, long delay, TimeUnit unit)
ScheduledExecutorService.schedule(Callable, long, TimeUnit) but returns null
instead of throwing a RejectedExecutionException if the task cannot be scheduled for
execution.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.silentSchedule in interface SilentScheduledExecutorServiceV - the type of the callable's resulttask - the function to executedelay - the time from now to delay executionunit - the time unit of the delay parameternull if the task cannot be
scheduled for executionpublic ScheduledFuture<?> silentScheduleAtFixedRate(Runnable task, long initialDelay, long period, TimeUnit unit)
ScheduledExecutorService.scheduleAtFixedRate(Runnable, long, long, TimeUnit) but returns
null instead of throwing a RejectedExecutionException if the task cannot be scheduled for
execution.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.silentScheduleAtFixedRate in interface SilentScheduledExecutorServicetask - the task to executeinitialDelay - the time to delay first executionperiod - the period between successive executionsunit - the time unit of the initialDelay and period parametersget() method will throw
an exception upon cancellation / null if the task cannot be scheduled for executionpublic ScheduledFuture<?> silentScheduleWithFixedDelay(Runnable task, long initialDelay, long delay, TimeUnit unit)
ScheduledExecutorService.scheduleWithFixedDelay(Runnable, long, long, TimeUnit) but returns null
instead of throwing a RejectedExecutionException if the task cannot be scheduled for execution.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.silentScheduleWithFixedDelay in interface SilentScheduledExecutorServicetask - the task to executeinitialDelay - the time to delay first executiondelay - the delay between the termination of one execution and the commencement of the nextunit - the time unit of the initialDelay and delay parametersget() method will throw
an exception upon cancellation / null if the task cannot be scheduled for executionpublic <T> Future<T> silentSubmit(Callable<T> task)
ExecutorService.submit(Callable) but returns null instead of throwing a
RejectedExecutionException if the task cannot be scheduled for execution.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.silentSubmit in interface SilentScheduledExecutorServiceT - the type of the task's resulttask - the task to submitnull if the task cannot be scheduled for executionpublic Future<?> silentSubmit(Runnable task)
ExecutorService.submit(Runnable) but returns null instead of throwing a
RejectedExecutionException if the task cannot be scheduled for execution.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.silentSubmit in interface SilentScheduledExecutorServicetask - the task to submitnull if the task cannot be scheduled for
executionpublic <T> Future<T> silentSubmit(Runnable task, T result)
ExecutorService.submit(Runnable, Object) but returns null instead of
throwing a RejectedExecutionException if the task cannot be scheduled for execution.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.silentSubmit in interface SilentScheduledExecutorServiceT - the type of the resulttask - the task to submitresult - the result to returnnull if the task cannot be scheduled for
executionpublic <T> List<Future<T>> silentInvokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException
ExecutorService.invokeAll(Collection) but returns null instead of
throwing a RejectedExecutionException if any task cannot be scheduled for execution.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.silentInvokeAll in interface SilentScheduledExecutorServiceT - the type of the values returned from the taskstasks - the collection of tasksnull if any task cannot be scheduled for
executionInterruptedException - if interrupted while waiting, in which case unfinished tasks are cancelledpublic <T> List<Future<T>> silentInvokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException
ExecutorService.invokeAll(Collection, long, TimeUnit) but returns null
instead of throwing a RejectedExecutionException if any task cannot be scheduled for execution.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.silentInvokeAll in interface SilentScheduledExecutorServiceT - the type of the values returned from the taskstasks - the collection of taskstimeout - the maximum time to waitunit - the time unit of the timeout argumentnull if any task cannot be scheduled
for execution.InterruptedException - if interrupted while waiting, in which case unfinished tasks are cancelledpublic <T> T silentInvokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException
ExecutorService.invokeAny(Collection) but returns null instead of
throwing a RejectedExecutionException if tasks cannot be scheduled for execution.Futures are not passed to the caller, we only wrap the Callables to correctly
free the resources and omit wrapping the Futures as well.silentInvokeAny in interface SilentScheduledExecutorServiceT - the type of the values returned from the taskstasks - the collection of tasksnull if tasks cannot be scheduled for executionInterruptedException - if interrupted while waitingExecutionException - if no task successfully completespublic <T> T silentInvokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
ExecutorService.invokeAny(Collection, long, TimeUnit) but returns null
instead of throwing a RejectedExecutionException if tasks cannot be scheduled for execution.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.silentInvokeAny in interface SilentScheduledExecutorServiceT - the type of the values returned from the taskstasks - the collection of taskstimeout - the maximum time to waitunit - the time unit of the timeout argumentnull if tasks cannot be scheduled for executionInterruptedException - if interrupted while waitingExecutionException - if no task successfully completesTimeoutException - if the given timeout elapses before any task successfully completespublic void silentExecute(Runnable task)
Executor.execute(Runnable) but doesn't throw a
RejectedExecutionException if this task cannot be accepted for execution.Future passed to the caller, we only wrap the Runnable to correctly free
the resources.silentExecute in interface SilentScheduledExecutorServicetask - the runnable taskpublic ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit)
schedule in interface ScheduledExecutorServicepublic <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit)
schedule in interface ScheduledExecutorServicepublic ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
scheduleAtFixedRate in interface ScheduledExecutorServicepublic ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)
scheduleWithFixedDelay in interface ScheduledExecutorServicepublic <T> Future<T> submit(Callable<T> task)
submit in interface ExecutorServicepublic <T> Future<T> submit(Runnable task, T result)
submit in interface ExecutorServicepublic Future<?> submit(Runnable task)
submit in interface ExecutorServicepublic <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException
invokeAll in interface ExecutorServiceInterruptedExceptionpublic <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException
invokeAll in interface ExecutorServiceInterruptedExceptionpublic <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException
invokeAny in interface ExecutorServiceInterruptedExceptionExecutionExceptionpublic <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
invokeAny in interface ExecutorServiceInterruptedExceptionExecutionExceptionTimeoutExceptionpublic 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.