Java Timer.schedule(TimerTask task, Date firstTime, long period)

Syntax

Timer.schedule(TimerTask task, Date firstTime, long period) has the following syntax.

public void schedule(TimerTask task,  Date firstTime,  long period)

Example

In the following code shows how to use Timer.schedule(TimerTask task, Date firstTime, long period) method.


//w  w  w .  j a v a 2 s .  co m

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


public class Main {
   public static void main(String[] args) {
      // creating timer task, timer
      TimerTask tasknew = new MyTimerTask();
      Timer timer = new Timer();
      
      // scheduling the task at interval
      timer.schedule(tasknew, new Date(),1000);      
   }
   // this method performs the task
   public void run() {
      System.out.println("working on");      
   }    
}

The code above generates the following result.