java - Android Timer 时间表与 scheduleAtFixedRate

标签 java android multithreading timer scheduled-tasks

我正在编写一个每 10 分钟录制一次音频的 Android 应用程序。我正在使用计时器来做到这一点。但是 schedule 和 scheduleAtFixedRate 有什么区别呢?使用其中一种是否有任何性能优势?

最佳答案

this non-Android documentation 可以很好地解释差异。 :

固定速率计时器 (scheduleAtFixedRate()) 基于开始时间(因此每次迭代将在 startTime + iterationNumber * delayTime 执行)。

In fixed-rate execution, each execution is scheduled relative to the scheduled execution time of the initial execution. If an execution is delayed for any reason (such as garbage collection or other background activity), two or more executions will occur in rapid succession to "catch up."

固定延迟计时器(schedule())基于上一次执行(因此每次迭代将在 lastExecutionTime + delayTime 处执行)。

In fixed-delay execution, each execution is scheduled relative to the actual execution time of the previous execution. If an execution is delayed for any reason (such as garbage collection or other background activity), subsequent executions will be delayed as well.

除此之外,没有任何区别。您也不会发现显着的性能差异。

如果您在想要与其他东西保持同步的情况下使用它,则需要使用 scheduleAtFixedRate()schedule() 的延迟可能会漂移并引入错误。

关于java - Android Timer 时间表与 scheduleAtFixedRate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12684604/

相关文章:

java - 对于Java BigInteger类,有没有办法定义一个具有多个条件的if语句,并用逻辑运算符分隔?

java - If 语句中的 JSP session

java - 如何在 Android native C 中获取 APK signing 签名?

android - Android蓝牙低功耗回调(LeScanCallBack)在单独的线程上?

android - Phonegap - 无法从服务器下载存档

android - 为什么 GestureOverlayView 这么慢?

c - C中的线程管理

multithreading - FFmpeg 对 avcodec_send_packet() 做了什么?

c# - Monitor.TryEnter 与通用类

java - 带有自定义类加载器的 Jmockit : System class loader does not support adding JAR file to system class path during the live phase