Medium🚀 ScheduledExecutorService Explained Like a Backend Engineer | by curiouswomen | Medium
Sitemap
curiouswomen

curiouswomen

I am here to share my experience being it a technical, non-technical or daily life experience that I am curious and thrive a positive change.

🚀 ScheduledExecutorService Explained Like a Backend Engineer

4 min readJun 12, 2026

--

Why ScheduledExecutorService Exists

When developers first learn multithreading, they often try to execute recurring tasks using:

while(true){
performTask();
Thread.sleep(5000);
}

At first glance this seems fine.

However, this approach has several problems:

  • Dedicated thread remains occupied forever
  • Difficult to manage lifecycle
  • Hard to stop gracefully
  • Poor scalability
  • No centralized scheduling mechanism

The Java Concurrency team introduced ScheduledExecutorService in Java 5 to solve this problem.

Think of it as:

ExecutorService + Time Awareness

It not only executes tasks but also understands when tasks should execute.

Creating a Scheduled Thread Pool

ScheduledExecutorService scheduler =
Executors.newScheduledThreadPool(2);

Here:

  • Pool Size = 2
  • Supports delayed execution
  • Supports recurring execution

Think of it as hiring two employees and giving them a timetable.

1. schedule() — Execute Once After Delay

Example

ScheduledExecutorService scheduler =
Executors.newScheduledThreadPool(1);
scheduler.schedule(
() -> System.out.println("OTP Expired"),
30,
TimeUnit.SECONDS
);

Output after 30 seconds:

OTP Expired

Real World Example: OTP Expiration

When a user requests an OTP:

User Requests OTP
↓
Generate OTP
↓
Schedule Expiration After 5 Minutes

Instead of constantly checking expiration, we simply schedule the task.

Other Use Cases

  • Session Timeout
  • Temporary Cache Eviction
  • Delayed Notifications
  • Retry Mechanisms

2. scheduleAtFixedRate()

One of the most important methods in backend systems.

Syntax

scheduler.scheduleAtFixedRate(
task,
initialDelay,
period,
TimeUnit.SECONDS
);

Example:

scheduler.scheduleAtFixedRate(
() -> System.out.println("Health Check"),
0,
5,
TimeUnit.SECONDS
);

Timeline

0 sec   -> Run
5 sec -> Run
10 sec -> Run
15 sec -> Run
20 sec -> Run

Notice:

The scheduler tries to maintain a fixed clock schedule.

Real World Example: Service Health Monitoring

Microservices often need periodic checks.

Example:

Check Database Connection
Every 30 Seconds

Implementation:

scheduler.scheduleAtFixedRate(
() -> checkDatabase(),
0,
30,
TimeUnit.SECONDS
);

This ensures monitoring occurs at predictable intervals.

Understanding Fixed Rate Deeply

Suppose:

Period = 5 Seconds
Task Time = 2 Seconds

Timeline:

0 -> Start
2 -> Finish
5 -> Start
7 -> Finish
10 -> Start
12 -> Finish

Everything looks normal.

What Happens If Task Takes Longer?

Suppose:

Period = 5 Seconds
Task Duration = 8 Seconds

Timeline:

0 -> Start
8 -> Finish
8 -> Start Again
16 -> Finish
16 -> Start Again

Java does not run the same periodic task concurrently.

Instead, it starts the next execution immediately after completion to catch up.

Real Production Use Cases

Monitoring Systems

Check CPU Usage Every Minute

Heartbeat Signals

Send Heartbeat Every 10 Seconds

Metrics Collection

Collect JVM Metrics Every 30 Seconds

Service Discovery Updates

Refresh Service Registry Every Minute

3. scheduleWithFixedDelay()

Many developers confuse this with Fixed Rate.

Get curiouswomen’s stories in your inbox

Join Medium for free to get updates from this writer.

Let’s understand carefully.

Example

scheduler.scheduleWithFixedDelay(
() -> System.out.println("Processing Logs"),
0,
5,
TimeUnit.SECONDS
);

Timeline

Suppose task takes:

2 Seconds

Timeline:

0 -> Start
2 -> Finish
7 -> Start
9 -> Finish
14 -> Start
16 -> Finish

Notice the difference.

The scheduler waits:

Task Completes
↓
Wait 5 Seconds
↓
Run Again

Fixed Rate vs Fixed Delay

Fixed Rate

Run every 5 seconds

Timeline:

0
5
10
15
20

Based on a fixed clock.

Fixed Delay

Finish
↓
Wait 5 Seconds
↓
Run Again

Based on completion time.

The Flatmate Analogy

Imagine your mom says:

Fixed Rate

Wash utensils every hour.

Whether you finish early or late:

1 PM
2 PM
3 PM
4 PM

The schedule remains fixed.

Fixed Delay

Mom says:

After finishing utensils,
rest for one hour,
then wash again.

Now timing depends on when you completed the previous task.

Which One Should You Use?

Use scheduleAtFixedRate()

When timing matters.

Examples:

  • Monitoring
  • Health Checks
  • Heartbeats
  • Metrics Collection
  • Service Discovery Refresh

Goal:

Maintain Consistent Frequency

Use scheduleWithFixedDelay()

When spacing between executions matters.

Examples:

  • Log Processing
  • Batch Jobs
  • Cleanup Tasks
  • File Processing
  • Cache Maintenance

Goal:

Avoid Overlapping Pressure

Spring Boot Connection

Most Spring Boot developers use scheduling without realizing the underlying implementation.

Example:

@Scheduled(fixedRate = 5000)
public void monitorServices() {
}

Conceptually equivalent to:

scheduleAtFixedRate(...)

Similarly:

@Scheduled(fixedDelay = 5000)
public void processLogs() {
}

Conceptually equivalent to:

scheduleWithFixedDelay(...)

Shutting Down the Scheduler

Always shutdown gracefully.

scheduler.shutdown();

Wait for existing tasks to finish.

For immediate shutdown:

scheduler.shutdownNow();

Interrupts running tasks and stops queued tasks.

Interview Questions

Difference Between Fixed Rate and Fixed Delay?

Fixed Rate:

Based On Start Time

Fixed Delay:

Based On Completion Time

When Would You Use Fixed Rate?

Monitoring, Metrics, Heartbeats.

When Would You Use Fixed Delay?

Batch Jobs, Cleanup Jobs, Log Processing.

What Is ScheduledExecutorService?

An ExecutorService that supports delayed and periodic task execution.

Key Takeaways

  • schedule() → Execute once after delay
  • scheduleAtFixedRate() → Execute at fixed intervals
  • scheduleWithFixedDelay() → Execute after completion plus delay
  • Ideal for monitoring, batch processing, cleanup jobs, cache refresh, and recurring backend operations
  • Forms the conceptual foundation behind Spring Boot’s @Scheduled

Understanding ScheduledExecutorService is not about memorizing methods.

It is about understanding how modern backend systems automate recurring work without wasting threads or CPU resources.

--

--

curiouswomen

Written by curiouswomen

I am here to share my experience being it a technical, non-technical or daily life experience that I am curious and thrive a positive change.