Java Utility Library

Java TimerTask - run() Method



The java.util.TimerTask.run() method is used to specify the action to be performed by this timer task.

Syntax

public abstract void run()

Parameters

No parameter is required.

Return Value

void type.

Exception

NA.

Example:

In the example below, the java.util.TimerTask.run() method is used to specify the action to be performed by the given timer task.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a timer
    Timer timer = new Timer();

    //creating a timertask
    TimerTask tt = new TimerTask() {
      public void run() {
        for (int i = 0; i <= 5; i++) {
          System.out.println("Working on the task.");
        }
        System.out.println("Task is finished.");
      };
    };
    
    //scheduling the task to be executed  
    //after a delay of 100 milliseconds
    timer.schedule(tt, 100);
  }
}

The output of the above code will be:

Working on the task.
Working on the task.
Working on the task.
Working on the task.
Working on the task.
Working on the task.
Task is finished.

❮ Java.util - TimerTask