Top Description Inners Fields Constructors Methods
org.springframework.core.task

public Class SimpleAsyncTaskExecutor

extends CustomizableThreadCreator
implements AsyncListenableTaskExecutor, Serializable, AutoCloseable
Class Inheritance
All Implemented Interfaces
java.lang.AutoCloseable, java.io.Serializable, org.springframework.core.task.AsyncListenableTaskExecutor, org.springframework.core.task.AsyncTaskExecutor, org.springframework.core.task.TaskExecutor, java.util.concurrent.Executor
Annotations
@SuppressWarnings:serial, deprecation
Imports
java.io.Serializable, java.util.Collections, .Set, java.util.concurrent.Callable, .ConcurrentHashMap, .Future, .FutureTask, .ThreadFactory, org.springframework.lang.Nullable, org.springframework.util.Assert, .ConcurrencyThrottleSupport, .CustomizableThreadCreator, org.springframework.util.concurrent.ListenableFuture, .ListenableFutureTask

Deprecated

as a consequence of AsyncListenableTaskExecutor being deprecated. See corresponding docs for further information.

TaskExecutor implementation that fires up a new Thread for each task, executing it asynchronously. Provides a virtual thread option on JDK 21.

Supports a graceful shutdown through setTaskTerminationTimeout, at the expense of task tracking overhead per execution thread at runtime. Supports limiting concurrent threads through setConcurrencyLimit. 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. Alternatively, on JDK 21, consider setting setVirtualThreads to true.

Author
Juergen Hoeller
Since
2.0
See Also
setVirtualThreads, setTaskTerminationTimeout, setConcurrencyLimit, org.springframework.scheduling.concurrent.SimpleAsyncTaskScheduler, org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
SimpleAsyncTaskExecutor.ConcurrencyThrottleAdapter

Subclass of the general ConcurrencyThrottleSupport class, making beforeAccess() and afterAccess() visible to the surrounding class.

private class
SimpleAsyncTaskExecutor.TaskTrackingRunnable

Decorates a target task with active thread tracking and concurrency throttle management, if necessary.

Field Summary

Modifier and TypeField and Description
private volatile boolean
private Set<Thread>
private final SimpleAsyncTaskExecutor.ConcurrencyThrottleAdapter
concurrencyThrottle

Internal concurrency throttle used by this executor.

public static final int
NO_CONCURRENCY

Switch concurrency 'off': that is, don't allow any concurrent invocations.

private TaskDecorator
private long
private ThreadFactory
public static final int
UNBOUNDED_CONCURRENCY

Permit any number of concurrent invocations: that is, don't throttle concurrency.

private VirtualThreadDelegate

Constructor Summary

AccessConstructor and Description
public
SimpleAsyncTaskExecutor()

Create a new SimpleAsyncTaskExecutor with default thread name prefix.

public
SimpleAsyncTaskExecutor(String
the prefix to use for the names of newly created threads
threadNamePrefix
)

Create a new SimpleAsyncTaskExecutor with the given thread name prefix.

public
SimpleAsyncTaskExecutor(ThreadFactory
the factory to use for creating new Threads
threadFactory
)

Create a new SimpleAsyncTaskExecutor with the given external thread factory.

Method Summary

Modifier and TypeMethod and Description
public void
close()

Implements java.lang.AutoCloseable.close.

This close methods tracks the termination of active threads if a concrete task termination timeout has been set.
protected void
doExecute(Runnable
the Runnable to execute
task
)

Template method for the actual execution of a task.

public void
execute(Runnable
the Runnable to execute (never null)
task
)

Implements org.springframework.core.task.TaskExecutor.execute.

