title: How this java program keeps running even after main function exits?
body: <p>I am trying to learn the concurrency API of java. Below is a sample program.</p>
  
  <pre><code>    class WaitTest {
      public static void main(String[] args) throws InterruptedException, ExecutionException, TimeoutException {
          ExecutorService executorService = null;
          try {
              executorService = Executors.newSingleThreadExecutor();
              Future&lt;?&gt; future = executorService.submit(() -&gt;
                  {
                      for (int i = 0; i &lt; 100; i++) {
                          try {
                              Thread.sleep(100);
                          } catch (InterruptedException e) {
                              e.printStackTrace();
                          }
                          System.out.println("Printing " + i);
                      }
                  });
              future.get(5, TimeUnit.SECONDS);
              System.out.println("Reached successfully");
          } finally {
              if (executorService != null) {
                  executorService.shutdown();
              }
          }
      }
  }
  </code></pre>
  
  <p>The Runnable task being provided to the ExecutorService takes 10 seconds to complete. I have set a timeout of 5 seconds to get the result from the future object. So obviously the main method is exiting after 5 seconds because of the TimeoutException being thrown. But the Runnable task keeps on executing even after main method exits.</p>
  
  <p>Here is the output.</p>
  
  <pre><code>Printing 0
  Printing 1
  Printing 2
  Printing 3
  Printing 4
  Printing 5
  Printing 6
  Printing 7
  Printing 8
  Printing 9
  Printing 10
  Printing 11
  Printing 12
  Printing 13
  Printing 14
  Printing 15
  Printing 16
  Printing 17
  Printing 18
  Printing 19
  Printing 20
  Printing 21
  Printing 22
  Printing 23
  Printing 24
  Printing 25
  Printing 26
  Printing 27
  Printing 28
  Printing 29
  Printing 30
  Printing 31
  Printing 32
  Printing 33
  Printing 34
  Printing 35
  Printing 36
  Printing 37
  Printing 38
  Printing 39
  Printing 40
  Printing 41
  Printing 42
  Printing 43
  Exception in thread "main" java.util.concurrent.TimeoutException
      at java.util.concurrent.FutureTask.get(FutureTask.java:205)
      at ocp.WaitTest.main(ConcurrencyTest.java:89)
  Printing 44
  Printing 45
  Printing 46
  Printing 47
  Printing 48
  Printing 49
  Printing 50
  Printing 51
  Printing 52
  Printing 53
  Printing 54
  Printing 55
  Printing 56
  Printing 57
  Printing 58
  Printing 59
  Printing 60
  Printing 61
  Printing 62
  Printing 63
  Printing 64
  Printing 65
  Printing 66
  Printing 67
  Printing 68
  Printing 69
  Printing 70
  Printing 71
  Printing 72
  Printing 73
  Printing 74
  Printing 75
  Printing 76
  Printing 77
  Printing 78
  Printing 79
  Printing 80
  Printing 81
  Printing 82
  Printing 83
  Printing 84
  Printing 85
  Printing 86
  Printing 87
  Printing 88
  Printing 89
  Printing 90
  Printing 91
  Printing 92
  Printing 93
  Printing 94
  Printing 95
  Printing 96
  Printing 97
  Printing 98
  Printing 99
  </code></pre>
  
  <p>Any idea ?</p>