title: How can I replace Thread.sleep() in an infinite loop?
body: <p>I have an infinite loop inside my main, it runs a job which is also an infinite loop, and wait for it to throw an error. Then it sleeps for a given amount of time and starts the task again.</p>
  
  <pre><code>public static void main(String[] args) {
      while (true) {
          try {
              MyClass myClass = new MyClass();
              myClass.startInfiniteLoop();
          }
          catch (SomeException ex) {
              try {
                  Thread.sleep(MyClass.DEFAULT_SLEEP_TIME);
              }
              catch (InterruptedException ex2) {
                  System.exit(-1);
              }
          }
      }
  }
  </code></pre>
  
  <p>This works fine, but I wonder if this could be done better, perhaps with an <code>ExecutorService</code> as I (and my IDE) don't like <code>Thread.sleep()</code> in a <code>while (true)</code> loop.</p>
  
  <p>I have read a lot of questions and their answers about <code>ScheduledExecutorService</code> and task management, but I did not find this particular case since it's not really a schedule, I don't know if and when the task if going to end.</p>