java.util.TimerTask.cancel() 方法
描述
cancel() 方法用于取消这个定时器任务。
声明
以下是 java.util.TimerTask.cancel() 方法的声明。
public boolean cancel()
参数
NA
返回值
如果该任务被安排为一次性执行且尚未运行,则该方法调用返回 true。如果该任务被安排为一次性执行且已运行,则该方法调用返回 false。
异常
NA
示例
下面的例子展示了 java.util.TimerTask.cancel() 的用法。
package com.tutorialspoint;
import java.util.*;
public class TimerTaskDemo {
public static void main(String[] args) {
// creating timer task, timer
TimerTask task = new TimerTaskCancel();
Timer timer = new Timer();
// scheduling the task
timer.scheduleAtFixedRate(task, new Date(), 1000);
// cancelling the task
System.out.println("cancelling task: "+task.cancel());
}
// this is the implementation method
public void run() {
System.out.println("Working");
}
}
让我们编译并运行上面的程序,这将产生以下结果.
cancelling task: true Working

