title: How to stop a running TimerTask
body: <p>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:</p>
  
  <pre><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);
      }
  }
  </code></pre>