title: ScheduledExecutorService - program not ending after one-shot action
body: <p>I have a scheduled task in my program that closes a frame after a given period of time. However, after the task has been executed, the program keeps running as if the <code>ScheduledExecutorService</code> was still running on a different thread.</p>
  
  <p>This is the relevant part of my code:</p>
  
  <pre><code>int delay = 1000;
  
  ScheduledExecutorService ex = Executors.newSingleThreadScheduledExecutor();
  ex.schedule(() -&gt; {
  
      System.out.println("executed");
      getWindow().closeWindow();
      // ex.shutdown();
  
  }, delay, TimeUnit.MILLISECONDS);
  </code></pre>
  
  <p>Here the task is executed after a 1 second delay, "executed" is printed once, the frame closes, and the program keeps running even after this code. If I uncomment the <code>ex.shutdownNow();</code>, the program successfully ends as intended. However, I cannot figure out why this is happening. I also failed to find anything from the rest of the Internet.</p>
  
  <p><strong>MCVE:</strong></p>
  
  <pre><code>import java.util.concurrent.Executors;
  import java.util.concurrent.ScheduledExecutorService;
  import java.util.concurrent.TimeUnit;
  
  public class Main {
  
      public static void main(String[] args) {
          int delay = 1000;
  
          ScheduledExecutorService ex = Executors.newSingleThreadScheduledExecutor();
          ex.schedule(() -&gt; {
  
              System.out.println("executed");
              // ex.shutdown();
  
          }, delay, TimeUnit.MILLISECONDS);
      }
  
  }
  </code></pre>
  
  <hr>
  
  <p>The lambdas might've given it away, but this is indeed Java 8.</p>
  
  <p>Why is the program not stopping after the task has been executed?</p>