The TaskScheduler interface is Spring's abstraction for running tasks at a future time or repeatedly, for example once at a given instant, at a fixed rate, with a fixed delay or on a cron schedule. It lets you schedule work without touching ScheduledExecutorService directly, and it is the engine behind the @Scheduled annotation.
The standard implementation is ThreadPoolTaskScheduler. Spring Boot auto-configures one when you enable scheduling, and in Spring 6 the scheduling methods take Instant and Duration instead of Date and milliseconds.
TaskScheduler is the scheduling counterpart of TaskExecutor. Its main methods are:
| Method | Behaviour |
|---|---|
schedule(Runnable, Trigger) | Runs according to a Trigger, such as CronTrigger |
schedule(Runnable, Instant) | Runs once at the given instant |
scheduleAtFixedRate(Runnable, Duration) | Runs every period, measured from the start of each run |
scheduleAtFixedRate(Runnable, Instant, Duration) | Same, starting at a given instant |
scheduleWithFixedDelay(Runnable, Duration) | Waits the delay after each run finishes |
scheduleWithFixedDelay(Runnable, Instant, Duration) | Same, with a start instant |
Each call returns a ScheduledFuture, which you use to cancel the task. The older overloads that accept Date and long milliseconds are deprecated in favour of the Instant and Duration forms.
The default pool size is 1, which means a long-running task delays every other scheduled task. Set a size that matches the number of independent jobs. Spring Boot's auto-configured scheduler uses spring.task.scheduling.pool.size, which also defaults to 1.
Spring cron expressions have six fields: second, minute, hour, day of month, month and day of week. This differs from the five-field Unix format.
For static schedules, annotate a method and enable scheduling:
@Scheduled methods must be void (or ignore the result) and take no arguments. Use zone for cron triggers so a server timezone change cannot move your job.
With scheduleAtFixedRate, a slow run can cause the next one to start immediately after, and on a single-thread scheduler runs queue up rather than overlap. With scheduleWithFixedDelay, runs never overlap because the delay starts after completion. Choose delay when the task must not run concurrently with itself.
In a cluster, every instance runs the schedule. Use a distributed lock such as ShedLock or a database check if the job must run once.
TaskExecutor runs tasks as soon as a thread is free. TaskScheduler runs them at a time or repeatedly according to a trigger.
Yes. ScheduledAnnotationBeanPostProcessor registers each annotated method with a TaskScheduler.
The default scheduler has a single thread. Increase the pool size or set spring.task.scheduling.pool.size.
Yes, in Spring Boot 3.2 and later with Java 21, spring.threads.virtual.enabled=true makes the auto-configured scheduler use virtual threads for running tasks.
TaskScheduler runs work once, at a fixed rate, with a fixed delay or on a cron trigger. Use ThreadPoolTaskScheduler with a pool size above one, prefer the Instant and Duration methods, and remember that clustered applications need external coordination for single-run jobs.