类 SimpleAsyncTaskExecutor
- 所有已实现的接口:
AsyncListenableTaskExecutor,AsyncTaskExecutor,TaskExecutor,Serializable,AutoCloseable,Executor
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(long),
at the expense of task tracking overhead per execution thread at runtime.
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. Alternatively, on JDK 21,
consider setting setVirtualThreads(boolean) to true.
- 从以下版本开始:
- 4.0
- 作者:
- Juergen Hoeller, Harry Yang
- 另请参阅:
-
嵌套类概要
嵌套类修饰符和类型类说明private static classSubclass of the general ConcurrencyThrottleSupport class, makingbeforeAccess()andafterAccess()visible to the surrounding class.private classDecorates a target task with active thread tracking and concurrency throttle management, if necessary. -
字段概要
字段修饰符和类型字段说明private booleanprivate final SimpleAsyncTaskExecutor.ConcurrencyThrottleAdapterInternal concurrency throttle used by this executor.static final intSwitch concurrency 'off': that is, don't allow any concurrent invocations.private TaskDecoratorprivate longprivate ThreadFactorystatic final intPermit any number of concurrent invocations: that is, don't throttle concurrency.private VirtualThreadDelegate从接口继承的字段 cn.taketoday.core.task.AsyncTaskExecutor
TIMEOUT_IMMEDIATE, TIMEOUT_INDEFINITE -
构造器概要
构造器构造器说明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. -
方法概要
修饰符和类型方法说明voidclose()This close methods tracks the termination of active threads if a concretetask termination timeouthas been set.protected voidTemplate method for the actual execution of a task.voidExecutes the given task, within a concurrency throttle if configured (through the superclass's settings).voidExecutes the given task, within a concurrency throttle if configured (through the superclass's settings).final intReturn the maximum number of parallel task executions allowed.final ThreadFactoryReturn the external factory to use for creating new Threads, if any.booleanisActive()Return whether this executor is still active, i.e. not closed yet, and therefore accepts further task submissions.final booleanReturn whether the concurrency throttle is currently active.protected ThreadCreate a new Thread for the given task.voidsetConcurrencyLimit(int concurrencyLimit) Set the maximum number of parallel task executions allowed.final voidsetTaskDecorator(TaskDecorator taskDecorator) Specify a customTaskDecoratorto be applied to anyRunnableabout to be executed.voidsetTaskTerminationTimeout(long timeout) Specify a timeout for task termination when closing this executor.voidsetThreadFactory(ThreadFactory threadFactory) Specify an external factory to use for creating new Threads, instead of relying on the local properties of this executor.voidsetVirtualThreads(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 aRunnabletask for execution, receiving aListenableFuturerepresenting that task.<T> ListenableFuture<T>submitListenable(Callable<T> task) Submit aCallabletask for execution, receiving aListenableFuturerepresenting that task.从类继承的方法 cn.taketoday.util.CustomizableThreadCreator
createThread, getDefaultThreadNamePrefix, getThreadGroup, getThreadNamePrefix, getThreadPriority, isDaemon, nextThreadName, setDaemon, setThreadGroup, setThreadGroupName, setThreadNamePrefix, setThreadPriority从类继承的方法 java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait从接口继承的方法 cn.taketoday.core.task.AsyncTaskExecutor
submitCompletable, submitCompletable
-
字段详细资料
-
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. -
concurrencyThrottle
Internal concurrency throttle used by this executor. -
virtualThreadDelegate
-
threadFactory
-
taskDecorator
-
taskTerminationTimeout
private long taskTerminationTimeout -
activeThreads
-
active
private volatile boolean active
-
-
构造器详细资料
-
SimpleAsyncTaskExecutor
public SimpleAsyncTaskExecutor()Create a new SimpleAsyncTaskExecutor with default thread name prefix. -
SimpleAsyncTaskExecutor
Create a new SimpleAsyncTaskExecutor with the given thread name prefix.- 参数:
threadNamePrefix- the prefix to use for the names of newly created threads
-
SimpleAsyncTaskExecutor
Create a new SimpleAsyncTaskExecutor with the given external thread factory.- 参数:
threadFactory- the factory to use for creating new Threads
-
-
方法详细资料
-
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 totruein order to create virtual threads instead. -
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 customTaskDecoratorto be applied to anyRunnableabout to be executed.Note that such a decorator is not necessarily being applied to the user-supplied
Runnable/Callablebut 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
TaskDecoratorimplementations is limited to plainRunnableexecution viaexecutecalls. In case of#submitcalls, the exposedRunnablewill be aFutureTaskwhich does not propagate any exceptions; you might have to cast it and callFuture#getto evaluate exceptions. -
setTaskTerminationTimeout
public void setTaskTerminationTimeout(long timeout) Specify a timeout 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.
- 参数:
timeout- the timeout in milliseconds- 另请参阅:
-
close()cn.taketoday.scheduling.concurrent.ExecutorConfigurationSupport#setAwaitTerminationMillis
-
isActive
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. -
setConcurrencyLimit
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.
- 另请参阅:
-
UNBOUNDED_CONCURRENCYcn.taketoday.scheduling.concurrent.ThreadPoolTaskExecutor#setMaxPoolSize
-
getConcurrencyLimit
public final int getConcurrencyLimit()Return the maximum number of parallel task executions allowed. -
isThrottleActive
public final boolean isThrottleActive()Return whether the concurrency throttle is currently active.- 返回:
trueif the concurrency limit for this instance is active- 另请参阅:
-
execute
Executes the given task, within a concurrency throttle if configured (through the superclass's settings).- 指定者:
execute在接口中Executor- 指定者:
execute在接口中TaskExecutor- 参数:
task- theRunnableto execute (nevernull)- 另请参阅:
-
execute
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.
- 指定者:
execute在接口中AsyncTaskExecutor- 参数:
task- theRunnableto 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_IMMEDIATEorAsyncTaskExecutor.TIMEOUT_INDEFINITE(the default as used byTaskExecutor.execute(Runnable)).- 另请参阅:
-
submit
从接口复制的说明:AsyncTaskExecutorSubmit a Runnable task for execution, receiving a Future representing that task. The Future will return anullresult upon completion.this method comes with a default implementation that delegates to
TaskExecutor.execute(Runnable).- 指定者:
submit在接口中AsyncTaskExecutor- 参数:
task- theRunnableto execute (nevernull)- 返回:
- a Future representing pending completion of the task
-
submit
从接口复制的说明:AsyncTaskExecutorSubmit a Callable task for execution, receiving a Future representing that task. The Future will return the Callable's result upon completion.this method comes with a default implementation that delegates to
TaskExecutor.execute(Runnable).- 指定者:
submit在接口中AsyncTaskExecutor- 参数:
task- theCallableto execute (nevernull)- 返回:
- a Future representing pending completion of the task
-
submitListenable
从接口复制的说明:AsyncListenableTaskExecutorSubmit aRunnabletask for execution, receiving aListenableFuturerepresenting that task. The Future will return anullresult upon completion.in favor of
AsyncTaskExecutor.submitCompletable(Runnable)- 指定者:
submitListenable在接口中AsyncListenableTaskExecutor- 参数:
task- theRunnableto execute (nevernull)- 返回:
- a
ListenableFuturerepresenting pending completion of the task
-
submitListenable
从接口复制的说明:AsyncListenableTaskExecutorSubmit aCallabletask for execution, receiving aListenableFuturerepresenting that task. The Future will return the Callable's result upon completion.in favor of
AsyncTaskExecutor.submitCompletable(Callable)- 指定者:
submitListenable在接口中AsyncListenableTaskExecutor- 参数:
task- theCallableto execute (nevernull)- 返回:
- a
ListenableFuturerepresenting 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.
- 参数:
task- the Runnable to execute- 另请参阅:
-
newThread
Create a new Thread for the given task.- 参数:
task- the Runnable to create a Thread for- 返回:
- the new Thread instance
- 另请参阅:
-
close
public void close()This close methods tracks the termination of active threads if a concretetask termination timeouthas been set. Otherwise, it is not necessary to close this executor.- 指定者:
close在接口中AutoCloseable
-