全屏
cancel()方法用来取消此计时器任务。
声明
以下是java.util.TimerTask.cancel()方法的声明。public boolean cancel()
参数NA
返回值
如果此任务安排为一次执行,尚未执行该方法调用返回true。如果被安排为一次执行任务,并已运行它返回false。
异常NA
例子
下面的例子显示java.util.TimerTask.cancel()方法的使用package cn.sxt;
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: trueWorking
分享到:
0评论