
Spring Boot @Scheduled Tutorial: Building Scheduled Tasks with TaskScheduler
Learn how to schedule tasks in Spring Boot using the @Scheduled annotation and TaskScheduler. This guide covers fixedRate, fixedDelay, cron expressions, async scheduling with @Async, custom thread pools, and best practices for building efficient scheduled tasks.

Munaf Badarpura
September 08, 2025
3 min read
Task scheduling is the process of executing methods or tasks at specified times or intervals, either once or repeatedly, without manual intervention. Spring Boot simplifies this with the @Scheduled
annotation and the TaskScheduler.
This enabling developers to automate tasks like logging, notifications, and data processing. This blog explores how to implement scheduled tasks in Spring Boot, including configuration, use cases, and best practices.
What is Task Scheduling in Spring Boot?#
Spring Boot provides built-in support for scheduling through the @Scheduled
annotation, which integrates with the TaskScheduler
interface. By default, it uses a ScheduledExecutorService
under the hood, leveraging a thread pool for task execution. This allows for efficient management of recurring or delayed tasks without manual thread handling.
Common Use Cases#
- Logging user activity in a separate thread.
- Sending emails or SMS notifications asynchronously after user actions.
- Processing multiple image uploads, database queries, or API calls in parallel.
- Cleaning up expired database records.
- Archiving logs.
- Fetching data from external APIs regularly.
- Performing regular health checks on external systems or services.
Setting Up Scheduled Tasks#
Steps to Implement#
- Enable Scheduling: Add
@EnableScheduling
to the main application class to activate Spring’s scheduling support. - Annotate the Method: Use
@Scheduled
on the method you want to schedule. - Method Requirements: The method must be
void
and take no arguments.
Example Configuration#
How @Scheduled Works#
When a method is annotated with @Scheduled
, Spring:
- Registers it as a scheduled task.
- Delegates execution to a
TaskScheduler
implementation. - Uses a thread pool (defaulting to
ScheduledExecutorService
) for concurrent task execution. - By default, employs a single-threaded executor, running tasks sequentially.
Customizing for Concurrency#
By default, tasks run sequentially. To enable parallel execution, configure a custom TaskScheduler
with a thread pool:
Asynchronous Task Scheduling#
Blocking Behavior#
By default, @Scheduled
tasks are blocking. For example, if a task takes 5 seconds and is scheduled every 2 seconds, it will only rerun after the previous execution completes. This can lead to delays.
Enabling Async Execution#
To avoid blocking, use the @Async
annotation:
- Add
@EnableAsync
to the main application class. - Annotate the scheduled method with
@Async
.
Example:
Note: @Async
is ignored if the method is called within the same class due to Spring’s proxy mechanism. Move the method to a separate class or use a proxy instance.
Benefits of @Async#
- Frees the main thread for other operations by offloading time-consuming tasks to a new thread.
- Improves responsiveness in applications with heavy scheduled workloads.
Best Practices#
- Configure Thread Pool Size: Adjust
poolSize
based on the number of concurrent tasks to optimize performance. - Handle Exceptions: Wrap task logic in try-catch blocks to prevent crashes.
- Use Appropriate Scheduling Types: Choose
fixedRate
,fixedDelay
, orcron
based on needs (e.g.,fixedRate
for regular intervals,cron
for complex schedules). - Test Scheduling: Verify task timing and concurrency in a non-production environment.
Conclusion#
Spring Boot’s @Scheduled
annotation, combined with TaskScheduler
and @Async
, provides a flexible and powerful way to implement scheduled tasks. Whether you need to log activities, send notifications, or perform health checks, this framework simplifies the process.
Want to Master Spring Boot and Land Your Dream Job?
Struggling with coding interviews? Learn Data Structures & Algorithms (DSA) with our expert-led course. Build strong problem-solving skills, write optimized code, and crack top tech interviews with ease
Learn more