Spring ThreadPoolTask​​Scheduler 与 ThreadPoolTask​​Executor [英] Spring ThreadPoolTaskScheduler vs ThreadPoolTaskExecutor

查看:156
本文介绍了Spring ThreadPoolTask​​Scheduler 与 ThreadPoolTask​​Executor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Spring 文档中提到了这一点 即:

ThreadPoolTask​​Scheduler 实际上也实现了 Spring 的 TaskExecutor 接口,这样单个实例就可以尽快用于异步执行,并且可以按计划进行,并且可能会重复执行,处决.

ThreadPoolTaskScheduler actually implements Spring's TaskExecutor interface as well, so that a single instance can be used for asynchronous execution as soon as possible as well as scheduled, and potentially recurring, executions.

那么在哪些场景中我们希望使用 ThreadPoolTask​​Executor 实例而不是 ThreadPoolTask​​Scheduler 实例?

So which are the scenarios where we would want to use ThreadPoolTaskExecutor instance over ThreadPoolTaskScheduler instance?

我目前正在使用 Spring XML.我正在创建 ThreadPoolTask​​Scheduler 的 bean,如下所示:

I am using currently using Spring XML. I am creating bean of ThreadPoolTaskScheduler as follows:

<task:scheduler id="myScheduler" pool-size="1"/>

虽然 ThreadPoolTask​​Executor 实例的 bean 可以创建为

while bean of ThreadPoolTaskExecutor instance can be created as

<task:executor id="executor" pool-size="10"/>

推荐答案

  • ThreadPoolTask​​Executor 是一个专门用于执行任务的类.
  • ThreadPoolTask​​Scheduler 是一个专门用于调度任务的类.
  • 你在 Spring 文档中引用的那句话只是说你可以使用调度器来执行任务,但这不是它的主要目的.ThreadPoolTask​​Executor 通过其 corePoolSize, maxPoolSize, keepAliveSecondsqueueCapacity 属性.ThreadPoolTask​​Scheduler 等调度程序不提供此类配置.

    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 ThreadPoolTask​​Scheduler 与 ThreadPoolTask​​Executor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