Android Timer schedule vs scheduleAtFixedRate [英] Android Timer schedule vs scheduleAtFixedRate
问题描述
我正在编写一个每 10 分钟记录一次音频的 Android 应用程序.我正在使用计时器来做到这一点.但是 schedule 和 scheduleAtFixedRate 有什么区别?使用其中一种是否有任何性能优势?
I'm writing an Android application that records audio every 10 minutes. I am using a Timer to do that. But what is the difference between schedule and scheduleAtFixedRate? Is there any performance benefit in using one over the other?
推荐答案
固定速率计时器(scheduleAtFixedRate()
)基于开始时间(因此每次迭代将在 startTime + iterationNumber * delayTime
处执行).
Fixed-rate timers (scheduleAtFixedRate()
) are based on the starting time (so each iteration will execute at 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
处执行).
Fixed-delay timers (schedule()
) are based on the previous execution (so each iteration will execute at 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.
除此之外,没有任何区别.您也不会发现显着的性能差异.
Aside from this, there is no difference. You will not find a significance performance difference, either.
如果您在希望与其他事物保持同步的情况下使用它,您将需要使用 scheduleAtFixedRate()
.schedule()
的延迟可能会漂移并引入错误.
If you are using this in a case where you want to stay synchronized with something else, you'll want to use scheduleAtFixedRate()
. The delay from schedule()
can drift and introduce error.
这篇关于Android Timer schedule vs scheduleAtFixedRate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!