title: Java best solution for repeated task in Runnable
body: <p>What would you recommend me to achieve my goal? 
  I want to repeat <code>Robot</code> stuff (keys pressed) in a regular intervall. Description is also in my code.
  I´m using <code>eclipse neon with windowbuilder</code>(java/swing/jframe). </p>
  
  <pre><code>package patrick;
  
  import java.awt.AWTException;
  import java.awt.Robot;
  import java.awt.event.KeyEvent;
  import java.sql.Time;
  import java.util.TimerTask;
  import java.util.concurrent.Executors;
  import java.util.concurrent.ScheduledExecutorService;
  
  import javax.management.timer.Timer;
  
  public class Background implements Runnable {
  
      public boolean isRunning = true;
      int intervall = 0;
      Robot r = null;
  
  
      public Background(int i)
      {
          this.intervall = i;
      }
  
  
  
  
      @Override
      public void run() 
      {
          System.out.println("Thread: " + Thread.currentThread().getName() + " gestartet!");
          System.out.println("Intervall i: " + intervall);
          try {
              r = new Robot();
          } catch (AWTException e) {
              e.printStackTrace();
          }
          while(isRunning)
          {
              //int intervall is given me from my MainWindow Class and represents the minutes (for example I get a 5 which means 5 minutes)
              //now I want to do Robot stuff here that repeats in the given intervall time
          }
      }
  
      public void stop()
      {
          isRunning = false;
          System.out.println("Thread: " + Thread.currentThread().getName() + " gestoppt!");
      }
  
  }
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>Background is started from MainWindow Class like this:</p>
  
  <pre><code>bg = new Background(itmp);
          th1 = new Thread(bg);
          th1.start();
  </code></pre>
  
  <p>EDIT2: to first Answer</p>
  
  <p>Like this?</p>
  
  <pre><code>timer.scheduleAtFixedRate(
  while(isRunning)
          {
              //int intervall is given me from my MainWindow Class and represents the minutes (for example I get a 5 which means 5 minutes)
              //now I want to do Robot stuff here that repeats in the given intervall time
          }
  , delay, period);
  </code></pre>
  
  <p>EDIT3:</p>
  
  <pre><code>timer.scheduleAtFixedRate(new TimerTask() {
  
              @Override
              public void run() {
                  while(isRunning)
                  {
                      //int intervall is given me from my MainWindow Class and represents the minutes (for example I get a 5 which means 5 minutes)
                      //now I want to do Robot stuff here that repeats in the given intervall time
                  }
              }
          }, 0, intervall*6000);
  </code></pre>