public class SimpleAsyncTaskExecutor extends CustomizableThreadCreator implements AsyncListenableTaskExecutor, Serializable
TaskExecutor
implementation that fires up a new Thread for each task,
executing it asynchronously.
Supports limiting concurrent threads through setConcurrencyLimit(int)
.
By default, the number of concurrent task executions is unlimited.
NOTE: This implementation does not reuse threads! Consider a thread-pooling TaskExecutor implementation instead, in particular for executing a large number of short-lived tasks.
setConcurrencyLimit(int)
,
SyncTaskExecutor
,
ThreadPoolTaskExecutor
,
WorkManagerTaskExecutor
,
Serialized FormModifier and Type | Field and Description |
---|---|
static int |
NO_CONCURRENCY
Switch concurrency 'off': that is, don't allow any concurrent invocations.
|
static int |
UNBOUNDED_CONCURRENCY
Permit any number of concurrent invocations: that is, don't throttle concurrency.
|
TIMEOUT_IMMEDIATE, TIMEOUT_INDEFINITE
Constructor and Description |
---|
SimpleAsyncTaskExecutor()
Create a new SimpleAsyncTaskExecutor with default thread name prefix.
|
SimpleAsyncTaskExecutor(String threadNamePrefix)
Create a new SimpleAsyncTaskExecutor with the given thread name prefix.
|
SimpleAsyncTaskExecutor(ThreadFactory threadFactory)
Create a new SimpleAsyncTaskExecutor with the given external thread factory.
|
Modifier and Type | Method and Description |
---|---|
protected void |
doExecute(Runnable task)
Template method for the actual execution of a task.
|
void |
execute(Runnable task)
Executes the given task, within a concurrency throttle
if configured (through the superclass's settings).
|
void |
execute(Runnable task,
long startTimeout)
Deprecated.
|
int |
getConcurrencyLimit()
Return the maximum number of parallel task executions allowed.
|
ThreadFactory |
getThreadFactory()
Return the external factory to use for creating new Threads, if any.
|
boolean |
isThrottleActive()
Return whether the concurrency throttle is currently active.
|
void |
setConcurrencyLimit(int concurrencyLimit)
Set the maximum number of parallel task executions allowed.
|
void |
setTaskDecorator(TaskDecorator taskDecorator)
Specify a custom
TaskDecorator to be applied to any Runnable
about to be executed. |
void |
setThreadFactory(ThreadFactory threadFactory)
Specify an external factory to use for creating new Threads,
instead of relying on the local properties of this executor.
|
<T> Future<T> |
submit(Callable<T> task)
Submit a Callable task for execution, receiving a Future representing that task.
|
Future<?> |
submit(Runnable task)
Submit a Runnable task for execution, receiving a Future representing that task.
|
<T> ListenableFuture<T> |
submitListenable(Callable<T> task)
Submit a
Callable task for execution, receiving a ListenableFuture
representing that task. |
ListenableFuture<?> |
submitListenable(Runnable task)
Submit a
Runnable task for execution, receiving a ListenableFuture
representing that task. |
createThread, getDefaultThreadNamePrefix, getThreadGroup, getThreadNamePrefix, getThreadPriority, isDaemon, nextThreadName, setDaemon, setThreadGroup, setThreadGroupName, setThreadNamePrefix, setThreadPriority
public static final int UNBOUNDED_CONCURRENCY
public static final int NO_CONCURRENCY
public SimpleAsyncTaskExecutor()
public SimpleAsyncTaskExecutor(String threadNamePrefix)
threadNamePrefix
- the prefix to use for the names of newly created threadspublic SimpleAsyncTaskExecutor(ThreadFactory threadFactory)
threadFactory
- the factory to use for creating new Threadspublic void setThreadFactory(@Nullable ThreadFactory threadFactory)
You may specify an inner ThreadFactory bean or also a ThreadFactory reference obtained from JNDI (on a Java EE 6 server) or some other lookup mechanism.
@Nullable public final ThreadFactory getThreadFactory()
public void setTaskDecorator(TaskDecorator taskDecorator)
TaskDecorator
to be applied to any Runnable
about to be executed.
Note that such a decorator is not necessarily being applied to the
user-supplied Runnable
/Callable
but rather to the actual
execution callback (which may be a wrapper around the user-supplied task).
The primary use case is to set some execution context around the task's invocation, or to provide some monitoring/statistics for task execution.
NOTE: Exception handling in TaskDecorator
implementations
is limited to plain Runnable
execution via execute
calls.
In case of #submit
calls, the exposed Runnable
will be a
FutureTask
which does not propagate any exceptions; you might
have to cast it and call Future#get
to evaluate exceptions.
public void setConcurrencyLimit(int concurrencyLimit)
This is the equivalent of a maximum pool size in a thread pool, preventing temporary overload of the thread management system.
public final int getConcurrencyLimit()
public final boolean isThrottleActive()
true
if the concurrency limit for this instance is activegetConcurrencyLimit()
,
setConcurrencyLimit(int)
public void execute(Runnable task)
execute
in interface Executor
execute
in interface TaskExecutor
task
- the Runnable
to execute (never null
)doExecute(Runnable)
@Deprecated public void execute(Runnable task, long startTimeout)
Executes urgent tasks (with 'immediate' timeout) directly, bypassing the concurrency throttle (if active). All other tasks are subject to throttling.
execute
in interface AsyncTaskExecutor
task
- the Runnable
to execute (never null
)startTimeout
- the time duration (milliseconds) within which the task is
supposed to start. This is intended as a hint to the executor, allowing for
preferred handling of immediate tasks. Typical values are AsyncTaskExecutor.TIMEOUT_IMMEDIATE
or AsyncTaskExecutor.TIMEOUT_INDEFINITE
(the default as used by TaskExecutor.execute(Runnable)
).AsyncTaskExecutor.TIMEOUT_IMMEDIATE
,
doExecute(Runnable)
public Future<?> submit(Runnable task)
AsyncTaskExecutor
null
result upon completion.submit
in interface AsyncTaskExecutor
task
- the Runnable
to execute (never null
)public <T> Future<T> submit(Callable<T> task)
AsyncTaskExecutor
submit
in interface AsyncTaskExecutor
task
- the Callable
to execute (never null
)public ListenableFuture<?> submitListenable(Runnable task)
AsyncListenableTaskExecutor
Runnable
task for execution, receiving a ListenableFuture
representing that task. The Future will return a null
result upon completion.submitListenable
in interface AsyncListenableTaskExecutor
task
- the Runnable
to execute (never null
)ListenableFuture
representing pending completion of the taskpublic <T> ListenableFuture<T> submitListenable(Callable<T> task)
AsyncListenableTaskExecutor
Callable
task for execution, receiving a ListenableFuture
representing that task. The Future will return the Callable's result upon
completion.submitListenable
in interface AsyncListenableTaskExecutor
task
- the Callable
to execute (never null
)ListenableFuture
representing pending completion of the taskprotected void doExecute(Runnable task)
The default implementation creates a new Thread and starts it.
task
- the Runnable to executesetThreadFactory(java.util.concurrent.ThreadFactory)
,
CustomizableThreadCreator.createThread(java.lang.Runnable)
,
Thread.start()