Java TimerTask cancel() 方法



描述

Java TimerTask cancel() 方法用於取消此計時器任務。

宣告

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

public boolean cancel()

引數

返回值

如果此任務計劃執行一次並且尚未執行,則方法呼叫返回 true。如果任務計劃執行一次並且已經執行,則返回 false。

異常

取消計時器操作示例

以下示例演示瞭如何使用 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 TimerTaskDemo {
   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 task
      System.out.println("cancelling task: " + tasknew.cancel());
   }
}

class CustomTimerTask extends TimerTask {

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

輸出

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

working on
cancelling task: true

取消作為守護程序執行的計時器操作示例

以下示例演示瞭如何使用 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 TimerTaskDemo {
   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 task
      System.out.println("cancelling task: " + tasknew.cancel());
   }
}

class CustomTimerTask extends TimerTask {

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

輸出

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

working on
cancelling task: true

取消作為守護程序執行的命名計時器操作示例

以下示例演示瞭如何使用 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 TimerTaskDemo {
   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 task
      System.out.println("cancelling task: " + tasknew.cancel());
   }
}

class CustomTimerTask extends TimerTask {

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

輸出

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

cancelling timer
working on
java_util_timertask.htm
廣告