0
0
Spring Bootframework~10 mins

Scheduled tasks with @Scheduled in Spring Boot - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Scheduled tasks with @Scheduled
Application starts
Spring scans for @Scheduled
Schedule task setup
Wait for trigger time
Run scheduled method
Task completes
Wait for next trigger
Loop back to Run scheduled method
Spring Boot starts, finds methods marked with @Scheduled, sets timers, and runs those methods repeatedly at set times.
Execution Sample
Spring Boot
@Scheduled(fixedRate = 2000)
public void sayHello() {
  System.out.println("Hello every 2 seconds");
}
This method prints a message every 2 seconds automatically.
Execution Table
StepTime (ms)ActionOutputNext Trigger
10Application starts, schedules sayHello()2000
22000sayHello() runsHello every 2 seconds4000
34000sayHello() runsHello every 2 seconds6000
46000sayHello() runsHello every 2 seconds8000
58000sayHello() runsHello every 2 seconds10000
610000sayHello() runsHello every 2 seconds12000
712000Application stops or continues scheduling
💡 Application stops or user interrupts; scheduled tasks stop running.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
Next Trigger (ms)20004000600080001000012000Stopped or continues
Key Moments - 3 Insights
Why does the scheduled method run automatically without being called?
Because Spring detects @Scheduled and sets timers to call the method at fixed intervals (see execution_table steps 2-6).
What happens if the method takes longer than the fixedRate to run?
Spring will still trigger the next run based on fixedRate timing, possibly causing overlap or delays depending on configuration.
How does Spring know when to run the method next?
Spring calculates the next trigger time after each run, shown in the 'Next Trigger' column in execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output at step 4?
AHello every 2 seconds
BNo output
CError message
DHello every 4 seconds
💡 Hint
Check the 'Output' column at step 4 in the execution_table.
At which step does the next trigger time become 8000 ms?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Next Trigger' column in the execution_table for the value 8000.
If fixedRate is changed to 1000 ms, how would the 'Next Trigger' values change?
AThey would stay the same
BThey would increase by 1000 ms each step
CThey would increase by 4000 ms each step
DThey would decrease
💡 Hint
Compare the current fixedRate of 2000 ms with the new 1000 ms in variable_tracker.
Concept Snapshot
@Scheduled annotation runs methods automatically on a schedule.
Use fixedRate to run repeatedly every set milliseconds.
Spring sets timers and calls the method without manual calls.
Next trigger time updates after each run.
Useful for tasks like cleanup, notifications, or polling.
Full Transcript
When a Spring Boot application starts, it looks for methods marked with @Scheduled. It sets timers to run these methods automatically at fixed intervals, like every 2 seconds. The method runs without needing to be called manually. After each run, Spring calculates the next time to run the method again. This continues until the application stops or the scheduling is disabled. This helps automate repeated tasks easily.