Call the run() method of the Timer Task in Java



The java.util.TimerTask.run() method looks onto the action to be performed by the task. It is used to carry out the action performed by the task.

Declaration −The java.util.TimerTask.run() method is declared as follows −

public abstract void run()

Let us see an example program to call the run() method of the Timer Task −

Example

 Live Demo

import java.util.*;
class MyTask extends TimerTask {
   public void run() {
      System.out.println("Running");
   }
}
public class Example {
   public static void main(String[] args) {
      // creating timer task, timer
      TimerTask task = new MyTask();
      Timer timer = new Timer();
      // scheduling the task
      timer.scheduleAtFixedRate(task, new Date(), 3000);
   }
}

Output

Running
Running
Running
Running

Advertisements