Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

What is the difference between scheduleAtFixedRate and scheduleWithFixedDelay, and what happens if a scheduled task throws an exception?

scheduleAtFixedRate starts runs on a fixed timetable measured from the initial time, so with a period of 2 seconds it aims for 0, 2, 4 and so on; if a run takes longer than the period, the next one starts late, but runs of the same task never overlap. scheduleWithFixedDelay waits the given delay after each run finishes. If any run throws an exception, all later runs are silently suppressed: nothing is logged, and the exception only sits in the returned ScheduledFuture, so we wrap the task body in try/catch and log.

Why this question is asked

Periodic tasks are everywhere in services: refreshing a cache, cleaning expired sessions, polling for new files. ScheduledExecutorService is the JDK's basic tool for them, and its two scheduling methods look almost the same. Interviewers ask about the difference, and especially about exceptions, because the exception behaviour causes real incidents: a task that silently stops forever. In this answer, we will recap ScheduledExecutorService, compare the two methods with timestamps, look at what happens when a run is slower than the period, and then see what an exception does and how to keep the schedule alive.

1A Quick Recap of ScheduledExecutorService

ScheduledExecutorService is an executor that can run a task after a delay or repeatedly. We usually create one with Executors.newScheduledThreadPool(n) or build a ScheduledThreadPoolExecutor directly. Internally it keeps scheduled tasks in a queue ordered by the time each should next run, and its threads take tasks from the front when their time arrives.

For a repeating task, the executor runs it, computes when it should run next, and puts it back into the queue. The two methods in the question differ only in how they compute that next time.

2Fixed Rate Versus Fixed Delay, With Timestamps

Let us schedule a task that takes 1 second, once with a fixed rate of 2 seconds and once with a fixed delay of 2 seconds, and print when each run starts.

Java
ScheduledExecutorService ses = Executors.newScheduledThreadPool(2);
Runnable oneSecondJob = () -> { log("start"); sleep(1000); };
ses.scheduleAtFixedRate(oneSecondJob, 0, 2, TimeUnit.SECONDS);
// starts at 0 s, 2 s, 4 s, 6 s ... (start-to-start is 2 s)
ses.scheduleWithFixedDelay(oneSecondJob, 0, 2, TimeUnit.SECONDS);
// starts at 0 s, 3 s, 6 s, 9 s ... (end-to-start is 2 s: 1 s of work + 2 s of delay)
Start times for a 1 s task, period or delay 2 s
RunscheduleAtFixedRatescheduleWithFixedDelay
10 s0 s
22 s3 s
34 s6 s
46 s9 s

With a fixed rate, the next start is the previous scheduled start plus the period. The executor keeps a timetable, and the run time does not shift it. With a fixed delay, the next start is the moment the previous run finished plus the delay, so the gap between runs is always the same, and the run time adds to the cycle.

So the choice comes from the kind of task. Fixed rate fits work that should happen on a clock, such as sampling a metric every 10 seconds. Fixed delay fits work that needs a rest between runs, such as polling a queue or an API, where we never want to start a new poll right after a slow one.

3When a Run Takes Longer Than the Period

Now suppose the task takes 3 seconds with a fixed rate of 2 seconds. The executor never starts the same task again while a run is still going; runs of one scheduled task do not overlap, even with many threads in the pool. So the next run starts late, right after the previous one ends.

Java
ses.scheduleAtFixedRate(threeSecondJob, 0, 2, TimeUnit.SECONDS);
// starts at 0 s, 3 s, 6 s, 9 s ... each run is late, back to back, no overlap
// A single slow run on a 2 s schedule (8 s once, then 1 s):
// starts at 0 s, 8 s, 9 s, 10 s, 11 s, 12 s ... catching up on the missed slots

Notice the second example. Because a fixed-rate task keeps its timetable, the next scheduled times after a long run are already in the past, so the following runs start immediately one after the other until the task has caught up. That can surprise us after a long garbage collection pause or a slow dependency: a burst of runs. A fixed-delay task never does this, because it always measures from the end of the last run.

4What Happens When a Run Throws

Here is the part that causes incidents. If any run of a periodic task throws an exception, the executor does not schedule it again. The documentation says subsequent executions are suppressed. Nothing is printed, because the exception is not passed to the thread's uncaught exception handler; it is caught and stored in the task's ScheduledFuture, the object the schedule method returned.

