public class

ThreadPoolTaskScheduler

extends ExecutorConfigurationSupport
implements SchedulingTaskExecutor TaskScheduler
java.lang.Object
   ↳ org.springframework.util.CustomizableThreadCreator
     ↳ org.springframework.scheduling.concurrent.CustomizableThreadFactory
       ↳ org.springframework.scheduling.concurrent.ExecutorConfigurationSupport
         ↳ org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler

Class Overview

Implementation of Spring's TaskScheduler interface, wrapping a native java.util.concurrent.ScheduledThreadPoolExecutor.

Summary

[Expand]
Inherited Constants
From interface org.springframework.core.task.AsyncTaskExecutor
[Expand]
Inherited Fields
From class org.springframework.scheduling.concurrent.ExecutorConfigurationSupport
Public Constructors
ThreadPoolTaskScheduler()
Public Methods
void execute(Runnable task)
Execute the given task.
void execute(Runnable task, long startTimeout)
Execute the given task.
ScheduledExecutorService getScheduledExecutor()
Return the underlying ScheduledExecutorService for native access.
boolean prefersShortLivedTasks()
Does this TaskExecutor prefer short-lived tasks over long-lived tasks?

A SchedulingTaskExecutor implementation can indicate whether it prefers submitted tasks to perform as little work as they can within a single task execution.

ScheduledFuture schedule(Runnable task, Trigger trigger)
Schedule the given Runnable, invoking it whenever the trigger indicates a next execution time.
ScheduledFuture schedule(Runnable task, Date startTime)
Schedule the given Runnable, invoking it at the specified execution time.
ScheduledFuture scheduleAtFixedRate(Runnable task, Date startTime, long period)
Schedule the given Runnable, invoking it at the specified execution time and subsequently with the given period.
ScheduledFuture scheduleAtFixedRate(Runnable task, long period)
Schedule the given Runnable, starting as soon as possible and invoking it with the given period.
ScheduledFuture scheduleWithFixedDelay(Runnable task, Date startTime, long delay)
Schedule the given Runnable, invoking it at the specified execution time and subsequently with the given delay between the completion of one execution and the start of the next.
ScheduledFuture scheduleWithFixedDelay(Runnable task, long delay)
Schedule the given Runnable, starting as soon as possible and invoking it with the given delay between the completion of one execution and the start of the next.
void setErrorHandler(ErrorHandler errorHandler)
Provide an ErrorHandler strategy.
void setPoolSize(int poolSize)
Set the ScheduledExecutorService's pool size.
<T> Future<T> submit(Callable<T> task)
Submit a Callable task for execution, receiving a Future representing that task.
Future<?> submit(Runnable task)
Submit a Runnable task for execution, receiving a Future representing that task.
Protected Methods
ScheduledExecutorService createExecutor(int poolSize, ThreadFactory threadFactory, RejectedExecutionHandler rejectedExecutionHandler)
Create a new ScheduledExecutorService instance.
ExecutorService initializeExecutor(ThreadFactory threadFactory, RejectedExecutionHandler rejectedExecutionHandler)
Create the target ExecutorService instance.
[Expand]
Inherited Methods
From class org.springframework.scheduling.concurrent.ExecutorConfigurationSupport
From class org.springframework.scheduling.concurrent.CustomizableThreadFactory
From class org.springframework.util.CustomizableThreadCreator
From class java.lang.Object
From interface java.util.concurrent.Executor
From interface java.util.concurrent.ThreadFactory
From interface org.springframework.beans.factory.BeanNameAware
From interface org.springframework.beans.factory.DisposableBean
From interface org.springframework.beans.factory.InitializingBean
From interface org.springframework.core.task.AsyncTaskExecutor
From interface org.springframework.core.task.TaskExecutor
From interface org.springframework.scheduling.SchedulingTaskExecutor
From interface org.springframework.scheduling.TaskScheduler

Public Constructors

public ThreadPoolTaskScheduler ()

Public Methods

public void execute (Runnable task)

Execute the given task.

The call might return immediately if the implementation uses an asynchronous execution strategy, or might block in the case of synchronous execution.

Parameters
task the Runnable to execute (never null)

public void execute (Runnable task, long startTimeout)

Execute the given task.

Parameters
task the Runnable to execute (never null)
startTimeout the time duration (milliseconds) within which the task is supposed to start. This is intended as a hint to the executor, allowing for preferred handling of immediate tasks. Typical values are TIMEOUT_IMMEDIATE or TIMEOUT_INDEFINITE (the default as used by execute(Runnable)).

public ScheduledExecutorService getScheduledExecutor ()

Return the underlying ScheduledExecutorService for native access.

Returns
  • the underlying ScheduledExecutorService (never null)
Throws
IllegalStateException if the ThreadPoolTaskScheduler hasn't been initialized yet

public boolean prefersShortLivedTasks ()

