简体   繁体   English

如何在java中每隔x秒执行一次方法

[英]How can I execute a method every x second for y time in java

I want to execute a method every second for 20 times. 我想每秒执行一次方法20次。

Actually I have timer 其实我有计时器

Timer timer = new Timer();
int begin = 0;
int timeInterval = 1000;

timer.scheduleAtFixedRate(new TimerTask() {
    @Override
    public void run() {
        //call the method
    }
}, begin, timeInterval);

How can I call a method 20 times with this interval? 如何以此间隔调用方法20次?

Use timer.schedule() , and keep track of how many times the timer was executed, and stop the timer after 20 times, with timer.cancel() 使用timer.schedule() ,并跟踪计时器执行的次数,并在20次后停止计时器,使用timer.cancel()

java doc - time schedule java doc - 时间表

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. 随后的执行大约以规定的时间间隔进行,并以指定的时间段分隔。

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. 如果执行因任何原因(例如垃圾收集或其他后台活动)而延迟,则后续执行也将延迟。 In the long run, the frequency of execution will generally be slightly lower than the reciprocal of the specified period (assuming the system clock underlying Object.wait(long) is accurate). 从长远来看,执行频率通常会略低于指定时间段的倒数(假设Object.wait(long)下的系统时钟是准确的)。

Fixed-delay execution is appropriate for recurring activities that require "smoothness." 固定延迟执行适用于需要“平滑”的重复活动。 In other words, it is appropriate for activities where it is more important to keep the frequency accurate in the short run than in the long run. 换句话说,它适用于在短期内保持频率比长期更准确的活动。 This includes most animation tasks, such as blinking a cursor at regular intervals. 这包括大多数动画任务,例如定期闪烁光标。 It also includes tasks wherein regular activity is performed in response to human input, such as automatically repeating a character as long as a key is held down. 它还包括响应于人类输入而执行常规活动的任务,例如只要按下键就自动重复一个字符。

Parameters: 参数:

 task - task to be scheduled. delay - delay in milliseconds before task is to be executed. period - time in milliseconds between successive task executions. 

Example

Timer timer = new Timer();
int begin = 0;
int timeInterval = 1000;
timer.schedule(new TimerTask() {
  int counter = 0;
   @Override
   public void run() {
       //call the method
       counter++;
       if (counter >= 20){
         timer.cancel();
       }
   }
}, begin, timeInterval);

Try the Executor Service. 尝试Executor服务。 You have to count youself, how often you called the Callable and cancel the Timer. 你必须自己计算,你多久调用一次Callable并取消Timer。

If it does not meet your requirements, you can follow this thread: 如果它不符合您的要求,您可以遵循以下主题:

Java Timer to call function n times after every t seconds Java Timer在每t秒后调用n次函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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