Use a ScheduledExecutorService from Executors.newScheduledThreadPool(n) and call schedule for a one-time delayed task, scheduleAtFixedRate for a task that starts on a fixed clock, or scheduleWithFixedDelay for a task that waits a fixed time after the previous run finishes. Each call returns a ScheduledFuture that you can use to cancel the task. Always call shutdown() when you are done, otherwise the non-daemon pool threads keep the JVM alive.
ScheduledExecutorService lives in java.util.concurrent and replaces the older Timer and TimerTask. It runs tasks on a pool of threads, so one slow task does not stall the others, and an uncaught exception in one task does not kill the whole scheduler.
The Executors factory has three common ways to create a scheduler:
| Factory method | Threads | Use it when |
|---|---|---|
Executors.newScheduledThreadPool(int corePoolSize) | Fixed number | Several independent scheduled jobs |
Executors.newSingleThreadScheduledExecutor() | One | Jobs that must never run at the same time |
Executors.newScheduledThreadPool(n, threadFactory) | Fixed number, custom threads | You want named or daemon threads |
Since Java 21 you can also pass a virtual-thread factory (Thread.ofVirtual().factory()) to the scheduler when the scheduled tasks mostly block on I/O. The scheduling itself is still done by the executor, so you keep the same API.
Named threads make thread dumps and logs much easier to read.
schedule(Runnable, long, TimeUnit) runs a task one time after the delay. The Callable overload returns a value through the ScheduledFuture.
Both methods repeat a task, but they measure time differently.
| Method | Next run starts | Typical use |
|---|---|---|
scheduleAtFixedRate(task, initialDelay, period, unit) | period after the previous run started | Heartbeats, metrics, clock-aligned jobs |
scheduleWithFixedDelay(task, initialDelay, delay, unit) | delay after the previous run finished | Polling, cleanup, retries |
With a fixed rate, if a run takes longer than the period, the next run starts late as soon as the previous one ends. Runs of the same task never overlap, even with a large pool, because the executor does not start the next execution until the previous one completes. The scheduler may run a burst of catch-up executions only in the sense that late runs follow each other immediately.
With scheduleAtFixedRate the task starts roughly every 1.5 seconds, because each run takes longer than the 1 second period. With scheduleWithFixedDelay it starts roughly every 2.5 seconds (1.5 seconds of work plus the 1 second delay).
A periodic task that throws an exception stops being scheduled. The exception is stored in the ScheduledFuture and nothing is printed, so the job silently dies. Catch exceptions inside the task.
This is the main practical advantage over Timer, where an uncaught exception terminates the single timer thread and cancels every other task.
schedule* returns a ScheduledFuture. Call cancel(false) to stop future runs without interrupting a run in progress, or cancel(true) to interrupt it.
By default, shutdown() on a ScheduledThreadPoolExecutor cancels pending periodic tasks and still runs pending one-shot delayed tasks. Use shutdownNow() to also interrupt running tasks and drop the queue. If cancelled tasks pile up in the queue, ScheduledThreadPoolExecutor.setRemoveOnCancelPolicy(true) removes them immediately.
ScheduledExecutorService for in-process timing: retries, polling, cache refresh, timeouts.@Scheduled with @EnableScheduling runs on a scheduler under the hood and supports cron expressions, which the JDK executor does not.scheduleAtFixedRate is based on System.nanoTime, so wall-clock changes do not shift it, but it also cannot fire at "every day at 02:00". Compute the initial delay yourself or use cron-capable tooling for that.Fixed rate measures the period from the start of one run to the start of the next. Fixed delay measures from the end of one run to the start of the next. Use fixed delay when runs must not crowd each other.
No. The executor never runs two executions of one periodic task at the same time. A slow run delays the next one instead.
An exception escaped the task, which cancels future runs. Wrap the body in a try-catch and log the error.
The pool threads are non-daemon, so the JVM waits for them. Call shutdown(), or create the pool with a daemon thread factory.
No. Timer uses a single thread and dies on an unchecked exception. Prefer ScheduledExecutorService.
Create a ScheduledExecutorService with Executors.newScheduledThreadPool, use schedule for delayed work, scheduleAtFixedRate for clock-based repetition and scheduleWithFixedDelay for gaps between runs. Catch exceptions inside periodic tasks, keep the returned ScheduledFuture to cancel, and always shut the scheduler down. Reach for Spring @Scheduled or Quartz when you need cron or persistence.