powered by Jive Software

TimerTasks vs ExecutorService

I observed that TimerTasks are used extensively in Openfire. Is there any clear advantage here, of using those in place of ScheduledExecutorService?

There are at least a couple of good things of ExecutorService over TimerTask -

  1. Can utilize multiple threads. However, it may not be possible in some cases in Openfire, where the order of data is important.

  2. Can handle runtime exceptions, which can be utilized for retrying execution.

I would like to know if it is worth investing to move to using ExecutorService.

Thanks,

Chinmay

I believe that there’s nothing more to it than most of that code simply pre-dating Java 1.5 (which is when ExecutorService was introduced in Java.

As you said, there are subtle differences. There’s likely no harm in updating, but then again, if it ain’t broken…

I’d suggest to use ExecutorService in new implementations, perhaps make improvements to existing code where needed, but not change the API if there’s no need to. That might introduce issues for third party developers, which we should avoid.

2 Likes