Difference between TimerTask and Executors.newScheduledThreadPool(1) Difference between TimerTask and Executors.newScheduledThreadPool(1) multithreading multithreading

Difference between TimerTask and Executors.newScheduledThreadPool(1)


The biggest difference is that the Timer will schedule all of its tasks on a single background thread. The ExecutorService, on the other hand, will create new threads (if necessary) to run the tasks (up to the size of the pool you specify, at which point tasks will be queued.)


One other difference is if there is an uncaught exception. In case of a Timer, the background thread is terminated but it is not brought back up. With a ScheduledExecutor (even with a single thread configuration), the ScheduledExecutor can continue after an uncaught exception. It tries to ensure the desired number of threads are running to process the tasks.

The ScheduledExecutor also produces a future in case you want to interact with the progress.