ScheduledExecutorService 的推荐用途之一是作为 Timer 的直接替代品类,正如已经在许多 StackOverflow
主题中讨论的那样:
- Java Timer vs ExecutorService?
- Difference between TimerTask and Executors.newScheduledThreadPool(1)
- What is the difference between schedule and scheduleAtFixedRate?
- Android Timer schedule vs scheduleAtFixedRate
但是,ScheduledExecutorService
和 Timer
支持的方法的命名约定并不相同。例如,尽管它们都有一个 scheduleAtFixedRate()
方法,但 Timer 方法
- schedule (TimerTask任务,长延迟,长周期)
没有同名对应物。
是ScheduledExecutorService
方法
- scheduleWithFixedDelay (可运行命令,long initialDelay,long delay,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.
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.
我会说 - 是的 ;-)
关于java - Timer vs. ScheduledExecutorService 调度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48956511/