Task Execution and Scheduling
上下文内不存在 Executor
Bean 时,Spring Boot 会自动配置一个 AsyncTaskExecutor
。当启用虚拟线程(使用 Java 21+ 并且将 configprop:spring.threads.virtual.enabled[] 设置为 true
)时,它将是使用虚拟线程的 SimpleAsyncTaskExecutor
。否则,它将是具有合理默认值的 ThreadPoolTaskExecutor
。无论哪种情况,都会自动将自动配置的执行程序用于:
In the absence of an Executor
bean in the context, Spring Boot auto-configures an AsyncTaskExecutor
.
When virtual threads are enabled (using Java 21+ and configprop:spring.threads.virtual.enabled[] set to true
) this will be a SimpleAsyncTaskExecutor
that uses virtual threads.
Otherwise, it will be a ThreadPoolTaskExecutor
with sensible defaults.
In either case, the auto-configured executor will be automatically used for:
-
asynchronous task execution (
@EnableAsync
) -
Spring for GraphQL’s asynchronous handling of
Callable
return values from controller methods -
Spring MVC’s asynchronous request processing
-
Spring WebFlux’s blocking execution support
如果您已经定义了上下文中自定义的 If you have defined a custom 自动配置的 The auto-configured |
当自动配置 ThreadPoolTaskExecutor
时,线程池使用 8 个核心线程,这些线程可以根据负载增长和缩减。这些默认设置可以使用 spring.task.execution
命名空间进行微调,如下例所示:
When a ThreadPoolTaskExecutor
is auto-configured, the thread pool uses 8 core threads that can grow and shrink according to the load.
Those default settings can be fine-tuned using the spring.task.execution
namespace, as shown in the following example:
spring: task: execution: pool: max-size: 16 queue-capacity: 100 keep-alive: "10s"
这更改了线程池,以便使用一个有界队列,因此当队列已满(100 个任务)时,线程池将增加到最多 16 个线程。线程池的缩减会更加激进,因为当线程空闲 10 秒(而不是默认的 60 秒)时,它们会被回收。
This changes the thread pool to use a bounded queue so that when the queue is full (100 tasks), the thread pool increases to maximum 16 threads. Shrinking of the pool is more aggressive as threads are reclaimed when they are idle for 10 seconds (rather than 60 seconds by default).
也可以自动配置调度程序,如果它需要与计划任务执行(例如,使用 @EnableScheduling
)关联的话。
A scheduler can also be auto-configured if it needs to be associated with scheduled task execution (using @EnableScheduling
for instance).
如果启用了虚拟线程(使用 Java 21+ 和将 configprop:spring.threads.virtual.enabled[] 设置为 true
),这将是一个使用虚拟线程的 SimpleAsyncTaskScheduler
。这个 SimpleAsyncTaskScheduler
将忽略任何与池相关的属性。
If virtual threads are enabled (using Java 21+ and configprop:spring.threads.virtual.enabled[] set to true
) this will be a SimpleAsyncTaskScheduler
that uses virtual threads.
This SimpleAsyncTaskScheduler
will ignore any pooling related properties.
如果未启用虚拟线程,它将是一个具有明智默认值的 ThreadPoolTaskScheduler
。默认情况下 ThreadPoolTaskScheduler
使用一个线程,并且可以使用 spring.task.scheduling
命名空间来微调其设置,如下例所示:
If virtual threads are not enabled, it will be a ThreadPoolTaskScheduler
with sensible defaults.
The ThreadPoolTaskScheduler
uses one thread by default and its settings can be fine-tuned using the spring.task.scheduling
namespace, as shown in the following example:
spring: task: scheduling: thread-name-prefix: "scheduling-" pool: size: 2
如果需要创建自定义 executor 或调度程序,会在上下文中提供一个 ThreadPoolTaskExecutorBuilder
bean、一个 SimpleAsyncTaskExecutorBuilder
bean、一个 ThreadPoolTaskSchedulerBuilder
bean 和一个 SimpleAsyncTaskSchedulerBuilder
。如果启用了虚拟线程,则 SimpleAsyncTaskExecutorBuilder
和 SimpleAsyncTaskSchedulerBuilder
bean 会自动配置为使用虚拟线程(使用 Java 21+ 和将 configprop:spring.threads.virtual.enabled[] 设置为 true
)。
A ThreadPoolTaskExecutorBuilder
bean, a SimpleAsyncTaskExecutorBuilder
bean, a ThreadPoolTaskSchedulerBuilder
bean and a SimpleAsyncTaskSchedulerBuilder
are made available in the context if a custom executor or scheduler needs to be created.
The SimpleAsyncTaskExecutorBuilder
and SimpleAsyncTaskSchedulerBuilder
beans are auto-configured to use virtual threads if they are enabled (using Java 21+ and configprop:spring.threads.virtual.enabled[] set to true
).