Timer: cancel()


import java.util.Timer;
import java.util.TimerTask;

public class Main {
  Timer timer;

  public Main(int seconds) {
    timer = new Timer();
    timer.schedule(new RemindTask(), seconds * 1000);
  }

  class RemindTask extends TimerTask {
    public void run() {
      System.out.println("Time's up!");
      timer.cancel(); //Terminate the timer thread
    }
  }

  public static void main(String args[]) {
    System.out.println("About to schedule task.");
    new Main(5);
    System.out.println("Task scheduled.");
  }
}
Home 
  Java Book 
    Thread Conncurrent  

Timer:
  1. Timer and TimerTask
  2. Timer: cancel()
  3. Timer: schedule(TimerTask task, Date firstTime, long period)
  4. Timer: scheduleAtFixedRate(TimerTask task, long delay, long period)
  5. extends TimerTask