Java Timer purge() 方法



描述

Java Timer purge() 方法用於從此計時器的任務佇列中移除所有已取消的任務。

宣告

以下是 java.util.Timer.purge() 方法的宣告。

public int purge()

引數

返回值

方法呼叫返回從佇列中移除的任務數量。

異常

以固定速率排程計時器的清除示例

以下示例演示瞭如何使用 Java Timer purge() 方法清除當前已排程的計時器操作。我們使用 CustomTimerTask 物件建立了一個計時器物件。CustomTimerTask 是擴充套件 TimerTask 類並實現將在計劃時間執行的 run() 方法的自定義類。然後,我們建立了一個計時器物件,並使用 scheduleAtFixedRate() 方法排程了一個任務,該任務從現在開始每 100 毫秒執行一次。然後,使用 cancel() 方法終止計時器操作,並使用 purge() 方法從佇列中移除已取消的任務。

package com.tutorialspoint;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

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

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

      // scheduling the task
      timer.scheduleAtFixedRate(tasknew, new Date(), 100);

      // terminating the timer
      System.out.println("cancelling timer");
      timer.cancel();
      System.out.println("purge value :"+timer.purge());
   }
}

class CustomTimerTask extends TimerTask {

   @Override
   public void run() {
      System.out.println("working on");
   }  
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

cancelling timer
purge value :0
working on

以固定速率和日期排程計時器的清除示例

以下示例演示瞭如何使用 Java Timer purge() 方法清除當前已排程的計時器操作。我們使用 CustomTimerTask 物件建立了一個計時器物件。CustomTimerTask 是擴充套件 TimerTask 類並實現將在計劃時間執行的 run() 方法的自定義類。然後,我們建立了一個作為守護執行緒的計時器物件,並使用 scheduleAtFixedRate() 方法排程了一個任務,該任務從現在開始每 100 毫秒執行一次。然後,使用 cancel() 方法終止計時器操作,並使用 purge() 方法從佇列中移除已取消的任務。

package com.tutorialspoint;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

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

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

      // scheduling the task
      timer.scheduleAtFixedRate(tasknew, new Date(), 100);

      // terminating the timer
      System.out.println("cancelling timer");
      timer.cancel();
      System.out.println("purge value :"+timer.purge());
   }
}

class CustomTimerTask extends TimerTask {

   @Override
   public void run() {
      System.out.println("working on");
   }  
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

cancelling timer
purge value :0
working on

以固定日期和初始延遲排程計時器的清除示例

以下示例演示瞭如何使用 Java Timer purge() 方法清除當前已排程的計時器操作。我們使用 CustomTimerTask 物件建立了一個計時器物件。CustomTimerTask 是擴充套件 TimerTask 類並實現將在計劃時間執行的 run() 方法的自定義類。然後,我們建立了一個具有給定名稱的作為守護執行緒的計時器物件,並使用 scheduleAtFixedRate() 方法排程了一個任務,該任務從現在開始每 100 毫秒執行一次。然後,使用 cancel() 方法終止計時器操作,並使用 purge() 方法從佇列中移除已取消的任務。

package com.tutorialspoint;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

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

      // creating timer task, timer
      TimerTask tasknew = new CustomTimerTask();
      Timer timer = new Timer("test",true);

      // scheduling the task
      timer.scheduleAtFixedRate(tasknew, new Date(), 100);

      // terminating the timer
      System.out.println("cancelling timer");
      timer.cancel();
      System.out.println("purge value :"+timer.purge());
   }
}

class CustomTimerTask extends TimerTask {

   @Override
   public void run() {
      System.out.println("working on");
   }  
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

cancelling timer
purge value :0
working on
java_util_timer.htm
廣告