title: When does Java process die after shutdown hook?
body: <p>My problem is that my shutdown is happening too <em>early</em>, without waiting for the process to clean itself up.</p>
  
  <p>However, if in my shutdown Thread I add a <code>while(true) Thread.sleep(1000);</code>, the process cleans up fine, and obviously, never shuts down.</p>
  
  <p>This is confusing to me since the shutdown completes before many non-daemon threads are complete. For example, if I write a very simple test program in which the <code>main()</code> infinite-loops and the shutdown thread doesn't, <code>main</code> will be terminated anyway (without any exception trace coming from catching <code>InterruptedException</code>s).</p>
  
  <p>So as far as I can tell the shutdown happens when the shutdown thread <code>run</code> method completes.  I think from this knowledge I can fix this by ending it in a poll loop, polling for a <code>Future</code> that denotes successful shutdown of the rest of my processes. But this seems wrong - I really just want to wait for all non-daemon threads to shutdown, and I certainly don't want to do a poll.</p>
  
  <ol>
  <li>Am I correct that shutdown completes when shutdown hook's thread's <code>run</code> method completes?</li>
  <li>What is a good practice way to perform the shutdown, waiting for my actual shutdown tasks to complete?</li>
  </ol>