Class ExecutorTaskInterceptor
- java.lang.Object
-
- org.threadly.concurrent.wrapper.interceptor.ExecutorTaskInterceptor
-
- All Implemented Interfaces:
java.util.concurrent.Executor
,SubmitterExecutor
- Direct Known Subclasses:
SubmitterSchedulerTaskInterceptor
public class ExecutorTaskInterceptor extends java.lang.Object implements SubmitterExecutor
Class to wrapExecutor
pool so that tasks can be intercepted and either wrapped, or modified, before being submitted to the pool. This class can be passed a lamba in the constructor, or you can extend this class and override the functionwrapTask(Runnable)
to provide the task which should be submitted to theExecutor
. Please see the javadocs ofwrapTask(Runnable)
for more details about ways a task can be modified or wrapped.Other variants of task wrappers:
SubmitterSchedulerTaskInterceptor
,SchedulerServiceTaskInterceptor
,PrioritySchedulerTaskInterceptor
.- Since:
- 4.6.0
-
-
Constructor Summary
Constructors Constructor Description ExecutorTaskInterceptor(java.util.concurrent.Executor parentExecutor, java.util.function.Function<java.lang.Runnable,java.lang.Runnable> taskManipulator)
Constructs a wrapper forExecutor
pool so that tasks can be intercepted and modified, before being submitted to the pool.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
execute(java.lang.Runnable task)
<T> ListenableFuture<T>
submit(java.lang.Runnable task, T result)
Submit a task to run as soon as possible.<T> ListenableFuture<T>
submit(java.util.concurrent.Callable<T> task)
Submit aCallable
to run as soon as possible.java.lang.Runnable
wrapTask(java.lang.Runnable task)
Implementation to modify a provided task.-
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.threadly.concurrent.SubmitterExecutor
submit
-
-
-
-
Constructor Detail
-
ExecutorTaskInterceptor
public ExecutorTaskInterceptor(java.util.concurrent.Executor parentExecutor, java.util.function.Function<java.lang.Runnable,java.lang.Runnable> taskManipulator)
Constructs a wrapper forExecutor
pool so that tasks can be intercepted and modified, before being submitted to the pool.- Parameters:
parentExecutor
- An instance ofExecutor
to wraptaskManipulator
- A lambda to manipulate aRunnable
that was submitted for execution
-
-
Method Detail
-
wrapTask
public java.lang.Runnable wrapTask(java.lang.Runnable task)
Implementation to modify a provided task. The provided runnable will be the one submitted to the Executor, unless aCallable
was submitted, in which case aListenableFutureTask
will be provided. In the last condition the original callable can be retrieved by invokingListenableFutureTask.getContainedCallable()
. The returned task can not be null, but could be either the original task, a modified task, a wrapper to the provided task, or if no action is desiredDoNothingRunnable.instance()
may be provided. However caution should be used in that if aListenableFutureTask
is provided, and then never returned (and not canceled), then the future will never complete (and thus possibly forever blocked). So if you are doing conditional checks forListenableFutureTask
and may not execute/return the provided task, then you should be careful to ensureFuture.cancel(boolean)
is invoked.- Parameters:
task
- A runnable that was submitted for execution- Returns:
- A non-null task that will be provided to the parent executor
-
execute
public void execute(java.lang.Runnable task)
- Specified by:
execute
in interfacejava.util.concurrent.Executor
-
submit
public <T> ListenableFuture<T> submit(java.lang.Runnable task, T result)
Description copied from interface:SubmitterExecutor
Submit a task to run as soon as possible. TheFuture.get()
method will return the provided result once the runnable has completed.- Specified by:
submit
in interfaceSubmitterExecutor
- Type Parameters:
T
- type of result for future- Parameters:
task
- runnable to be executedresult
- result to be returned from resulting future .get() when runnable completes- Returns:
- a future to know when the task has completed
-
submit
public <T> ListenableFuture<T> submit(java.util.concurrent.Callable<T> task)
Description copied from interface:SubmitterExecutor
Submit aCallable
to run as soon as possible. This is needed when a result needs to be consumed from the callable.- Specified by:
submit
in interfaceSubmitterExecutor
- Type Parameters:
T
- type of result returned from the future- Parameters:
task
- callable to be executed- Returns:
- a future to know when the task has completed and get the result of the callable
-
-