Java Timer vs ExecutorService?

前端 未结 6 1353
孤独总比滥情好
孤独总比滥情好 2020-11-21 23:51

I have code where I schedule a task using java.util.Timer. I was looking around and saw ExecutorService can do the same. So this question here, hav

6条回答
  •  旧时难觅i
    2020-11-22 00:26

    According to Java Concurrency in Practice:

    • Timer can be sensitive to changes in the system clock, ScheduledThreadPoolExecutor isn't.
    • Timer has only one execution thread, so long-running task can delay other tasks. ScheduledThreadPoolExecutor can be configured with any number of threads. Furthermore, you have full control over created threads, if you want (by providing ThreadFactory).
    • Runtime exceptions thrown in TimerTask kill that one thread, thus making Timer dead :-( ... i.e. scheduled tasks will not run anymore. ScheduledThreadExecutor not only catches runtime exceptions, but it lets you handle them if you want (by overriding afterExecute method from ThreadPoolExecutor). Task which threw exception will be canceled, but other tasks will continue to run.

    If you can use ScheduledThreadExecutor instead of Timer, do so.

    One more thing... while ScheduledThreadExecutor isn't available in Java 1.4 library, there is a Backport of JSR 166 (java.util.concurrent) to Java 1.2, 1.3, 1.4, which has the ScheduledThreadExecutor class.

提交回复
热议问题