title: Java: How to make a piece of code run continuously
body: <p>I have a piece of Java code that lasts for 5 minutes
  and is scheduled to run the task every 60 seconds.</p>
  
  <p>Question: How do I change the code to make it run forever and and execute the task every 1 minute:</p>
  
  <p>Code:</p>
  
  <pre><code>public class MyTimerTask extends TimerTask{
  @Override
  public void run() {
      System.out.println("Timer task started at:" + new Date());
      completeTask();
      System.out.println("Timer task finished at:" + new Date());
  }
  
  private void completeTask() {
      //Task to be exeucted
  }
  
  public static void main(String args[]) {
      TimerTask timerTask = new MyTimerTask();
      // running timer task as daemon thread
      Timer timer = new Timer(true);
      // schedule the task to run every 1 minute
      timer.scheduleAtFixedRate(timerTask, 0, 60 * 1000);
      System.out.println("TimerTask started");
      // cancel after sometime
  
      try {
          Thread.sleep(300*1000);
      } catch (InterruptedException e) {
          e.printStackTrace();
      }
      //timer.cancel();
      System.out.println("TimerTask cancelled");
      try {
          Thread.sleep(30000);
      } 
  
      catch (InterruptedException e) {
          e.printStackTrace();
      }
  
     }
   }
  </code></pre>
  
  <p>Please advise</p>