當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java Timer scheduleAtFixedRate()用法及代碼示例

Timer類scheduleAtFixedRate()方法

用法:

    public void scheduleAtFixedRate (TimerTask tt, Date ft, long period);
    public void scheduleAtFixedRate (TimerTask tt, long de, long period);
  • scheduleAtFixedRate() 方法可在java.util包。
  • scheduleAtFixedRate (TimerTask tt, Date ft, long period) 方法用於調度給定任務以在給定時間重複開始以恒定速率執行。
  • scheduleAtFixedRate (TimerTask tt, long delay, long period) 方法用於調度給定任務以在給定延遲後重複開始以恒定速率執行。
  • 這些方法可能會在調度任務時拋出異常。
    • IllegalArgumentException:當任何一個參數不在範圍內時,可能會拋出此異常。
    • IllegalStateException:當任務已被安排或取消時,可能會拋出此異常。
  • 這些是非靜態方法,隻能通過類對象訪問,如果我們嘗試使用類名訪問這些方法,則會出現錯誤。

參數:

  • 在第一種情況下,scheduleAtFixedRate (TimerTask tt, Date ft, long period)
    • TimerTask tt- 表示要調度的計時器任務。
    • Date ft- 表示要調度的計時器任務。
    • long period- 表示任務執行之間的時間(以毫秒為單位)。
  • 第一種情況,scheduleAtFixedRate(TimerTask tt, long de, long period)
    • TimerTask tt- 表示要調度的計時器任務。
    • long de- 代表第一次執行任務。
    • long period- 表示任務執行之間的時間(以毫秒為單位)。

返回值:

在這兩種情況下,方法的返回類型都是void,它什麽都不返回。

範例1:

// Java program to demonstrate the example 
// of scheduleAtFixedRate() method of
// Timer

import java.util.*;

public class ScheduleAtFixedRateOfTimer {
 public static void main(String[] args) {
  // Instantaites a TimerTask and
  // Timer object
  TimerTask task = new ScheduleTask();
  Timer tmr = new Timer();

  System.out.println("tmr.scheduleAtFixedRate(task, new Date(), 1000):");
  // By using scheduleAtFixedRate(task,date,period) method isto
  // schedule the task at a constant rate in a
  // repeated manner and starts at the given time 1000 ms
  tmr.scheduleAtFixedRate(task, new Date(), 1000);
 }
}

class ScheduleTask extends TimerTask {
 // Task defined in this method
 public void run() {
  System.out.println("Out Of Stock...Keep Working");
 }
}

輸出

tmr.scheduleAtFixedRate(task, new Date(), 1000):
Out Of Stock...Keep Working
Out Of Stock...Keep Working
Out Of Stock...Keep Working
Out Of Stock...Keep Working
Out Of Stock...Keep Working
Out Of Stock...Keep Working
Out Of Stock...Keep Working
Out Of Stock...Keep Working
Out Of Stock...Keep Working
Out Of Stock...Keep Working
Out Of Stock...Keep Working
Out Of Stock...Keep Working
Out Of Stock...Keep Working
Out Of Stock...Keep Working
Out Of Stock...Keep Working

範例2:

import java.util.*;

public class ScheduleAtFixedRateOfTimer {
 public static void main(String[] args) {
  // Instantaites a TimerTask and
  // Timer object
  TimerTask task = new ScheduleTask();
  Timer tmr = new Timer();

  System.out.println("tmr.scheduleAtFixedRate(task, 50, 330):");
  // By using scheduleAtFixedRate(task,delay,period) method isto
  // schedule the task at a constant rate in a
  // repeated manner and starts after the given delay 
  tmr.scheduleAtFixedRate(task, 100, 800);
 }
}

class ScheduleTask extends TimerTask {
 // Task defined in this method
 public void run() {
  System.out.println("Out of Stock...Keep Working");
 }
}

輸出

tmr.scheduleAtFixedRate(task, 50, 330):
Out of Stock...Keep Working
Out of Stock...Keep Working
Out of Stock...Keep Working
Out of Stock...Keep Working
Out of Stock...Keep Working
Out of Stock...Keep Working
Out of Stock...Keep Working
Out of Stock...Keep Working
Out of Stock...Keep Working
Out of Stock...Keep Working
Out of Stock...Keep Working
Out of Stock...Keep Working
Out of Stock...Keep Working
Out of Stock...Keep Working
Out of Stock...Keep Working
Out of Stock...Keep Working
Out of Stock...Keep Working
Out of Stock...Keep Working
Out of Stock...Keep Working


相關用法


注:本文由純淨天空篩選整理自Preeti Jain大神的英文原創作品 Java Timer scheduleAtFixedRate() Method with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。