java.util.Timer.cancel() 方法
描述
cancel() 方法用于终止此计时器并丢弃任何当前计划的任务。
声明
以下是 java.util.Timer.cancel() 方法的声明。
public void cancel()
参数
NA
返回值
NA
异常
NA
示例
下面的例子展示了 java.util.Timer.cancel() 的用法。
package com.tutorialspoint;
import java.util.*;
public class TimerDemo {
public static void main(String[] args) {
// creating timer task, timer
TimerTask tasknew = new TimerCancel();
Timer timer = new Timer();
// scheduling the task
timer.scheduleAtFixedRate(tasknew, new Date(), 100);
// terminating the timer
System.out.println("cancelling timer");
timer.cancel();
}
// this method performs the task
public void run() {
System.out.println("working on");
}
}
让我们编译并运行上面的程序,这将产生以下结果.
cancelling timer working on

