Class SimpleAsyncTaskScheduler
- All Implemented Interfaces:
Serializable, AutoCloseable, Executor, EventListener, Aware, ApplicationContextAware, ApplicationListener<ContextClosedEvent>, Lifecycle, Phased, SmartLifecycle, AsyncTaskExecutor, TaskExecutor, TaskScheduler
TaskScheduler interface, using
a single scheduler thread and executing every scheduled task in an individual
separate thread. This is an attractive choice with virtual threads on JDK 21,
expecting common usage with setVirtualThreads(true).
NOTE: Scheduling with a fixed delay enforces execution on a single scheduler thread, in order to provide traditional fixed-delay semantics! Prefer the use of fixed rates or cron triggers instead which are a better fit with this thread-per-task scheduler variant.
Supports a graceful shutdown through SimpleAsyncTaskExecutor.setTaskTerminationTimeout(long),
at the expense of task tracking overhead per execution thread at runtime.
Supports limiting concurrent threads through SimpleAsyncTaskExecutor.setConcurrencyLimit(int).
By default, the number of concurrent task executions is unlimited.
This allows for dynamic concurrency of scheduled task executions, in contrast
to ThreadPoolTaskScheduler which requires a fixed pool size.
NOTE: This implementation does not reuse threads! Consider a
thread-pooling TaskScheduler implementation instead, in particular for
scheduling a large number of short-lived tasks. Alternatively, on JDK 21,
consider setting SimpleAsyncTaskExecutor.setVirtualThreads(boolean) to true.
Extends SimpleAsyncTaskExecutor and can serve as a fully capable
replacement for it, for example, as a single shared instance serving as a
TaskExecutor as well as a TaskScheduler.
This is generally not the case with other executor/scheduler implementations
which tend to have specific constraints for the scheduler thread pool,
requiring a separate thread pool for general executor purposes in practice.
NOTE: This scheduler variant does not track the actual completion of tasks
but rather just the hand-off to an execution thread. As a consequence,
a ScheduledFuture handle (for example, from schedule(Runnable, Instant))
represents that hand-off rather than the actual completion of the provided task
(or series of repeated tasks). Also, this scheduler participates in lifecycle
management to a limited degree only, stopping trigger firing and fixed-delay
task execution but not stopping the execution of handed-off tasks.
As an alternative to the built-in thread-per-task capability, this scheduler
can also be configured with a separate target executor for scheduled task
execution through setTargetTaskExecutor(Executor): for example, pointing to a shared
ThreadPoolTaskExecutor bean. This is still rather different from a
ThreadPoolTaskScheduler setup since it always uses a single scheduler
thread while dynamically dispatching to the target thread pool which may have
a dynamic core/max pool size range, participating in a shared concurrency limit.
- Since:
- 6.1
- Author:
- Juergen Hoeller
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe default phase for an executorSmartLifecycle:Integer.MAX_VALUE / 2.Fields inherited from class SimpleAsyncTaskExecutor
NO_CONCURRENCY, UNBOUNDED_CONCURRENCYFields inherited from interface AsyncTaskExecutor
TIMEOUT_IMMEDIATE, TIMEOUT_INDEFINITE -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()This close method tracks the termination of active threads if a concretetask termination timeouthas been set.protected voidTemplate method for the actual execution of a task.voidExecutes the given task, within a concurrency throttle if configured (through the superclass's settings).getClock()Return the clock to use for scheduling purposes.intgetPhase()Return the lifecycle phase for pausing and resuming this executor.booleanCheck whether this component is currently running.voidHandle an application event.Schedule the givenRunnable, invoking it at the specified execution time.Schedule the givenRunnable, invoking it whenever the trigger indicates a next execution time.scheduleAtFixedRate(Runnable task, Duration period) Schedule the givenRunnable, starting as soon as possible and invoking it with the given period.scheduleAtFixedRate(Runnable task, Instant startTime, Duration period) Schedule the givenRunnable, invoking it at the specified execution time and subsequently with the given period.scheduleWithFixedDelay(Runnable task, Duration delay) Schedule the givenRunnable, starting as soon as possible and invoking it with the given delay between the completion of one execution and the start of the next.scheduleWithFixedDelay(Runnable task, Instant startTime, Duration delay) Schedule the givenRunnable, 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.voidsetApplicationContext(ApplicationContext applicationContext) Set the ApplicationContext that this object runs in.voidSet the clock to use for scheduling purposes.voidsetErrorHandler(ErrorHandler errorHandler) Provide anErrorHandlerstrategy.voidsetPhase(int phase) Specify the lifecycle phase for pausing and resuming this executor.voidsetTargetTaskExecutor(Executor targetTaskExecutor) Specify a custom targetExecutorto delegate to for the individual execution of scheduled tasks.voidstart()Start this component.void