Java Timer cancel() 方法



描述

Java Timer cancel() 方法用於終止此計時器並丟棄任何當前計劃的任務。

宣告

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

public void cancel()

引數

返回值

異常

使用固定日期和初始延遲取消計時器示例

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

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();
   }
}

class CustomTimerTask extends TimerTask {

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

輸出

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

cancelling timer
working on

將計時器計劃為守護執行緒並取消示例

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

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();
   }
}

class CustomTimerTask extends TimerTask {

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

輸出

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

cancelling timer
working on

將命名計時器計劃為守護執行緒並取消示例

以下示例演示了 Java Timer cancel() 方法的使用,用於取消當前計劃的計時器操作。我們使用 CustomTimerTask 物件建立了一個計時器物件。CustomTimerTask 是擴充套件 TimerTask 類並實現 run() 方法的自定義類,該方法將在計劃時間執行。然後我們建立了一個作為守護執行緒的計時器物件,併為其指定了一個名稱,並使用 scheduleAtFixedRate() 方法計劃了一個任務,該任務從現在開始每 100 毫秒執行一次。然後使用 cancel() 方法終止計時器操作。

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();
   }
}

class CustomTimerTask extends TimerTask {

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

輸出

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

cancelling timer
working on
java_util_timer.htm
廣告