Clean • Professional
In Java, sometimes you need to run tasks after a delay or repeatedly at fixed intervals, like sending reminders, updating logs, or performing background cleanup.
The ScheduledExecutorService in Java makes this easy. It allows you to schedule tasks for future execution in a thread-safe and efficient way, without blocking your main thread.
Task Scheduling helps automate repetitive jobs, improve performance, and manage multiple tasks concurrently.
Task Scheduling is the process of executing tasks at a specific time or repeatedly. Java provides the ScheduledExecutorService in the java.util.concurrent package for this purpose.
Think of it like setting an alarm clock for your tasks:
schedule(), scheduleAtFixedRate(), or scheduleWithFixedDelay().Java provides three main ways to schedule tasks using the ScheduledExecutorService. Each type has a specific use case depending on how and when you want the task to run.

Syntax:
scheduler.schedule(task, delay, TimeUnit.SECONDS);
Example:
scheduler.schedule(() -> System.out.println("Task executed after 5 seconds"),5, TimeUnit.SECONDS);
Syntax:
scheduler.scheduleAtFixedRate(task, initialDelay, period, TimeUnit.SECONDS);
Example:
scheduler.scheduleAtFixedRate(() -> System.out.println("Task runs every 3 seconds"),2,3, TimeUnit.SECONDS);
—> Even if a task takes longer than the period, the next execution starts on schedule (may run concurrently if threads available).
Syntax:
scheduler.scheduleWithFixedDelay(task, initialDelay, delay, TimeUnit.SECONDS);
Example:
scheduler.scheduleWithFixedDelay(() -> System.out.println("Task runs with 4-second delay after previous completion"),1,4, TimeUnit.SECONDS);
—> Each execution waits for the previous task to complete before starting the delay timer.
The modern and preferred way to schedule tasks in Java is using ScheduledExecutorService from java.util.concurrent.
Key Methods:
schedule(Runnable command, long delay, TimeUnit unit) → Run once after a delayscheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) → Run repeatedly at a fixed ratescheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) → Run repeatedly with a fixed delay between task completionimport java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
publicclassTaskSchedulingExample {
publicstaticvoidmain(String[] args) {
ScheduledExecutorServicescheduler= Executors.newScheduledThreadPool(1);
Runnabletask= () -> System.out.println("Task executed at: " + System.currentTimeMillis());
// Schedule the task to run after 5 seconds
scheduler.schedule(task,5, TimeUnit.SECONDS);
scheduler.shutdown();
}
}
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
publicclassRepeatedTaskExample {
publicstaticvoidmain(String[] args) {
ScheduledExecutorServicescheduler= Executors.newScheduledThreadPool(1);
Runnabletask= () -> System.out.println("Task executed at: " + System.currentTimeMillis());
// Run task every 3 seconds after an initial delay of 2 seconds
scheduler.scheduleAtFixedRate(task,2,3, TimeUnit.SECONDS);
}
}
Explanation:
scheduleAtFixedRate() → Runs the task at a fixed interval, regardless of how long the task takes.scheduleWithFixedDelay() → Waits for the previous task to finish before starting the next one.You can also use Timer and TimerTask to schedule tasks, but this is less flexible and less thread-safe compared to ScheduledExecutorService.
import java.util.Timer;
import java.util.TimerTask;
publicclassTimerExample {
publicstaticvoidmain(String[] args) {
Timertimer=newTimer();
TimerTasktask=newTimerTask() {
@Override
publicvoidrun() {
System.out.println("Task executed at: " + System.currentTimeMillis());
}
};
// Schedule task to run every 2 seconds
timer.scheduleAtFixedRate(task,0,2000);
}
}
Task Scheduling in Java using ScheduledExecutorService is a powerful feature of the Java Concurrency API. It allows you to automate tasks, improve performance, and run multithreaded programs efficiently.
Whether you need delayed execution or periodic background tasks, using ScheduledExecutorService ensures your tasks run on time, reliably, and safely.