Does this TaskExecutor prefer short-lived tasks over long-lived tasks?

A SchedulingTaskExecutor implementation can indicate whether it prefers submitted tasks to perform as little work as they can within a single task execution. For example, submitted tasks might break a repeated loop into individual subtasks which submit a follow-up task afterwards (if feasible).

This should be considered a hint. Of course TaskExecutor clients are free to ignore this flag and hence the SchedulingTaskExecutor interface overall. However, thread pools will usually indicated a preference for short-lived tasks, to be able to perform more fine-grained scheduling.

Returns
  • true if this TaskExecutor prefers short-lived tasks

public ScheduledFuture schedule (Runnable task, Trigger trigger)

Schedule the given Runnable, invoking it whenever the trigger indicates a next execution time.

Execution will end once the scheduler shuts down or the returned ScheduledFuture gets cancelled.

Parameters
task the Runnable to execute whenever the trigger fires
trigger an implementation of the Trigger interface, e.g. a CronTrigger object wrapping a cron expression
Returns

public ScheduledFuture schedule (Runnable task, Date startTime)

Schedule the given Runnable, invoking it at the specified execution time.

Execution will end once the scheduler shuts down or the returned ScheduledFuture gets cancelled.

Parameters
task the Runnable to execute whenever the trigger fires
startTime the desired execution time for the task (if this is in the past, the task will be executed immediately, i.e. as soon as possible)
Returns

public ScheduledFuture scheduleAtFixedRate (Runnable task, Date startTime, long period)

Schedule the given Runnable, invoking it at the specified execution time and subsequently with the given period.

Execution will end once the scheduler shuts down or the returned ScheduledFuture gets cancelled.

Parameters
task the Runnable to execute whenever the trigger fires
startTime the desired first execution time for the task (if this is in the past, the task will be executed immediately, i.e. as soon as possible)
period the interval between successive executions of the task (in milliseconds)
Returns

public ScheduledFuture scheduleAtFixedRate (Runnable task, long period)

Schedule the given Runnable, starting as soon as possible and invoking it with the given period.

Execution will end once the scheduler shuts down or the returned ScheduledFuture gets cancelled.

Parameters
task the Runnable to execute whenever the trigger fires
period the interval between successive executions of the task (in milliseconds)
Returns

public ScheduledFuture scheduleWithFixedDelay (Runnable task, Date startTime, long delay)

Schedule the given Runnable, invoking it at the specified execution time and subsequently with the given delay between the completion of one execution and the start of the next.

Execution will end once the scheduler shuts down or the returned ScheduledFuture gets cancelled.

Parameters
task the Runnable to execute whenever the trigger fires
startTime the desired first execution time for the task (if this is in the past, the task will be executed immediately, i.e. as soon as possible)
delay the delay between the completion of one execution and the start of the next (in milliseconds)
Returns

public ScheduledFuture scheduleWithFixedDelay (Runnable task, long delay)

Schedule the given Runnable, starting as soon as possible and invoking it with the given delay between the completion of one execution and the start of the next.

Execution will end once the scheduler shuts down or the returned ScheduledFuture gets cancelled.

Parameters
task the Runnable to execute whenever the trigger fires
delay the interval between successive executions of the task (in milliseconds)
Returns

public void setErrorHandler (ErrorHandler errorHandler)

Provide an ErrorHandler strategy.

public void setPoolSize (int poolSize)

Set the ScheduledExecutorService's pool size. Default is 1.

public Future<T> submit (Callable<T> task)

Submit a Callable task for execution, receiving a Future representing that task. The Future will return the Callable's result upon completion.

Parameters
task the Callable to execute (never null)
Returns
  • a Future representing pending completion of the task

public Future<?> submit (Runnable task)

Submit a Runnable task for execution, receiving a Future representing that task. The Future will return a null result upon completion.

Parameters
task the Runnable to execute (never null)
Returns
  • a Future representing pending completion of the task

Protected Methods

protected ScheduledExecutorService createExecutor (int poolSize, ThreadFactory threadFactory, RejectedExecutionHandler rejectedExecutionHandler)

Create a new ScheduledExecutorService instance.

The default implementation creates a ScheduledThreadPoolExecutor. Can be overridden in subclasses to provide custom ScheduledExecutorService instances.

Parameters
poolSize the specified pool size
threadFactory the ThreadFactory to use
rejectedExecutionHandler the RejectedExecutionHandler to use
Returns
  • a new ScheduledExecutorService instance
See Also

protected ExecutorService initializeExecutor (ThreadFactory threadFactory, RejectedExecutionHandler rejectedExecutionHandler)

Create the target ExecutorService instance. Called by afterPropertiesSet.

Parameters
threadFactory the ThreadFactory to use
rejectedExecutionHandler the RejectedExecutionHandler to use
Returns
  • a new ExecutorService instance