public class ExecutorTaskInterceptor extends java.lang.Object implements SubmitterExecutor
Executor
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 function wrapTask(Runnable)
to provide the task which should be submitted to the Executor
. Please see the javadocs
of wrapTask(Runnable)
for more details about ways a task can be modified or wrapped.
Other variants of task wrappers: SubmitterSchedulerTaskInterceptor
,
SchedulerServiceTaskInterceptor
, PrioritySchedulerTaskInterceptor
.
Constructor and Description |
---|
ExecutorTaskInterceptor(java.util.concurrent.Executor parentExecutor,
java.util.function.Function<java.lang.Runnable,java.lang.Runnable> taskManipulator)
Constructs a wrapper for
Executor pool so that tasks can be intercepted and modified,
before being submitted to the pool. |
Modifier and Type | Method and Description |
---|---|
void |
execute(java.lang.Runnable task) |
<T> ListenableFuture<T> |
submit(java.util.concurrent.Callable<T> task)
Submit a
Callable to run as soon as possible. |
<T> ListenableFuture<T> |
submit(java.lang.Runnable task,
T result)
Submit a task to run as soon as possible.
|
java.lang.Runnable |
wrapTask(java.lang.Runnable task)
Implementation to modify a provided task.
|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
submit
public ExecutorTaskInterceptor(java.util.concurrent.Executor parentExecutor, java.util.function.Function<java.lang.Runnable,java.lang.Runnable> taskManipulator)
Executor
pool so that tasks can be intercepted and modified,
before being submitted to the pool.parentExecutor
- An instance of Executor
to wraptaskManipulator
- A lambda to manipulate a Runnable
that was submitted for executionpublic java.lang.Runnable wrapTask(java.lang.Runnable task)
Callable
was submitted, in which case a
ListenableFutureTask
will be provided. In the last condition the original callable
can be retrieved by invoking ListenableFutureTask.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 desired
DoNothingRunnable.instance()
may be provided. However caution
should be used in that if a ListenableFutureTask
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 for ListenableFutureTask
and may not
execute/return the provided task, then you should be careful to ensure
FutureTask.cancel(boolean)
is invoked.task
- A runnable that was submitted for executionpublic void execute(java.lang.Runnable task)
execute
in interface java.util.concurrent.Executor
public <T> ListenableFuture<T> submit(java.lang.Runnable task, T result)
SubmitterExecutor
Future.get()
method will
return the provided result once the runnable has completed.submit
in interface SubmitterExecutor
T
- type of result for futuretask
- runnable to be executedresult
- result to be returned from resulting future .get() when runnable completespublic <T> ListenableFuture<T> submit(java.util.concurrent.Callable<T> task)
SubmitterExecutor
Callable
to run as soon as possible. This is needed when a result needs to
be consumed from the callable.submit
in interface SubmitterExecutor
T
- type of result returned from the futuretask
- callable to be executed