Timer: scheduleAtFixedRate(TimerTask task, long delay, long period)


import java.awt.Toolkit;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Schedule a task that executes once every second.
 */
public class Main {
  Toolkit toolkit;

  Timer timer;

  public Main() {
    toolkit = Toolkit.getDefaultToolkit();
    timer = new Timer();
    timer.scheduleAtFixedRate(new RemindTask(), 0, //initial delay
        1 * 1000); //subsequent rate
  }

  class RemindTask extends TimerTask {
    int numWarningBeeps = 3;

    public void run() {
      if (numWarningBeeps-- > 0) {
        long time = System.currentTimeMillis();
        if (time - scheduledExecutionTime() > 5) {
          return;
        }
        toolkit.beep();
        System.out.println("Beep!");
      } else {
        toolkit.beep();
        System.out.println("Time's up!");
        System.exit(0);
      }
    }
  }

  public static void main(String args[]) {
    new Main();
  }
}
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