chammu
chammu

Reputation: 1305

Spring ThreadPoolTaskScheduler vs ThreadPoolTaskExecutor

It is mentioned in the Spring documentation that:

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.

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

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

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

while bean of ThreadPoolTaskExecutor instance can be created as

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

Upvotes: 29

Views: 20610

Answers (1)

Tunaki
Tunaki

Reputation: 137084

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?

Upvotes: 34

Related Questions