In some application some tasks need to be run periodically for example GUI screen should update the data from server periodically.

This java tips illustrate a method of scheduling a task periodically. Developers may use it for repetitive invoking of a method as per their requirements.

For this aim, first you have to make a class extending TimerTask abstract class and write code in run method, you want to run repetitively.

 import java.util.TimerTask;

public class HeartBeatTask extends TimerTask{

  private int timerInterval;

  public HeartBeatTask(int timeInterval){

  public void run() {
    // add the task here


In your main program you can call this code to schedule task:

   java.util.Timer t1 = new java.util.Timer();

  HeartBeatTask tt = new HeartBeatTask(timeInterval);
  t1.schedule(tt, 0, timeInterval);

Now the task will repeat after the fixed time interval.