0
0
Spring Bootframework~10 mins

Cron expressions for scheduling in Spring Boot - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Cron expressions for scheduling
Start
Define cron expression
Spring parses expression
Schedule task at specified times
Task runs
Wait for next scheduled time
Back to Schedule task
This flow shows how Spring Boot reads a cron expression, schedules a task, runs it at the right time, then waits for the next run.
Execution Sample
Spring Boot
@Scheduled(cron = "0 0/1 * * * ?")
public void runTask() {
    System.out.println("Task runs every minute");
}
This code schedules a task to run every minute using a cron expression.
Execution Table
StepCron Expression PartValueMeaningAction
1Seconds0At second 0Task triggers at start of minute
2Minutes0/1Every 1 minute starting at 0Task runs every minute
3Hours*Every hourNo hour restriction
4Day of Month*Every dayNo day restriction
5Month*Every monthNo month restriction
6Day of Week?No specific day of weekIgnored because day of month is *
7Year (optional)Not specifiedRuns every year
8Task runsN/AAt each matching timePrints "Task runs every minute"
9WaitN/AWaits until next minuteCycle repeats
10ExitN/ANever exitsRuns indefinitely
💡 Cron tasks run indefinitely until application stops
Variable Tracker
VariableStartAfter 1 minAfter 2 minAfter 3 minFinal
Current Time (minute)0123N/A
Task TriggeredNoYesYesYesN/A
Key Moments - 3 Insights
Why does the task run every minute and not every second?
Because the cron expression sets seconds to 0 and minutes to 0/1, so it triggers only at second 0 of every minute (see execution_table step 1 and 2).
What does the '?' symbol mean in the day of week field?
It means 'no specific value' and is used when day of month is specified as '*', so Spring ignores day of week (see execution_table step 6).
Why does the task never stop running?
Because cron schedules repeat indefinitely until the app stops (see execution_table step 10).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step does the task actually run?
AStep 2
BStep 8
CStep 6
DStep 10
💡 Hint
Check the 'Action' column for when the task prints output.
According to variable_tracker, what is the value of 'Task Triggered' after 2 minutes?
AMaybe
BNo
CYes
DUndefined
💡 Hint
Look at the 'After 2 min' column for 'Task Triggered'.
If the minutes part changed from '0/1' to '0/5', how would the execution table change?
ATask runs every 5 minutes instead of every minute
BTask runs every second
CTask runs only once
DTask runs every hour
💡 Hint
Look at step 2 where minutes are defined.
Concept Snapshot
Cron expressions schedule tasks by specifying time parts:
Seconds Minutes Hours DayOfMonth Month DayOfWeek Year(optional)
Use * for any value, / for intervals, ? for no specific value.
Spring Boot reads this to run tasks repeatedly at matching times.
Example: "0 0/1 * * * ?" runs task every minute at second 0.
Full Transcript
Cron expressions let you schedule tasks by defining when they run using six or seven parts: seconds, minutes, hours, day of month, month, day of week, and optionally year. In Spring Boot, you use @Scheduled with a cron string. For example, "0 0/1 * * * ?" means run at second 0, every 1 minute, every hour, every day, every month, ignoring day of week. The task runs at these times repeatedly until the app stops. The '?' means no specific value for day of week because day of month is '*'. This way, Spring parses the expression, schedules the task, runs it at the right times, then waits for the next scheduled time. Beginners often wonder why the task runs every minute and not every second; that is because seconds is fixed at 0. They also ask about '?', which means no specific value. Finally, cron tasks run indefinitely unless stopped.