Java
AtomicInteger runs = new AtomicInteger();
ScheduledFuture<?> f = ses.scheduleAtFixedRate(() -> {
int n = runs.incrementAndGet();
System.out.println("run " + n);
if (n == 3) throw new IllegalStateException("bad data");
}, 0, 1, TimeUnit.SECONDS);
// run 1
// run 2
// run 3
// ... silence forever, no stack trace anywhere
f.isDone(); // true: the periodic task is finished
f.get(); // throws ExecutionException, cause: IllegalStateException: bad data

So the exception is not lost, but nobody looks at the future of a periodic task, because it normally never completes. In practice the task simply stops and the logs are clean.

5Keeping the Schedule Alive

The fix is to make sure no exception ever escapes the task body. We wrap the body in try/catch, log the error with its stack trace, and return normally, so the executor schedules the next run as usual.

Java
ses.scheduleAtFixedRate(() -> {
try {
refreshCache();
} catch (Throwable t) { // nothing may escape, or the schedule dies
log.error("cache refresh failed; will retry next run", t);
}
}, 0, 1, TimeUnit.MINUTES);
// run fails at 10:03 -> logged, next run at 10:04 as planned

Catching Throwable at this boundary also covers Errors such as a StackOverflowError from a bad input. Some teams prefer to catch Exception and let Errors stop the task, on the grounds that the JVM may be in a bad state; either way, the decision should be explicit. Spring's @Scheduled methods behave differently: Spring wraps repeating tasks with an error handler that logs the exception and keeps the schedule running, but check the behaviour of your Spring version and scheduler.

6Follow-up questions

Can runs of the same fixed-rate task overlap if the pool has several threads?

No. A periodic task is only put back into the queue after its current run finishes, so the same task never runs twice at once, however many threads the pool has. Extra threads let different scheduled tasks run in parallel.

What happens to periodic tasks when the executor shuts down?

By default, ScheduledThreadPoolExecutor cancels periodic tasks on shutdown() but still runs delayed one-shot tasks that are already queued. Both behaviours can be changed with setContinueExistingPeriodicTasksAfterShutdownPolicy and setExecuteExistingDelayedTasksAfterShutdownPolicy.

How can you tell from outside that a periodic task has died?

Its ScheduledFuture reports isDone() as true, which never happens to a healthy periodic task. More practically, publish a metric with the time of the last successful run and alert when it is older than a few periods.

7Conclusion

In this answer, we saw that scheduleAtFixedRate keeps a timetable from the initial time, starting late without overlapping when a run is slow and catching up afterwards, while scheduleWithFixedDelay always waits the full delay after each run ends. We also saw the important trap: an exception in any run silently suppresses all later runs and is only stored in the ScheduledFuture, so the task body must catch and log everything to keep the schedule alive.

What to say in the interviewAbout 90 seconds aloud

Open with

Both methods run a task repeatedly, and the difference is how they compute the next start. scheduleAtFixedRate keeps a timetable from the initial time: with a period of two seconds it aims for zero, two, four, six.

Then, if they want more

If one run takes longer than the period, the next run starts late, right after it, and runs of the same task never overlap, even with several threads.

Because it keeps the timetable, after one long run it can fire a few runs back to back to catch up. scheduleWithFixedDelay measures from the end of each run: it waits the delay after a run finishes, so a one-second task with a two-second delay starts at zero, three, six.

I use fixed rate for clock-like work such as sampling, and fixed delay for polling, where I want a rest between runs. The important part is exceptions.

If any run throws, the executor suppresses all later runs, and nothing is logged by default; the exception is stored in the ScheduledFuture that the schedule method returned, and nobody usually calls get on it. So the task just stops silently.

To prevent that, I wrap the body in try/catch, usually catching Throwable at that boundary, log it with the stack trace, and return normally so the next run happens. I would also add a last-success timestamp metric. Spring's @Scheduled logs and continues for repeating tasks, but I would still check the version's behaviour.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What is the difference between scheduleAtFixedRate and scheduleWithFixedDelay, and what...": https://scrivio-interview-library.netlify.app/java/executors/a8b55c9894/