Java的Timer中,如何取消多个定时任务

Taotesea 2017-11-27 01:14:01
例如:
for(int i = 0; i < 100; i++) {
timer.schedule(new TimerTask() {
......
}
}
请问各位大侠,我该如何取消这100个计划时再重新在添加100个定时任务,谢谢了:)
...全文
829 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
huage 2017-11-28
  • 打赏
  • 举报
回复
for(int i = 0; i < 100; i++) { timer.schedule(new TimerTask() { ......//执行前判断是否需要在执行定时任务即可,这里定义一个公共变量 } }
  • 打赏
  • 举报
回复
cancel之后调用purge方法,就清除了。。。
「已注销」 2017-11-27
  • 打赏
  • 举报
回复
不可以的,好像还少了点什么!
墨笙弘一 2017-11-27
  • 打赏
  • 举报
回复
public void cancel()
Terminates this timer, discarding any currently scheduled tasks. Does not interfere with a currently executing task (if it exists). Once a timer has been terminated, its execution thread terminates gracefully, and no more tasks may be scheduled on it.
Note that calling this method from within the run method of a timer task that was invoked by this timer absolutely guarantees that the ongoing task execution is the last task execution that will ever be performed by this timer.

This method may be called repeatedly; the second and subsequent calls have no effect.
timer里面的cancer方法不行么

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