title: Java Executor Best Practices for Tasks that Should Run Forever
body: <p>I'm working on a Java project where I need to have multiple tasks running asynchronously.  I'm led to believe Executor is the best way for me to do this, so I'm familiarizing myself with it.  (Yay getting paid to learn!)  However, it's not clear to me what the best way is to accomplish what I'm trying to do.</p>
  
  <p>For the sake of argument, let's say I have two tasks running.  Neither is expected to terminate, and both should run for the duration of the application's life.  I'm trying to write a main wrapper class such that:</p>
  
  <ul><li>If either task throws an exception, the wrapper will catch it and restart the task.</li>
  <li>If either task runs to completion, the wrapper will notice and restart the task.</li>
  </ul>
  
  <p>Now, it should be noted that the implementation for both tasks will wrap the code in <code>run()</code> in an infinite loop that will never run to completion, with a try/catch block that should handle all runtime exceptions without disrupting the loop.  I'm trying to add another layer of certainty; if either I or somebody who follows me does something stupid that defeats these safeguards and halts the task, the application needs to react appropriately.</p>
  
  <p>Is there a best practice for approaching this problem that folks more experienced than me would recommend?  </p>
  
  <p>FWIW, I've whipped-up this test class:</p>
  
  <pre><code>
  public class ExecTest {
  
     private static ExecutorService executor = null;
     private static Future results1 = null;
     private static Future results2 = null;
  
     public static void main(String[] args) {
        executor = Executors.newFixedThreadPool(2);
        while(true) {
           try {
              checkTasks();
              Thread.sleep(1000);
           }
           catch (Exception e) {
              System.err.println("Caught exception: " + e.getMessage());
           }
        }
     }
  
     private static void checkTasks() throws Exception{
        if (results1 == null || results1.isDone() || results1.isCancelled()) {
           results1 = executor.submit(new Test1());
        }
  
        if (results2 == null || results2.isDone() || results2.isCancelled()) {
           results2 = executor.submit(new Test2());
        }
     }
  }
  
  class Test1 implements Runnable {
     public void run() {
        while(true) {
           System.out.println("I'm test class 1");
           try {Thread.sleep(1000);} catch (Exception e) {}
        }
  
     }
  }
  
  class Test2 implements Runnable {
     public void run() {
        while(true) {
           System.out.println("I'm test class 2");
           try {Thread.sleep(1000);} catch (Exception e) {}
        }
     }
  }
  </code></pre>
  
  <p>It's behaving the way I want, but I don't know if there are any gotchas, inefficiencies, or downright wrong-headedness waiting to surprise me.  (In fact, given that I'm new to this, I'd be shocked if there <i>wasn't</i> something wrong/inadvisable about it.)</p>
  
  <p>Any insight is welcomed.</p>