问题描述
ThreadPoolTaskScheduler
实际上也实现了 Spring 的 TaskExecutor
接口,这样单个实例就可以尽快用于异步执行,并且可以按计划进行,并且可能会重复执行,处决.
那么在哪些场景中我们希望使用 ThreadPoolTaskExecutor
实例而不是 ThreadPoolTaskScheduler
实例?
So which are the scenarios where we would want to use ThreadPoolTaskExecutor
instance over ThreadPoolTaskScheduler
instance?
我目前正在使用 Spring XML.我正在创建 ThreadPoolTaskScheduler
的 bean,如下所示:
I am using currently using Spring XML. I am creating bean of ThreadPoolTaskScheduler
as follows:
<task:scheduler id="myScheduler" pool-size="1"/>
虽然 ThreadPoolTaskExecutor
实例的 bean 可以创建为
while bean of ThreadPoolTaskExecutor
instance can be created as
<task:executor id="executor" pool-size="10"/>
推荐答案
ThreadPoolTaskExecutor
是一个专门用于执行任务的类.ThreadPoolTaskScheduler
是一个专门用于调度任务的类.
你在 Spring 文档中引用的那句话只是说你可以使用调度器来执行任务,但这不是它的主要目的.ThreadPoolTaskExecutor
通过其 corePoolSize
, maxPoolSize
, keepAliveSeconds
和 queueCapacity
属性.ThreadPoolTaskScheduler
等调度程序不提供此类配置.
The sentence you quoted in the Spring documentation is only saying that you can use a scheduler to execute tasks, but that it is not its main purpose. A ThreadPoolTaskExecutor
provides fine-grained configuration over the thread pool through its corePoolSize
, maxPoolSize
, keepAliveSeconds
and queueCapacity
properties. A scheduler such as ThreadPoolTaskScheduler
does not provide such configuration.
因此,在两者之间进行选择会产生以下问题:我是否需要执行或安排任务的执行?
As such, choosing between the two comes down the following question: do I need to execute or schedule execution of tasks?
这篇关于Spring ThreadPoolTaskScheduler 与 ThreadPoolTaskExecutor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!