Quick revision
The short answer
java.util.Timer runs every task on one background thread, so one slow task delays all the others, and one unchecked exception kills that thread and silently stops every task the Timer owns. It also schedules against the wall clock, so clock changes move executions. We replace it with a ScheduledExecutorService, or with Spring's @Scheduled backed by a configured TaskScheduler, and we always catch exceptions inside the task.
What to remember
In this answer, we saw that Timer runs all tasks on one thread, so slow tasks delay others and one unchecked exception cancels the whole Timer without a word, and that it schedules against the wall clock. ScheduledExecutorService fixes the threading and uses a monotonic clock, but still suppresses later runs of a task that throws, so we catch exceptions inside every task. In Spring we prefer @Scheduled with a configured TaskScheduler, which also handles shutdown.
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.
java.util.Timer runs every task on one background thread, so one slow task delays all the others, and one unchecked exception kills that thread and silently stops every task the Timer owns. It also schedules against the wall clock, so clock changes move executions. We replace it with a ScheduledExecutorService, or with Spring's @Scheduled backed by a configured TaskScheduler, and we always catch exceptions inside the task.
Why this question is asked
Timer has been in the JDK since Java 1.3, so it still turns up in older services, utility classes and copied examples. It looks harmless, and it works in a demo. The trouble starts in production, where a task occasionally throws or runs long, and a cache that should refresh every minute quietly stops refreshing for days. Interviewers ask this question because it checks whether we understand threads behind a scheduling API, not just the method names. In this answer, we will recap how Timer works, reproduce its three problems one by one, and then look at what replaces it and the one habit that matters with any scheduler.
1A Quick Recap of Timer and TimerTask
A Timer is an object that runs tasks later or repeatedly. A TimerTask is an abstract class that implements Runnable; we extend it and put our work in run(). We hand the task to the Timer with schedule() for a fixed delay between runs, or scheduleAtFixedRate() for runs that try to keep a steady rhythm.
Inside, a Timer owns exactly one thread, called the timer thread, and a queue of tasks ordered by their next execution time. The thread takes the earliest task from the queue, waits until its time comes, runs it on that same thread, works out the next time if the task repeats, and goes back to the queue. That single thread is the root of the first two problems.
2Problem 1: One Slow Task Delays All the Others
Because every task shares the one timer thread, a task that takes long holds up everyone else. Suppose a report task takes 30 seconds and a heartbeat task should run every second.
Notice that the heartbeat is not lost, it is late, and with scheduleAtFixedRate the Timer then fires the missed runs back to back to catch up. A heartbeat that arrives 30 seconds late is as bad as none, because whatever watches it has already raised an alarm.
3Problem 2: An Exception Kills the Timer Silently
The timer thread runs our run() method directly. If run() throws an unchecked exception, such as a NullPointerException, nothing catches it. The exception ends the timer thread, and the Timer marks itself as cancelled. Every other task scheduled on it stops too, and nothing is logged except, at best, a stack trace on standard error.
6 messages in order. Play them, or step through them one at a time.
The exception escapes the shared thread, so task A stops even though it did nothing wrong.
So one buggy task takes down unrelated tasks, and the application keeps running as if nothing happened. We only find out when someone notices stale data or tries to schedule a new task and gets the Timer already cancelled error.
4Problem 3: Scheduling Against the Wall Clock
Timer calculates its next run times from System.currentTimeMillis(), the wall clock of the machine. If the clock jumps, because NTP corrects it or an operator changes it, the Timer's idea of when the next run is due jumps with it. A clock moved back by an hour can make a task wait an extra hour; a clock moved forward can trigger a burst of catch-up runs.
The executor that replaces Timer measures delays with System.nanoTime(), a monotonic clock that only moves forward and does not follow changes to the time of day. So a ScheduledExecutorService keeps its intervals steady when the wall clock changes.
5What Replaces Timer
The JDK replacement is ScheduledExecutorService, created with Executors.newScheduledThreadPool(n) or newSingleThreadScheduledExecutor(). It fixes the first two problems directly: it can use several threads, so a slow task does not block the others, and a task that throws does not kill the pool. It gives us scheduleAtFixedRate(), which starts runs at a steady rate, and scheduleWithFixedDelay(), which waits a fixed time after each run finishes.
| Concern | java.util.Timer | ScheduledExecutorService | Spring @Scheduled |
|---|---|---|---|
| Threads | Always one | As many as we configure | TaskScheduler pool, 1 by default in Boot |
| Task throws | Thread dies, all tasks stop | Only that task stops | Logged, schedule continues |
| Clock used | Wall clock | Monotonic nanoTime | Monotonic for rates and delays |
| Shutdown | Manual cancel() | Manual shutdown() | With the application context |
But there is a catch, and interviewers like it: a repeating task that throws is not retried. The executor suppresses all its later runs and stores the exception in the ScheduledFuture, where nobody looks unless they call get(). So the pool survives, but that one task still stops silently. The rule is the same for every scheduler: catch exceptions inside the task, log them, and let the next run try again.
In a Spring application we usually go one step higher and use @Scheduled on a bean method, with @EnableScheduling and a TaskScheduler that Spring Boot configures for us. Spring manages the threads and shuts them down with the application, and for repeating tasks its default error handler logs the exception and keeps the schedule going.
6Follow-up questions
What is the difference between scheduleAtFixedRate and scheduleWithFixedDelay?
scheduleAtFixedRate tries to start runs at 0, 60, 120 seconds and so on, measured from the first start, so a run that takes 10 seconds still leaves the next start at the planned time. scheduleWithFixedDelay waits the full delay after each run ends, so with a 60 second delay and 10 second runs, starts drift to 0, 70, 140. If a fixed-rate run takes longer than the period, the next run starts late; the executor never runs the same task twice at once.
If I use a pool with several threads, do I still need to catch exceptions?
Yes. More threads stop one task from blocking another, but a throwing task's later runs are still suppressed. Wrapping the body in try and catch, logging the error and perhaps incrementing a failure metric is what keeps that task running.
Is there any case where Timer is still acceptable?
In a tiny tool or a test where a single task runs and a failure can stop the program, it works. For anything long-lived on a server, there is no reason to choose it over an executor, which costs one line more.
7Conclusion
In this answer, we saw that Timer runs all tasks on one thread, so slow tasks delay others and one unchecked exception cancels the whole Timer without a word, and that it schedules against the wall clock. ScheduledExecutorService fixes the threading and uses a monotonic clock, but still suppresses later runs of a task that throws, so we catch exceptions inside every task. In Spring we prefer @Scheduled with a configured TaskScheduler, which also handles shutdown.
Open with
Timer has three problems. First, it runs every task on a single thread, so one slow task delays all the others, and fixed-rate tasks then fire in a burst to catch up.
Then, if they want more
Second, if a TimerTask throws an unchecked exception, it kills that thread and the Timer cancels itself, so every task stops silently, and scheduling a new one fails with Timer already cancelled. Third, it computes run times from the wall clock, so clock corrections can shift or bunch executions.
The replacement in plain Java is a ScheduledExecutorService from Executors.newScheduledThreadPool, with scheduleAtFixedRate for a steady rhythm or scheduleWithFixedDelay for a pause after each run. It can use several threads, survives a failing task and measures time with nanoTime.
One thing I always point out is that the executor still suppresses later runs of a task that throws and hides the exception in the future, so I wrap the body in try and catch and log it.
In a Spring service I would rather use @Scheduled with a TaskScheduler that Spring Boot configures, because Spring handles the thread pool and shutdown and logs errors without stopping the schedule.
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
- Spring Batch reference
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 "java.util.Timer and TimerTask are considered legacy. What are their problems, and what...": https://scrivio-interview-library.netlify.app/spring-boot/scheduling/9746a119e5/