Timer vs. ScheduledExecutorService scheduling

会有一股神秘感。 提交于 2019-12-01 09:29:11

问题


One of the recommended uses of the ScheduledExecutorService is as a direct replacement for the Timer class, as discussed in numerous StackOverflow topics already:

  • Java Timer vs ExecutorService?
  • Difference between TimerTask and Executors.newScheduledThreadPool(1)
  • What is the difference between schedule and scheduleAtFixedRate?
  • Android Timer schedule vs scheduleAtFixedRate

However, the naming conventions of the methods supported by ScheduledExecutorService and Timer, are not identical. For example, whereas they both have a scheduleAtFixedRate() method, the Timer method

  • schedule​(TimerTask task, long delay, long period)

does not have a namesake counterpart.

Is the ScheduledExecutorService method

  • scheduleWithFixedDelay​(Runnable command, long initialDelay, long delay, TimeUnit unit)

the one to use instead?


回答1:


https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ScheduledExecutorService.html#scheduleAtFixedRate(java.lang.Runnable,%20long,%20long,%20java.util.concurrent.TimeUnit)

Creates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given delay between the termination of one execution and the commencement of the next.

https://docs.oracle.com/javase/7/docs/api/java/util/Timer.html#schedule(java.util.TimerTask,%20long,%20long)

Schedules the specified task for repeated fixed-delay execution, beginning after the specified delay. Subsequent executions take place at approximately regular intervals separated by the specified period.

I would say - Yes ;-)



来源:https://stackoverflow.com/questions/48956511/timer-vs-scheduledexecutorservice-scheduling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!