Fri*_*z H 127
在某处保留对计时器的引用,并使用:
timer.cancel();
timer.purge();
Run Code Online (Sandbox Code Playgroud)
停止它正在做的事情.你可以把这个代码放在你正在执行的任务中,static int用来计算你去过的次数,例如
private static int count = 0;
public static void run() {
count++;
if (count >= 6) {
timer.cancel();
timer.purge();
return;
}
... perform task here ....
}
Run Code Online (Sandbox Code Playgroud)
Ver*_*ing 26
您应该停止在计时器上安排的任务:您的计时器:
Timer t = new Timer();
TimerTask tt = new TimerTask() {
@Override
public void run() {
//do something
};
}
t.schedule(tt,1000,1000);
Run Code Online (Sandbox Code Playgroud)
为了停止:
tt.cancel();
t.cancel(); //In order to gracefully terminate the timer thread
Run Code Online (Sandbox Code Playgroud)
请注意,仅取消计时器不会终止正在进行的时间任务.
Abh*_*bhi 14
timer.cancel(); //Terminates this timer,discarding any currently scheduled tasks.
timer.purge(); // Removes all cancelled tasks from this timer's task queue.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
157685 次 |
| 最近记录: |