Java util.timer固定延迟不工作?

Java util.timer固定延迟不工作?,java,timer,Java,Timer,我试图了解计时器的固定延迟方法(schedule)是如何工作的,但似乎失败了 这是我的代码: public class Timer_Test { static Timer _a_timer; static int num_o_proc = 0; static int timer_call = 0; static double prog_begin_time; public static void main(String[] args) { prog_begin_

我试图了解计时器的固定延迟方法(schedule)是如何工作的,但似乎失败了

这是我的代码:

public class Timer_Test {    
  static Timer _a_timer;
  static int num_o_proc = 0;
  static int timer_call = 0;
  static double prog_begin_time;

public static void main(String[] args) {
    prog_begin_time = System.currentTimeMillis();

    _a_timer = new Timer();

    _a_timer.schedule(new TimerTask() {
        @Override
        public void run() {
            timer_call++;
            System.out.println(timer_call + " timer start   at " + (System.currentTimeMillis() - prog_begin_time));
            process();


            System.out.println(timer_call + " timer end   at " + (System.currentTimeMillis() - prog_begin_time));
            if (timer_call >= 5) {
                System.exit(0);
            }
        }
    }, 1000, 2000);
}

public static void process() {
    num_o_proc++;
    int local_num_o_proc = num_o_proc;
    System.out.println(local_num_o_proc + " process start   at " + (System.currentTimeMillis() - prog_begin_time));

    double _a_ = 0;
    for(int x=0; x<Integer.MAX_VALUE/2; x++) {
        _a_++;
    }
    System.out.println(local_num_o_proc + " process end   at " + (System.currentTimeMillis() - prog_begin_time));
  }
}
公共类计时器测试{
静态定时器;
静态int num_o_proc=0;
静态整数计时器调用=0;
静态双程序开始时间;
公共静态void main(字符串[]args){
prog_begin_time=System.currentTimeMillis();
_a_定时器=新定时器();
_a_timer.schedule(新TimerTask(){
@凌驾
公开募捐{
定时器_调用++;
System.out.println(timer_call+“timer start at”+(System.currentTimeMillis()-prog_begin_time));
过程();
System.out.println(timer_call+“timer end at”+(System.currentTimeMillis()-prog_begin_time));
如果(计时器调用>=5){
系统出口(0);
}
}
}, 1000, 2000);
}
公共静态无效进程(){
num_o_proc++;
int local_num_o_proc=num_o_proc;
System.out.println(local_num_o_proc+“进程开始于”+(System.currentTimeMillis()-prog_begin_time));
double u a_uu=0;

对于(int x=0;xNope,这就是它的工作原理。周期是开始时间之间的周期,而不是结束时间和下一个开始时间之间的周期


基本上你是用通俗易懂的英语告诉它“从1秒开始,然后每两秒执行一次”,所以1、3、5、7等是合乎逻辑的解释。

好的,我重新阅读了Javadoc。这意味着“计划”使用前一个任务的开始时间作为参考(第二个任务将参考第一个任务,第三个任务将参考第二个任务)。虽然“scheduleAtFixedRate”总是使用第一个任务的开始时间作为参考(所有任务的开始时间都基于第一个任务的开始时间)。这就是它的工作原理吗?是的,我认为如果“scheduleAtFixedRate”错过了一个,它就会忘记它,如果“scheduleAtFixedRate”错过了一个,它就会补足。