scheduleAtFixedRate 执行速度快 [英] scheduleAtFixedRate executes fast
问题描述
我在我的程序(定时器类)中使用了 scheduleAtFixedRate 方法.它每隔一秒运行一次,但有时这种方法变得非常快(每秒执行 3 - 4 次).
I'm using method scheduleAtFixedRate in my program (Timer class ). It runs every one second, but sometimes this method becomes really fast ( it's executing 3 - 4 per second ).
但是我在网上做了一些研究,发现了这个:
However i did some research on web and i found this:
从安卓开发者页面复制:
Copied from android developer's page:
在固定速率执行中,任务每次连续运行的开始时间都是安排好的,而不考虑上一次运行的时间.如果延迟阻止计时器按时启动任务,这可能会导致一系列成堆的运行(一个接一个立即启动).
With fixed-rate execution, the start time of each successive run of a task is scheduled without regard for when the previous run took place. This may result in a series of bunched-up runs (one launched immediately after another) if delays prevent the timer from starting tasks on time.
我需要固定的计时器.它应该每 x 次运行一次,没有任何快速执行.scheduleAtFixedRate 方法是否有任何替代方法,或者我需要对此方法感到满意?
I need fixed timer. It should run only once per x time without any fast executions. Is there any alternative to scheduleAtFixedRate method or, i need to be satisfied with this method?
推荐答案
您可以尝试使用 ScheduledExecutorService,如果您的任务运行时间可能超过 1 秒,则使用多个线程.
You could try with a ScheduledExecutorService instead, with more than one thread if your task can take more than 1 sec to run.
语法与 Timer 非常相似,但它是一个更健壮和灵活的 API.
The syntax is very similar to Timer but it is a more robust and flexible API.
这篇关于scheduleAtFixedRate 执行速度快的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!