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
广告

© . All rights reserved.