110

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?

Yuri
  • 4,254
  • 1
  • 29
  • 46
om.
  • 4,159
  • 11
  • 37
  • 36
  • Update: The `Timer` and `TimerTask` classes have been supplanted, as noted in their Javadoc. Learn to use the *Executors* framework added to Java 5+. See [Java Tutorials](https://docs.oracle.com/javase/tutorial/essential/concurrency/executors.html) by Oracle. – Basil Bourque Jun 27 '21 at 17:17

4 Answers4

156

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
  • 3,539
  • 2
  • 26
  • 37
  • 12
    I think cancel is enough, don't need to have purge – LiangWang May 30 '15 at 08:43
  • 1
    is it good to add timer.cancel() in final according to ( Effetive Java book) – Tushar Pandey Jul 02 '15 at 05:04
  • 1
    @Jacky it's good practice to have both, but theoretically `cancel` itself would work. – Fritz H Apr 28 '16 at 00:03
  • 12
    @Jacky is right. Look at the implementation of Timer. Calling purge after cancel is absolutely useless. Cancel clears the whole task list, while the purge iterates over the same list, checks if the status is CANCELED and then removes the tasks. – Boyan Aug 30 '16 at 15:06
  • 1
    Calling cancel() on Timer itself will cancel ALL tasks done on that timer. That is a bad idea, because usually one would use a single Timer instance for multiple timed tasks. It is important to use a single Timer because a Timer is expensive: it allocates a thread. The correct answer is to retain a reference to a Timer*Task* and call cancel on that because that terminates just that instance and leaves the rest intact. – fjalvingh May 21 '19 at 15:53
  • 2
    if the activity/fragment that started the Timer is destroyed or stopped, does the Timer scheduled stop on its own? – aLL Jun 14 '19 at 06:29
65

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
  • 1,421,763
  • 867
  • 9,128
  • 9,194
39

You should stop the task that you have scheduled on the timer: Your timer:

Timer t = new Timer();
TimerTask tt = new TimerTask() {
    @Override
    public void run() {
        //do something
    };
};
t.schedule(tt,1000,1000);

In order to stop:

tt.cancel();
t.cancel(); //In order to gracefully terminate the timer thread

Notice that just cancelling the timer will not terminate ongoing timertasks.

Hasen
  • 11,710
  • 23
  • 77
  • 135
Vering
  • 907
  • 9
  • 19
2

Terminate the Timer once after awake at a specific time in milliseconds.

Timer t = new Timer();
t.schedule(new TimerTask() {
            @Override
             public void run() {
             System.out.println(" Run spcific task at given time.");
             t.cancel();
             }
 }, 10000);
Ajay Kumar
  • 4,864
  • 1
  • 41
  • 44