简体   繁体   中英

scheduleAtFixedRate executes fast

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.

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?

The method schedule in Timer class could be considered even though it does not guarantee that the task will be executed once per 'x' period of time.

public void schedule(TimerTask task, Date firstTime,  long period)

Atleast it would not have repeated execution like the way you said.

You could try with a ScheduledExecutorService instead, with more than one thread if your task can take more than 1 sec to run.

The syntax is very similar to Timer but it is a more robust and flexible API.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM