Hello, 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.How should i do that ?
+1
A:
Either call cancel() on the Timer if that's all it's doing, or cancel() on the TimerTask if the timer itself has other tasks which you wish to continue.
Jon Skeet
2009-09-11 05:26:02
Thanks Jon,let me check this
om
2009-09-11 05:53:56
+3
A:
Keep a reference to the timer somewhere, and use:
timer.cancel();
timer.purge();
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 ....
}
Fritz H
2009-09-11 05:28:00