title: Thread.sleep causing unexpected behavior in ExecutorService loop
body: <p>In below test class just "before" is outputted.</p>
  
  <p>But if I removed Thread.sleep the following is outputted : </p>
  
  <pre><code>before
  after
  before
  after
  before
  after
  before
  after
  before
  after
  before
  after
  before
  after
  before
  after
  before
  after
  before
  after
  </code></pre>
  
  <p>Which is what I expect.</p>
  
  <p>When I use Thread.sleep I think that there should be a 1000ms delay between each output of </p>
  
  <pre><code>before
  after
  </code></pre>
  
  <p>Why is this not occuring &amp; how can the code be amended so that the generated output is same regardless of calling sleep or not ?</p>
  
  <pre><code>import java.util.concurrent.Executors;
  import java.util.concurrent.ScheduledExecutorService;
  import org.junit.Test;
  
  public class RunExecutorTest {
  
      private ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
  
      private class RunTest implements Runnable {
          public void run() {
  
              System.out.println("before");
  
              try {
                  Thread.sleep(1000);
              } catch (InterruptedException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
  
              System.out.println("after");
          }
      }
  
      @Test
      public void testRun() {
  
          Runnable r = new RunTest();
  
          for(int counter = 0; counter &lt; 10; ++counter) {  
              executor.execute(r);
          }
      }
  
  }
  </code></pre>
  
  <p>Update from reading comment below "@user470184 It doesn't work in junit test cases because Junit kills all threads when the main thread completes. – Sotirios Delimanolis" if I add</p>
  
  <pre><code>try {
      Thread.sleep(10000);
  } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
  }
  </code></pre>
  
  <p>at the end of test method then it works as expected. </p>