Executes the given task, within a concurrency throttle if configured (through the superclass's settings).
public void
execute(Runnable
the Runnable to execute (never null)
task
,
long
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 TIMEOUT_IMMEDIATE or TIMEOUT_INDEFINITE (the default as used by execute(Runnable)).
startTimeout
)

Overrides default org.springframework.core.task.AsyncTaskExecutor.execute.

Deprecated
Executes the given task, within a concurrency throttle if configured (through the superclass's settings).
public final int
getConcurrencyLimit()

Return the maximum number of parallel task executions allowed.

public final ThreadFactory
getThreadFactory()

Return the external factory to use for creating new Threads, if any.

public boolean
isActive()

Return whether this executor is still active, i.e. not closed yet, and therefore accepts further task submissions.

public final boolean

Returns:

true if the concurrency limit for this instance is active
isThrottleActive
()

Return whether the concurrency throttle is currently active.

protected Thread

Returns:

the new Thread instance
newThread
(Runnable
the Runnable to create a Thread for
task
)

Create a new Thread for the given task.

public void
setConcurrencyLimit(int concurrencyLimit)

Set the maximum number of parallel task executions allowed.

public void
setTaskDecorator(TaskDecorator taskDecorator)

Specify a custom TaskDecorator to be applied to any Runnable about to be executed.

public void
setTaskTerminationTimeout(long
the timeout in milliseconds
timeout
)

Specify a timeout (in milliseconds) for task termination when closing this executor.

public void
setThreadFactory(ThreadFactory threadFactory)

Specify an external factory to use for creating new Threads, instead of relying on the local properties of this executor.

public void
setVirtualThreads(boolean virtual)

Switch this executor to virtual threads.

public Future<?>
submit(Runnable
the Runnable to execute (never null)
task
)

Overrides default org.springframework.core.task.AsyncTaskExecutor.submit.

Submit a Runnable task for execution, receiving a Future representing that task.
public <T> Future<T>
submit(Callable<T>
the Callable to execute (never null)
task
)

Overrides default org.springframework.core.task.AsyncTaskExecutor.submit.

Submit a Callable task for execution, receiving a Future representing that task.
public ListenableFuture<?>
submitListenable(Runnable
the Runnable to execute (never null)
task
)

Implements org.springframework.core.task.AsyncListenableTaskExecutor.submitListenable.

Deprecated as a consequence of ListenableFuture being deprecated.
Submit a Runnable task for execution, receiving a ListenableFuture representing that task.
public <T> ListenableFuture<T>
submitListenable(Callable<T>
the Callable to execute (never null)
task
)

Implements org.springframework.core.task.AsyncListenableTaskExecutor.submitListenable.

Deprecated as a consequence of ListenableFuture being deprecated.
Submit a Callable task for execution, receiving a ListenableFuture representing that task.
Inherited from org.springframework.util.CustomizableThreadCreator:
createThreadgetDefaultThreadNamePrefixgetThreadGroupgetThreadNamePrefixgetThreadPriorityisDaemonnextThreadNamesetDaemonsetThreadGroupsetThreadGroupNamesetThreadNamePrefixsetThreadPriority

Field Detail

activeback to summary
private volatile boolean active
activeThreadsback to summary
private Set<Thread> activeThreads
Annotations
@Nullable
concurrencyThrottleback to summary
private final SimpleAsyncTaskExecutor.ConcurrencyThrottleAdapter concurrencyThrottle

Internal concurrency throttle used by this executor.

NO_CONCURRENCYback to summary
public static final int NO_CONCURRENCY

Switch concurrency 'off': that is, don't allow any concurrent invocations.

See Also
ConcurrencyThrottleSupport#NO_CONCURRENCY
taskDecoratorback to summary
private TaskDecorator taskDecorator
Annotations
@Nullable
taskTerminationTimeoutback to summary
private long taskTerminationTimeout
threadFactoryback to summary
private ThreadFactory threadFactory
Annotations
@Nullable
UNBOUNDED_CONCURRENCYback to summary
public static final int UNBOUNDED_CONCURRENCY

Permit any number of concurrent invocations: that is, don't throttle concurrency.

See Also
ConcurrencyThrottleSupport#UNBOUNDED_CONCURRENCY
virtualThreadDelegateback to summary
private VirtualThreadDelegate virtualThreadDelegate
Annotations
@Nullable

Constructor Detail

SimpleAsyncTaskExecutorback to summary
public SimpleAsyncTaskExecutor()

Create a new SimpleAsyncTaskExecutor with default thread name prefix.

SimpleAsyncTaskExecutorback to summary
public SimpleAsyncTaskExecutor(String threadNamePrefix)

Create a new SimpleAsyncTaskExecutor with the given thread name prefix.

Parameters
threadNamePrefix:String

the prefix to use for the names of newly created threads

SimpleAsyncTaskExecutorback to summary
public SimpleAsyncTaskExecutor(ThreadFactory threadFactory)

Create a new SimpleAsyncTaskExecutor with the given external thread factory.

Parameters
threadFactory:ThreadFactory

the factory to use for creating new Threads

Method Detail

closeback to summary
public void close()

Implements java.lang.AutoCloseable.close.

This close methods tracks the termination of active threads if a concrete task termination timeout has been set. Otherwise, it is not necessary to close this executor.

Annotations
@Override
Since
6.1
doExecuteback to summary
protected void doExecute(Runnable task)

Template method for the actual execution of a task.

The default implementation creates a new Thread and starts it.

Parameters
task:Runnable

the Runnable to execute

See Also
newThread, Thread#start()
executeback to summary
public void execute(Runnable task)

Implements org.springframework.core.task.TaskExecutor.execute.

Executes the given task, within a concurrency throttle if configured (through the superclass's settings).

Parameters
task:Runnable

Doc from org.springframework.core.task.TaskExecutor.execute.

the Runnable to execute (never null)

Annotations
@SuppressWarnings:deprecation
@Override
See Also
doExecute(Runnable)
executeback to summary
public void execute(Runnable task, long startTimeout)

Overrides default org.springframework.core.task.AsyncTaskExecutor.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.

Parameters
task:Runnable

Doc from org.springframework.core.task.AsyncTaskExecutor.execute.

the Runnable to execute (never null)

startTimeout:long

Doc from org.springframework.core.task.AsyncTaskExecutor.execute.

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 TIMEOUT_IMMEDIATE or TIMEOUT_INDEFINITE (the default as used by execute(Runnable)).

Annotations
@Deprecated
@Override
See Also
TIMEOUT_IMMEDIATE, doExecute(Runnable)
getConcurrencyLimitback to summary
public final int getConcurrencyLimit()

Return the maximum number of parallel task executions allowed.

getThreadFactoryback to summary
public final ThreadFactory getThreadFactory()

Return the external factory to use for creating new Threads, if any.

Annotations
@Nullable
isActiveback to summary
public boolean isActive()

Return whether this executor is still active, i.e. not closed yet, and therefore accepts further task submissions. Otherwise, it is either in the task termination phase or entirely shut down already.

Since
6.1
See Also
setTaskTerminationTimeout, close()
isThrottleActiveback to summary
public final boolean isThrottleActive()

Return whether the concurrency throttle is currently active.

Returns:boolean

true if the concurrency limit for this instance is active

See Also
getConcurrencyLimit(), setConcurrencyLimit
newThreadback to summary
protected Thread newThread(Runnable task)

Create a new Thread for the given task.

Parameters
task:Runnable

the Runnable to create a Thread for

Returns:Thread

the new Thread instance

Since
6.1
See Also
setVirtualThreads, setThreadFactory, createThread
setConcurrencyLimitback to summary
public void setConcurrencyLimit(int concurrencyLimit)

Set the maximum number of parallel task executions allowed. The default of -1 indicates no concurrency limit at all.

This is the equivalent of a maximum pool size in a thread pool, preventing temporary overload of the thread management system.

See Also
UNBOUNDED_CONCURRENCY, org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor#setMaxPoolSize
setTaskDecoratorback to summary
public void setTaskDecorator(TaskDecorator taskDecorator)

Specify a custom 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.

Since
4.3
setTaskTerminationTimeoutback to summary
public void setTaskTerminationTimeout(long timeout)

Specify a timeout (in milliseconds) for task termination when closing this executor. The default is 0, not waiting for task termination at all.

Note that a concrete >0 timeout specified here will lead to the wrapping of every submitted task into a task-tracking runnable which involves considerable overhead in case of a high number of tasks. However, for a modest level of submissions with longer-running tasks, this is feasible in order to arrive at a graceful shutdown.

Note that SimpleAsyncTaskExecutor does not participate in a coordinated lifecycle stop but rather just awaits task termination on close().

Parameters
timeout:long

the timeout in milliseconds

Since
6.1
See Also
close(), org.springframework.scheduling.concurrent.ExecutorConfigurationSupport#setAwaitTerminationMillis
setThreadFactoryback to summary
public void setThreadFactory(ThreadFactory threadFactory)

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.

See Also
setThreadNamePrefix, setThreadPriority
setVirtualThreadsback to summary
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 to true in order to create virtual threads instead.

Since
6.1
submitback to summary
public Future<?> submit(Runnable task)

Overrides default org.springframework.core.task.AsyncTaskExecutor.submit.

Doc from org.springframework.core.task.AsyncTaskExecutor.submit.

Submit a Runnable task for execution, receiving a Future representing that task. The Future will return a null result upon completion.

As of 6.1, this method comes with a default implementation that delegates to execute(Runnable).

Parameters
task:Runnable

the Runnable to execute (never null)

Returns:Future<?>

a Future representing pending completion of the task

Annotations
@SuppressWarnings:deprecation
@Override
submitback to summary
public <T> Future<T> submit(Callable<T> task)

Overrides default org.springframework.core.task.AsyncTaskExecutor.submit.

Doc from org.springframework.core.task.AsyncTaskExecutor.submit.

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 execute(Runnable).

Parameters
task:Callable<T>

the Callable to execute (never null)

Returns:Future<T>

a Future representing pending completion of the task

Annotations
@SuppressWarnings:deprecation
@Override
submitListenableback to summary
public ListenableFuture<?> submitListenable(Runnable task)

Implements org.springframework.core.task.AsyncListenableTaskExecutor.submitListenable.

Doc from org.springframework.core.task.AsyncListenableTaskExecutor.submitListenable.

Deprecated

as a consequence of ListenableFuture being deprecated. See corresponding docs for further information.

Submit a Runnable task for execution, receiving a ListenableFuture representing that task. The Future will return a null result upon completion.

Parameters
task:Runnable

the Runnable to execute (never null)

Returns:ListenableFuture<?>

a ListenableFuture representing pending completion of the task

Annotations
@SuppressWarnings:deprecation
@Override
submitListenableback to summary
public <T> ListenableFuture<T> submitListenable(Callable<T> task)

Implements org.springframework.core.task.AsyncListenableTaskExecutor.submitListenable.

Doc from org.springframework.core.task.AsyncListenableTaskExecutor.submitListenable.

Deprecated

as a consequence of ListenableFuture being deprecated. See corresponding docs for further information.

Submit a Callable task for execution, receiving a ListenableFuture representing that task. The Future will return the Callable's result upon completion.

Parameters
task:Callable<T>

the Callable to execute (never null)

Returns:ListenableFuture<T>

a ListenableFuture representing pending completion of the task

Annotations
@SuppressWarnings:deprecation
@Override
org.springframework.core.task back to summary

private Class SimpleAsyncTaskExecutor.ConcurrencyThrottleAdapter

extends ConcurrencyThrottleSupport
Class Inheritance

Subclass of the general ConcurrencyThrottleSupport class, making beforeAccess() and afterAccess() visible to the surrounding class.

Field Summary

Inherited from org.springframework.util.ConcurrencyThrottleSupport:
loggerNO_CONCURRENCYUNBOUNDED_CONCURRENCY

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
protected void
afterAccess()

Overrides org.springframework.util.ConcurrencyThrottleSupport.afterAccess.

To be invoked after the main execution logic of concrete subclasses.
protected void
beforeAccess()

Overrides org.springframework.util.ConcurrencyThrottleSupport.beforeAccess.

To be invoked before the main execution logic of concrete subclasses.
Inherited from org.springframework.util.ConcurrencyThrottleSupport:
getConcurrencyLimitisThrottleActivesetConcurrencyLimit

Constructor Detail

ConcurrencyThrottleAdapterback to summary
private ConcurrencyThrottleAdapter()

Method Detail

afterAccessback to summary
protected void afterAccess()

Overrides org.springframework.util.ConcurrencyThrottleSupport.afterAccess.

Doc from org.springframework.util.ConcurrencyThrottleSupport.afterAccess.

To be invoked after the main execution logic of concrete subclasses.

Annotations
@Override
beforeAccessback to summary
protected void beforeAccess()

Overrides org.springframework.util.ConcurrencyThrottleSupport.beforeAccess.

Doc from org.springframework.util.ConcurrencyThrottleSupport.beforeAccess.

To be invoked before the main execution logic of concrete subclasses.

This implementation applies the concurrency throttle.

Annotations
@Override
org.springframework.core.task back to summary

private Class SimpleAsyncTaskExecutor.TaskTrackingRunnable

extends Object
implements Runnable
Class Inheritance
  • java.lang.Object
  • org.springframework.core.task.SimpleAsyncTaskExecutor.TaskTrackingRunnable
All Implemented Interfaces
java.lang.Runnable

Decorates a target task with active thread tracking and concurrency throttle management, if necessary.

Field Summary

Modifier and TypeField and Description
private final Runnable

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
run()

Implements java.lang.Runnable.run.

Runs this operation.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

taskback to summary
private final Runnable task

Constructor Detail

TaskTrackingRunnableback to summary
public TaskTrackingRunnable(Runnable task)

Method Detail

runback to summary
public void run()

Implements java.lang.Runnable.run.

Doc from java.lang.Runnable.run.

Runs this operation.

Annotations
@Override