Invoke a task every 1 minute

In order to invoke a task / function periodically (say every one minute) we can use following code snippet.

Step 1: Create a class which calls “function” to be executed priodically as follow

import java.util.TimerTask;
public class ScheduledTask extends TimerTask {

// Add your function call here
public void run() {
now = new Date(); // initialize date
System.out.println(“This will be executed every 1 minute”);

Step 2: Create a class which schedules this function periodically
(Note: this is implemented in such a manner that it will run indefinitely)

import java.util.Timer;

public class SchedulerMain {
public static void main(String args[]) throws InterruptedException {

Timer time = new Timer(); // Timer Object
ScheduledTask st = new ScheduledTask(); // create an instance of class where our code needs to be executed periodically
time.schedule(st, 0, 60000); // call class instance repetitively task for every 1 min