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

Syntax

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

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

Example

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


//from  w  w  w  .  j av a2  s . c  o  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) {

     TimerTask tasknew = new MyTimerTask();
     Timer timer = new Timer();
     
     timer.scheduleAtFixedRate(tasknew,new Date(),1000);      
  }
  public void run() {
     System.out.println("working at fixed rate");      
  }    
}

The code above generates the following result.