java - Timer vs. ScheduledExecutorService 调度

标签 java timer scheduling scheduledexecutorservice

ScheduledExecutorService 的推荐用途之一是作为 Timer 的直接替代品类,正如已经在许多 StackOverflow 主题中讨论的那样:

但是,ScheduledExecutorServiceTimer 支持的方法的命名约定并不相同。例如,尽管它们都有一个 scheduleAtFixedRate() 方法,但 Timer 方法

  • schedule (TimerTask任务,长延迟,长周期)

没有同名对应物。

ScheduledExecutorService方法

  • scheduleWithFixedDelay (可运行命令,long initialDelay,long delay,TimeUnit 单位)

用哪个代替?

最佳答案

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.

我会说 - 是的 ;-)

关于java - Timer vs. ScheduledExecutorService 调度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48956511/

相关文章:

java - Android IntentService 减慢 UI 线程速度

用于在应用程序之间委派任务的 Java 企业架构

ios - 同时运行两个定时器

c# - 计时器增加 C# 应用程序中的内存使用

java - 在java中处理定时器

Java Akka 调度 API 抛出 MissingMethodException

java - 基于数据库更新和时间间隔触发 Java 程序

java - Android runOnUiThread线程安全

发生多个匹配时的java正则表达式首选项

java - 架构管理异常 : Schema-validation: missing table [chk_groups] even when table is present in DB