title: Scheduling Java Threads with timer
body: <p>I have a Runnable class that needs to run after a set interval since it last ran.
  Example:  ProcessThread runs every 2 mins after it finishes.  So if I start ProcessThread at 1:00, and it takes 5 mins (finishing at 1:05), the next time it should run would be at 1:07.  If that one takes 3 mins to run, (finishing at 1:10), the next one starts at 1:12) and so on..</p>
  
  <p>I can't set it with a Fixed Rate of 2 minutes, because then a second Thread would be fired and the first one hasn't finished yet.</p>
  
  <p>So, here is my current code, but the way I have it, it keeps creating threads and never finishes them... so eventually my memory grows and grows:</p>
  
  <p>Main:</p>
  
  <pre><code>public class MyMain extends Thread{
     public static void main(String[] args)  {        
  
      ExecuteThread execute = new ExecuteThread();
      execute.start();
     }
  }
  </code></pre>
  
  <p>ExecuteThread (I took out the try-catch):</p>
  
  <pre><code>public void run() {
  
        MyProcessThread myProcess = new MyProcessThread();
        ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
  
        myProcess.start();
        while(myProcess.isAlive()){
           sleep(10000);
        }
  
        scheduler.schedule(this,myProcess.getDelaySeconds(),TimeUnit.SECONDS);
  
  }
  </code></pre>
  
  <p>Before I had the scheduler run inside the MyProcessThread, but it resulted in the same.  This looks to be the correct direction but something is still wrong.</p>