Class SimpleAsyncTaskExecutor
- All Implemented Interfaces:
Serializable
,Executor
,AsyncListenableTaskExecutor
,AsyncTaskExecutor
,TaskExecutor
TaskExecutor
implementation that fires up a new Thread for each task,
executing it asynchronously.
Supports limiting concurrent threads through the "concurrencyLimit" bean property. By default, the number of concurrent threads 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.
- Since:
- 2.0
- Author:
- Juergen Hoeller
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
Switch concurrency 'off': that is, don't allow any concurrent invocations.static final int
Permit any number of concurrent invocations: that is, don't throttle concurrency.Fields inherited from interface org.springframework.core.task.AsyncTaskExecutor
TIMEOUT_IMMEDIATE, TIMEOUT_INDEFINITE
-
Constructor Summary
ConstructorDescriptionCreate 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. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
Template method for the actual execution of a task.void
Executes the given task, within a concurrency throttle if configured (through the superclass's settings).void
Deprecated.final int
Return the maximum number of parallel accesses allowed.final ThreadFactory
Return the external factory to use for creating new Threads, if any.final boolean
Return whether this throttle is currently active.void
setConcurrencyLimit
(int concurrencyLimit) Set the maximum number of parallel accesses allowed.final void
setTaskDecorator
(TaskDecorator taskDecorator) Specify a customTaskDecorator
to be applied to anyRunnable
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.void
setVirtualThreads
(boolean virtual) Switch this executor to virtual threads.Future<?>
Submit a Runnable task for execution, receiving a Future representing that task.<T> Future<T>
Submit a Callable task for execution, receiving a Future representing that task.submitListenable
(Runnable task) Submit aRunnable
task for execution, receiving aListenableFuture
representing that task.<T> ListenableFuture<T>
submitListenable
(Callable<T> task) Submit aCallable
task for execution, receiving aListenableFuture
representing that task.Methods inherited from class org.springframework.util.CustomizableThreadCreator
createThread, getDefaultThreadNamePrefix, getThreadGroup, getThreadNamePrefix, getThreadPriority, isDaemon, nextThreadName, setDaemon, setThreadGroup, setThreadGroupName, setThreadNamePrefix, setThreadPriority
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.springframework.core.task.AsyncTaskExecutor
submitCompletable, submitCompletable
-
Field Details
-
UNBOUNDED_CONCURRENCY
public static final int UNBOUNDED_CONCURRENCYPermit any number of concurrent invocations: that is, don't throttle concurrency. -
NO_CONCURRENCY
public static final int NO_CONCURRENCYSwitch concurrency 'off': that is, don't allow any concurrent invocations.
-
-
Constructor Details
-
SimpleAsyncTaskExecutor
public SimpleAsyncTaskExecutor()Create a new SimpleAsyncTaskExecutor with default thread name prefix. -
SimpleAsyncTaskExecutor
Create a new SimpleAsyncTaskExecutor with the given thread name prefix.- Parameters:
threadNamePrefix
- the prefix to use for the names of newly created threads
-
SimpleAsyncTaskExecutor
Create a new SimpleAsyncTaskExecutor with the given external thread factory.- Parameters:
threadFactory
- the factory to use for creating new Threads
-
-
Method Details
-
setVirtualThreads
public void setVirtualThreads(boolean virtual) Switch this executor to virtual threads. Requires Java 21 or higher.The default is
false
, indicating platform threads. Set this flag totrue
in order to create virtual threads instead.- Since:
- 6.1
-
setThreadFactory
Specify an external factory to use for creating new Threads, instead of relying on the local properties of this executor.You may specify an inner ThreadFactory bean or also a ThreadFactory reference obtained from JNDI (on a Jakarta EE server) or some other lookup mechanism.
-
getThreadFactory
Return the external factory to use for creating new Threads, if any. -
setTaskDecorator
Specify a customTaskDecorator
to be applied to anyRunnable
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 plainRunnable
execution viaexecute
calls. In case of#submit
calls, the exposedRunnable
will be aFutureTask
which does not propagate any exceptions; you might have to cast it and callFuture#get
to evaluate exceptions.- Since:
- 4.3
-
setConcurrencyLimit
public void setConcurrencyLimit(int concurrencyLimit) Set the maximum number of parallel accesses allowed. -1 indicates no concurrency limit at all.In principle, this limit can be changed at runtime, although it is generally designed as a config time setting. NOTE: Do not switch between -1 and any concrete limit at runtime, as this will lead to inconsistent concurrency counts: A limit of -1 effectively turns off concurrency counting completely.
- See Also:
-
getConcurrencyLimit
public final int getConcurrencyLimit()Return the maximum number of parallel accesses allowed. -
isThrottleActive
public final boolean isThrottleActive()Return whether this throttle is currently active.- Returns:
true
if the concurrency limit for this instance is active- See Also:
-
execute
Executes the given task, within a concurrency throttle if configured (through the superclass's settings).- Specified by:
execute
in interfaceExecutor
- Specified by:
execute
in interfaceTaskExecutor
- Parameters:
task
- theRunnable
to execute (nevernull
)- See Also:
-
execute
Deprecated.Executes the given task, within a concurrency throttle if configured (through the superclass's settings).Executes urgent tasks (with 'immediate' timeout) directly, bypassing the concurrency throttle (if active). All other tasks are subject to throttling.
- Specified by:
execute
in interfaceAsyncTaskExecutor
- Parameters:
task
- theRunnable
to execute (nevernull
)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 areAsyncTaskExecutor.TIMEOUT_IMMEDIATE
orAsyncTaskExecutor.TIMEOUT_INDEFINITE
(the default as used byTaskExecutor.execute(Runnable)
).- See Also:
-
submit
Description copied from interface:AsyncTaskExecutor
Submit a Runnable task for execution, receiving a Future representing that task. The Future will return anull
result upon completion.As of 6.1, this method comes with a default implementation that delegates to
TaskExecutor.execute(Runnable)
.- Specified by:
submit
in interfaceAsyncTaskExecutor
- Parameters:
task
- theRunnable
to execute (nevernull
)- Returns:
- a Future representing pending completion of the task
-
submit
Description copied from interface:AsyncTaskExecutor
Submit a Callable task for execution, receiving a Future representing that task. The Future will return the Callable's result upon completion.As of 6.1, this method comes with a default implementation that delegates to
TaskExecutor.execute(Runnable)
.- Specified by:
submit
in interfaceAsyncTaskExecutor
- Parameters:
task
- theCallable
to execute (nevernull
)- Returns:
- a Future representing pending completion of the task
-
submitListenable
Description copied from interface:AsyncListenableTaskExecutor
Submit aRunnable
task for execution, receiving aListenableFuture
representing that task. The Future will return anull
result upon completion.- Specified by:
submitListenable
in interfaceAsyncListenableTaskExecutor
- Parameters:
task
- theRunnable
to execute (nevernull
)- Returns:
- a
ListenableFuture
representing pending completion of the task
-
submitListenable
Description copied from interface:AsyncListenableTaskExecutor
Submit aCallable
task for execution, receiving aListenableFuture
representing that task. The Future will return the Callable's result upon completion.- Specified by:
submitListenable
in interfaceAsyncListenableTaskExecutor
- Parameters:
task
- theCallable
to execute (nevernull
)- Returns:
- a
ListenableFuture
representing pending completion of the task
-
doExecute
Template method for the actual execution of a task.The default implementation creates a new Thread and starts it.
- Parameters:
task
- the Runnable to execute- See Also:
-