如何停止在java.util.Timer 类中调度的任务 [英] How to stop the task scheduled in java.util.Timer class
本文介绍了如何停止在java.util.Timer 类中调度的任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 java.util.Timer
类并且我正在使用它的 schedule 方法来执行一些任务,但是在执行它 6 次后我必须停止它的任务.
I am using java.util.Timer
class and I am using its schedule method to perform some task, but after executing it for 6 times I have to stop its task.
我该怎么做?
推荐答案
在某处保留对计时器的引用,并使用:
Keep a reference to the timer somewhere, and use:
timer.cancel();
timer.purge();
停止它正在做的任何事情.您可以将此代码放入您正在执行的任务中,并使用 static int
来计算您四处走动的次数,例如
to stop whatever it's doing. You could put this code inside the task you're performing with a static int
to count the number of times you've gone around, e.g.
private static int count = 0;
public static void run() {
count++;
if (count >= 6) {
timer.cancel();
timer.purge();
return;
}
... perform task here ....
}
这篇关于如何停止在java.util.Timer 类中调度的任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文