Java-util-timer-scheduleatfixedrate

提供:Dev Guides
移動先:案内検索

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 -これは、タスクが実行される最初の時間です。
  • 期間-これは連続したタスク実行間のミリ秒単位の時間です。

戻り値

NA

例外

  • IllegalArgumentException -この例外は、time.getTime()が負の場合にスローされます。
  • IllegalStateException -タスクがすでにスケジュールまたはキャンセルされている場合、タイマーがキャンセルされている場合、またはタイマースレッドが終了した場合にスローされます。

次の例は、java.util.Timer.scheduleAtFixedRate()の使用法を示しています

package com.finddevguides;

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 ...