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

Updated on: 26-Jun-2020

634 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements