To implement a timer in Java, create a java.util.Timer, subclass TimerTask with the code to run, and call schedule for a one-off or fixed-delay task or scheduleAtFixedRate for a fixed-rate task. Timer still works, but for new code ScheduledExecutorService is the recommended replacement because it uses a thread pool and handles task failures better.
This article shows both approaches, explains the differences that cause real bugs (single thread, uncaught exceptions, wall-clock dependence) and points to Spring and Quartz when you need cron-style schedules.
TimerTask.cancel() stops one task. Timer.cancel() discards all pending tasks and ends the timer's background thread; after that the timer cannot be reused. The thread is non-daemon by default, so a forgotten timer keeps the JVM alive. Create it with new Timer("name", true) for a daemon thread.
| Method | Timing | If a run is late |
|---|---|---|
schedule(task, delay, period) | Fixed delay: each run is timed from the previous actual start | Later runs drift |
scheduleAtFixedRate(task, delay, period) | Fixed rate: runs are timed against the planned start | Runs catch up in quick succession |
Use fixed delay when the gap between runs matters, and fixed rate when the average frequency matters (for example, a clock tick).
Timer run on one thread, so a slow task delays every other task.TimerTask throws an unchecked exception, the timer thread terminates and later calls to schedule throw IllegalStateException.Timer is based on System.currentTimeMillis(), so changes to the system clock can shift or skip runs. ScheduledExecutorService uses System.nanoTime().Future to cancel, inspect or wait for.Timer is not formally deprecated, but the Timer Javadoc itself points to ScheduledThreadPoolExecutor as the more versatile choice.
Equivalents: schedule for a one-off, scheduleWithFixedDelay for fixed delay, scheduleAtFixedRate for fixed rate. Be aware that if a periodic task throws, its future is cancelled and it is not run again, so catch exceptions inside the task as above when the job must continue. Tasks that take longer than the period in a fixed-rate schedule never run concurrently with themselves; they start late instead.
Neither Timer nor ScheduledExecutorService understands "every weekday at 09:00". For that:
@Scheduled(cron = "0 0 9 * * MON-FRI", zone = "Europe/Paris").java.time (Duration.between(now, nextRun)) and use scheduleAtFixedRate with a period of 24 hours, keeping in mind that daylight saving changes are not handled for you.No, it is not deprecated, but ScheduledExecutorService is preferred for new code.
An unchecked exception in one TimerTask ended the timer thread. Catch exceptions inside run, or use a scheduled executor.
Call timer.cancel() to stop everything, or task.cancel() for a single task. Create the timer as a daemon if it should not keep the JVM alive.
No. One thread runs all tasks in sequence. Use newScheduledThreadPool(n) for parallelism.
Timer and TimerTask give a simple way to delay or repeat work, but with one thread, fragile error handling and wall-clock timing. Prefer ScheduledExecutorService, catch exceptions inside periodic tasks, and use Spring or Quartz for cron schedules.