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?
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.
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