java.util.Timer.scheduleAtFixedRate()


描述

scheduleAtFixedRate(TimerTask task,Date firstTime,long period) 方法用于调度进行重复的固定速率执行指定的任务,在指定的时间开始。

声明

以下是java.util.Timer.scheduleAtFixedRate()方法的声明。

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

参数

task - 这是要安排的任务。

firstTime - 这是第一次执行任务。

period - 这是连续任务执行之间的时间(以毫秒为单位)。

返回值

NA

异常

IllegalArgumentException - 如果time.getTime()为负数,则抛出此异常。

IllegalStateException - 如果已安排或取消任务,取消计时器或终止计时器线程,则抛出此异常。

实例

以下示例显示了java.util.Timer.scheduleAtFixedRate()的用法

package com.tutorialspoint;

import java.util.*;

public class TimerDemo {
   public static void main(String[] args) {

      // creating timer task, timer
      TimerTask tasknew = new TimerScheduleFixedRate();
      Timer timer = new Timer();

      // scheduling the task at fixed rate
      timer.scheduleAtFixedRate(tasknew,new Date(),1000);      
   }
   // this method performs the task

   public void run() {
      System.out.println("working at fixed rate");      
   }    
}

让我们编译并运行上面的程序,这将产生以下结果。

working at fixed rate
working at fixed rate
working at fixed rate
working at fixed rate and so on ...