Spring中ThreadPoolTaskExecutor的默认池类型

wfsdck30  于 2022-10-30  发布在  Spring
关注(0)|答案(2)|浏览(146)

我已经阅读了代码,但是没有找到ThreadPoolTaskExecutor的默认池类型。ThreadPoolTaskExecutor的默认线程池是什么?newFixedThreadPool还是newCachedThreadPool?

0h4hbjxa

0h4hbjxa1#

对于纯Spring应用程序,默认的TaskExecutor将按以下顺序解析(此处为源代码):
1.为org.springframework.core.task.TaskExecutor的Bean
1.为java.util.concurrent.Executor且名称为taskExecutor的Bean

  1. SimpleAsyncTaskExecutor
    因此,如果你不做任何配置,默认情况下它会使用SimpleAsyncTaskExecutor,它在内部不使用JDK的ThreadPoolExecutor来创建线程。因此,没有线程池,并且会为每个异步调用创建一个新线程。
    对于spring-boot应用程序,它将自动配置一个ThreadPoolTaskExecutor bean(docs)。由于ThreadPoolTaskExecutororg.springframework.core.task.TaskExecutor的一种类型,因此将按照上述顺序将其用作默认值。
    ThreadPoolTaskExecutor在内部使用JDK的ThreadPoolExecutor,但它既不使用newFixedThreadPool()也不使用newCachedThreadPool()来创建ThreadPoolExecutor。而是直接调用其构造函数来创建,然后根据应用程序属性配置其设置。(此处为源代码)
qv7cva1a

qv7cva1a2#

您可以看到***线程池任务执行程序***的java文档:

摘自Spring框架文档。
默认配置是核心池大小为1,具有无限的最大池大小和无限的队列容量。这大致相当于Executors.newSingleThreadExecutor(),为所有任务共享一个线程。

将**"queueCapacity"设置为0会模拟Executors.newCachedThreadPool(),并将池中的线程立即扩展到一个可能非常大的数字。还应考虑在此时设置"maxPoolSize",以及可能更高的"corePoolSize"**(另请参阅“allowCoreThreadTimeOut”扩展模式)。

三个执行者之间的区别:
新的固定线程池:创建一个线程池,该线程池重用在共享的无界队列中运行的固定数量的线程。在任何时候,最多有nThread个线程是活动的处理任务。如果在所有线程都处于活动状态时提交了其他任务,则这些任务将在队列中等待,直到有可用的线程。如果任何线程在关闭之前的执行过程中由于失败而终止,一个新的线程将取代它的位置。2池中的线程将一直存在直到它被明确关闭。
新的单线程执行程序:创建一个Executor,该Executor使用单个工作线程处理未绑定的队列。(然而,注意,如果该单个线程由于在关闭之前的执行期间的故障而终止,则如果需要执行后续任务,则新的线程将取代它的位置。)任务被保证顺序地执行,并且在任何给定时间都不会有多于一个的任务处于活动状态。(1)保证返回的执行器不能被重新配置以使用额外的线程。
新的缓存线程池:创建一个线程池,该线程池根据需要创建新线程,但在以前构造的线程可用时将重用它们。这些池通常会提高执行许多短期异步任务的程序的性能。要执行的调用将重用以前构造的线程(如果可用)。如果没有现有线程可用,一个新的线程将被创建并添加到池中。60秒内未被使用的线程将被终止并该高速缓存中删除。因此,一个闲置时间足够长的池将不会消耗任何资源。2请注意,可以使用ThreadPoolExecutor构造函数来创建具有相似属性但细节不同(例如,超时参数)的池。
Spring Boot子应用程序:自动配置Bean TaskExecutionAutoConfiguration的属性的默认值:


# Whether core threads are allowed to time out. This enables dynamic

growing and shrinking of the pool.
spring.task.execution.pool.allow-core-thread-timeout=true

# Core number of threads.

spring.task.execution.pool.core-size=8

# Time limit for which threads may remain idle before being terminated.

spring.task.execution.pool.keep-alive=60s

# Maximum allowed number of threads. If tasks are filling up the queue, the pool can expand up to that size to accommodate the load. Ignored if the queue is unbounded.

spring.task.execution.pool.max-size=

# Queue capacity. An unbounded capacity does not increase the pool and therefore ignores the "max-size" property.

spring.task.execution.pool.queue-capacity=

# Whether the executor should wait for scheduled tasks to complete on shutdown.

spring.task.execution.shutdown.await-termination=false

# Maximum time the executor should wait for remaining tasks to complete.

spring.task.execution.shutdown.await-termination-period=

# Prefix to use for the names of newly created threads.

spring.task.execution.thread-name-prefix=task-

# Maximum allowed number of threads.

spring.task.scheduling.pool.size=1

# Whether the executor should wait for scheduled tasks to complete on shutdown.

spring.task.scheduling.shutdown.await-termination=false

# Maximum time the executor should wait for remaining tasks to complete.

spring.task.scheduling.shutdown.await-termination-period

# Prefix to use for the names of newly created threads.

spring.task.scheduling.thread-name-prefix=scheduling-

相关问题