TimerTask run() in Java

run(): This method is available in java.util.TimerTask class of Java.

Syntax:

void java.util.TimerTask.run()

The action to be performed by this timer task.

Parameters: NA

Returns: NA

Exceptions: NA

Approach

Java

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class TimerTaskrun {
    public static void main(String[] args) {

        TimerTask task = new TimerTask() {

            @Override
            public void run() {
                System.out.println("Runner");
            }
        };
        Timer timer = new Timer();

        timer.scheduleAtFixedRate(task, new Date(), 1);

        task.run();

        task.cancel();
    }

}

Output:

Runner

Runner


No comments:

Post a Comment