@Scheduled(fixedRate = ...) tells Spring to run a method repeatedly, starting a new run every fixed interval measured from the start of the previous run. A method with fixedRate = 5000 is triggered every 5 seconds whether the last run took 10 milliseconds or 4 seconds. It is the right choice for periodic work such as cache refreshes, polling and heartbeats, where you want a steady cadence.
Despite a common belief, Spring does not run the same @Scheduled method in parallel with itself. If a run is still going when the next one is due, the next run waits and starts as soon as the previous one finishes, so executions can bunch up but not overlap.
Scheduling is off until you add @EnableScheduling to a configuration class. The annotated method must take no arguments, and any return value is ignored, so use void.
The method runs once at startup (no initial delay unless you set one) and then every 5 seconds. The bean can be any Spring-managed bean, not only @Component.
The three trigger styles differ in what they measure.
| Attribute | Interval measured from | Typical use |
|---|---|---|
fixedRate | Start of one run to start of the next | Regular sampling, heartbeats |
fixedDelay | End of one run to start of the next | Work where runs must not crowd each other |
cron | A calendar expression | Daily or weekly jobs at a fixed time |
For a task that takes 2 seconds with a 5 second interval, fixedRate starts runs at 0, 5, 10 seconds, while fixedDelay starts them at 0, 7, 14 seconds. Pick fixedDelay when you need breathing room after every run, for example a worker that drains a queue.
With a single scheduler thread, which is the default, a slow run delays the next one. Spring does not start a second copy of the same method, and it does not skip missed runs permanently: the next run begins as soon as the thread is free, then the schedule continues from there. If the task averages longer than the interval, it simply runs back to back.
Different @Scheduled methods share the scheduler's threads. With the default pool of one thread, a long task blocks every other scheduled method. Spring Boot lets you raise the pool size:
With a pool of 4, different methods can run at the same time, but a given method still does not overlap with itself. On Spring Boot 3.2 and later with Java 21, spring.threads.virtual.enabled=true switches scheduling to a virtual-thread based scheduler, which suits tasks that mostly wait on I/O.
You can delay the first run, change the time unit and read the interval from configuration instead of hard-coding it.
The timeUnit attribute exists since Spring Framework 5.3.10. The string variants (fixedRateString, initialDelayString) accept property placeholders and SpEL, which lets you change cadence per environment without recompiling.
@EnableScheduling. Test the method body directly and keep timing out of unit tests.spring.task.scheduling.shutdown.await-termination=true so running tasks can finish when the application stops.fixedRate measures the interval between the start times of consecutive runs. fixedDelay waits the given time after the previous run finishes. Choose fixedDelay if overlapping pressure matters.
No, not with the standard annotation-driven scheduler. A late run waits for the previous one to finish. To run work in parallel, hand the job to an executor from inside the method or add @Async.
Yes, unless you set initialDelay. The first run starts as soon as the scheduler is ready after the context refreshes.
The annotation value is fixed once the bean is created. For runtime changes use SchedulingConfigurer with ScheduledTaskRegistrar, or schedule jobs directly with a TaskScheduler.
@Scheduled(fixedRate) starts a method at a steady interval measured from each run's start time. Spring never overlaps a method with itself, so a slow run delays the next one instead of running in parallel. Enable it with @EnableScheduling, size the scheduler pool when you have several jobs, and add a distributed lock when the application runs on multiple instances.