Java Timer.schedule(TimerTask task, long delay)

Syntax

Timer.schedule(TimerTask task, long delay) has the following syntax.

public void schedule(TimerTask task,  long delay)

Example

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


/*from  w  w w .jav a 2 s  .c  om*/
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, 100);      
   }
   // this method performs the task
   public void run() {
      System.out.println("timer working");      
   }    
}

The code above generates the following result.