Timer: schedule(TimerTask task, Date firstTime, long period) : Timer « java.util « Java by API






Timer: schedule(TimerTask task, Date firstTime, long period)

  
/*
 * Output:
Timer task executed.
Timer task executed.
Timer task executed.
Timer task executed.
Timer task executed. */

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

class MyTimerTask extends TimerTask {
  public void run() {
    System.out.println("Timer task executed.");
  }
}

public class MainClass {
  public static void main(String args[]) {
    MyTimerTask myTask = new MyTimerTask();
    Timer myTimer = new Timer();

    /*
     * Set an initial delay of 1 second, then repeat every half second.
     */
    myTimer.schedule(myTask, 1000, 500);

    try {
      Thread.sleep(5000);
    } catch (InterruptedException exc) {
    }

    myTimer.cancel();
  }
}
           
         
    
  








Related examples in the same category

1.new Timer()
2.Timer: cancel()
3.Timer: scheduleAtFixedRate(TimerTask task, long delay, long period)