Quick revision
The short answer
Most likely one run of the refresh task threw an unchecked exception, and ScheduledExecutorService responds by silently cancelling all future runs; the exception is stored in the ScheduledFuture that nobody inspects, so nothing reaches the logs. We prevent it by catching Throwable inside the task, logging it and returning normally, and by publishing the time of the last successful refresh with an alert when it becomes stale. If the task uses Spring's @Scheduled instead, check its error handler, because Spring logs and continues by default.
What to remember
In this answer, we found the most likely cause of the silent stop: a run threw an unchecked exception, ScheduledExecutorService suppressed every later run, and the exception was kept in a ScheduledFuture that nobody read. A thread dump separates that from a hung run. The fix is a wrapper that catches Throwable, logs it and returns, plus a last-success metric with a staleness alert that catches every cause. With Spring's @Scheduled, the default error handler logs and continues, but that is worth checking too.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Most likely one run of the refresh task threw an unchecked exception, and ScheduledExecutorService responds by silently cancelling all future runs; the exception is stored in the ScheduledFuture that nobody inspects, so nothing reaches the logs. We prevent it by catching Throwable inside the task, logging it and returning normally, and by publishing the time of the last successful refresh with an alert when it becomes stale. If the task uses Spring's @Scheduled instead, check its error handler, because Spring logs and continues by default.
Why this question is asked
A periodic task that stops without a trace is one of the more confusing problems in a Java service. The application is healthy, the endpoints respond, the logs are clean, and yet the cache has not been refreshed since three in the morning. Interviewers ask this debugging question to see whether we know how ScheduledExecutorService handles exceptions and whether we can prove the cause instead of guessing. In this answer, we will reproduce the problem, look at where the exception went, check the other possible cause, fix the task, and add the monitoring that would have caught it within minutes.
1Reproducing the Silent Stop
Here is a cache refresh similar to the one in the question. It loads prices from a downstream service every minute. One day, the service returns a price list with a null entry and the code throws a NullPointerException.
Notice that there is no error line in the log. The application keeps serving requests from the old cache, so everything looks healthy except that the data slowly goes out of date.
2Where the Exception Went
For a periodic task, ScheduledThreadPoolExecutor runs the task inside a FutureTask-like wrapper. When the run throws, the wrapper catches the exception, marks the future as completed exceptionally, and does not put the task back in the queue. The documentation of scheduleAtFixedRate says this directly: if any execution encounters an exception, subsequent executions are suppressed.
Because the exception is caught by the wrapper, it never reaches the thread's uncaught exception handler, which is the thing that would normally print a stack trace. It is stored in the ScheduledFuture returned by scheduleAtFixedRate, and that future is usually ignored, because a healthy periodic task never completes.
6 messages in order. Play them, or step through them one at a time.
The exception is kept in a future nobody reads, so the logs stay clean.
So if we kept the future, we can confirm the cause after the fact. If we did not, we look for indirect evidence: the last log line of the task and the time it stopped, which often lines up with a deploy of the downstream service or an unusual piece of data.
3Ruling Out the Other Cause: A Hung Run
There is one other way a fixed-rate task stops: a run that never finishes. Because runs of the same task never overlap, a run stuck forever on a network call without a timeout also stops all later runs, again without an error. A thread dump tells the two cases apart in seconds. If the scheduler thread is parked waiting for the next task, for example in DelayedWorkQueue.take, nothing is running and the task died from an exception. If the thread is inside our refresh code, for example blocked reading a socket, the run is hung and the fix is a timeout on that call.
- Take a thread dumpjcmd <pid>
Thread.print - Scheduler thread inside our task code?e.g. blocked in
socketReadYesHung run: add timeouts to the callNoThread is idle in the queue - Future isDone() is true?if we kept a referenceYesException: read it with get()NoCheck for cancel() or shutdown
- Fix and add a staleness alertlast success timestamp
4 steps in order. Play them, or step through them one at a time.
4The Fix: Nothing Escapes the Task Body
We make the task body handle its own failures. Any Throwable is caught, logged with its stack trace, and the method returns normally, so the executor schedules the next run as planned. Catching Throwable at this outer boundary also covers Errors such as an ExceptionInInitializerError from a bad class or a StackOverflowError on strange input; if we prefer to let real Errors stop the task, we should at least log them before rethrowing.
We also fix the data handling itself, skipping or reporting entries with a null price, but the wrapper is the general protection: every scheduled task in the service should go through it.
5Monitoring That Catches It Within Minutes
Logging only helps if someone reads the logs. The stronger signal is a metric for the time of the last successful refresh, set at the end of each successful run, and an alert when it is older than a few periods, for example 5 minutes for a task that runs every minute. That alert fires for every cause at once: an exception, a hung run, a scheduler that was shut down, even a task that was never started after a configuration change.
Finally, if the service uses Spring, it is worth checking what the framework does. Spring's @Scheduled methods run through a TaskScheduler that wraps repeating tasks with an error handler which logs the exception and keeps the schedule going, so the same bug would have produced an error in the logs instead of silence. Check your version and whether a custom ErrorHandler is configured, and keep the staleness alert either way.
6Follow-up questions
Why does the JDK suppress later runs instead of continuing?
The executor cannot know whether the failure is temporary or whether running the task again would repeat a harmful action, so it stops and reports the failure through the future. The design assumes the caller will inspect the future, which is rarely true for periodic tasks, so in practice the task body must handle its own errors.
Would scheduleWithFixedDelay have behaved differently?
No. Both periodic methods suppress later executions after an exception. They differ only in how they compute the next start time.
Could you restart the task automatically after a failure?
Yes, by watching the future or catching the failure and scheduling the task again, but it is simpler and clearer to never let the exception escape. Automatic restarts also hide repeated failures unless they are counted and alerted on.
7Conclusion
In this answer, we found the most likely cause of the silent stop: a run threw an unchecked exception, ScheduledExecutorService suppressed every later run, and the exception was kept in a ScheduledFuture that nobody read. A thread dump separates that from a hung run. The fix is a wrapper that catches Throwable, logs it and returns, plus a last-success metric with a staleness alert that catches every cause. With Spring's @Scheduled, the default error handler logs and continues, but that is worth checking too.
Open with
My first suspect is an exception in one run. With ScheduledExecutorService, if any execution of a periodic task throws, all later executions are suppressed, and nothing is logged, because the exception is caught by the task wrapper and stored in the ScheduledFuture returned by scheduleAtFixedRate.
Then, if they want more
Nobody looks at that future, so the task just stops and the logs stay clean, while the service keeps serving from the stale cache. To confirm, I would check the future if we kept it: isDone is true and get throws an ExecutionException with the real cause.
I would also take a thread dump to rule out the other possibility, a run that hangs forever on a call without a timeout, because runs of the same task never overlap, so a hung run also stops the schedule.
If the scheduler thread is idle in the queue, it was an exception; if it is inside our code blocked on a socket, it is a missing timeout. The fix is a small wrapper for every scheduled task that catches Throwable, logs it with the stack trace and returns normally, so the next run happens.
Then monitoring: a gauge with the age of the last successful refresh and an alert when it is older than a few periods, which catches exceptions, hangs and a dead scheduler alike. If we used Spring's @Scheduled, its default error handler logs and keeps the schedule running, but I would verify that for our version.
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.
- Official documentation
- java.util.concurrent, package documentation, JLS chapter 17, Threads and Locks
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 "A cache-refresh task scheduled with scheduleAtFixedRate every minute stopped running...": https://scrivio-interview-library.netlify.app/java/executors/8afd8a42b5/