Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop a running TimerTask

Tags:

I am trying to make a simple timer that plays a beep after the specified number of seconds. I managed to get it to work, but the TimerTask continues to run after the beep. Now do I stop execution? Here is my code:

import java.util.Scanner; import java.util.Timer; import java.util.TimerTask; import java.awt.Toolkit;  class Alarm {      public static void main(String[] args) {         long delay;         Scanner scan = new Scanner(System.in);         System.out.print("Enter a delay in seconds: ");         delay = scan.nextInt()*1000;          Timer timer = new Timer();          TimerTask task = new TimerTask() {             @Override             public void run() {                 Toolkit.getDefaultToolkit().beep();             }         };          timer.schedule(task, delay);     } } 
like image 647
Zim Avatar asked Apr 19 '13 09:04

Zim


People also ask

How do you stop a TimerTask?

In order to cancel the Timer Task in Java, we use the java. util. TimerTask. cancel() method.

Which method is used to stop Timer?

The cancel() method of Timer class is used to terminate this timer and remove any currently scheduled tasks.

What is the relationship between Timer and TimerTask?

Timer provides method to schedule Task where the task is an instance of TimerTask class, which implements the Runnable interface and overrides run() method to define task which is called on scheduled time.

What is TimerTask?

TimerTask is an abstract class defined in java. util package. TimerTask class defines a task that can be scheduled to run for just once or for repeated number of time. In order to define a TimerTask object, this class needs to be implemented and the run method need to be overridden.


2 Answers

You need to cancel the timer by calling the following methods

timer.cancel();  // Terminates this timer, discarding any currently scheduled tasks. timer.purge();   // Removes all cancelled tasks from this timer's task queue. 

This will cancel the task, so something like this would work:

import java.util.Scanner; import java.util.Timer; import java.util.TimerTask; import java.awt.Toolkit;  class Alarm {      private static boolean run = true;      public static void main(String[] args) {         long delay;         Scanner scan = new Scanner(System.in);         System.out.print("Enter a delay in seconds: ");         delay = scan.nextInt()*1000;          final Timer timer = new Timer();          final TimerTask task = new TimerTask() {             @Override             public void run() {                 if(run) {                    Toolkit.getDefaultToolkit().beep();                 } else {                    timer.cancel();                    timer.purge();                 }             }         };          timer.schedule(task, delay);          // set run to false here to stop the timer.         run = false;     } } 
like image 116
Erik Pragt Avatar answered Oct 04 '22 13:10

Erik Pragt


Here is what worked for me (used the purge() suggestion also):

import java.util.Scanner; import java.util.Timer; import java.util.TimerTask; import java.awt.Toolkit;  class Alarm {      public static void main(String[] args) {         long delay;         Scanner scan = new Scanner(System.in);         System.out.print("Enter a delay in seconds: ");         delay = scan.nextInt()*1000;          final Timer timer = new Timer();          final TimerTask task = new TimerTask() {             @Override             public void run() {                 Toolkit.getDefaultToolkit().beep();                 timer.cancel();                 timer.purge();             }         };          timer.schedule(task, delay);     } } 
like image 27
Zim Avatar answered Oct 04 '22 12:10

Zim