title: Graceful shutdown of threads and executor
body: <p>The following piece of code tries to accompolish this.</p>
  
  <p>The code loops forever and checks if there are any pending requests to be processed. If there is any, it creates a new thread to process the request and submits it to the executor. Once all the threads are done,it sleeps for 60 seconds and again checks for pending requests.</p>
  
  <pre><code>public static void main(String a[]){
      //variables init code omitted
      ExecutorService service = Executors.newFixedThreadPool(15);
      ExecutorCompletionService&lt;Long&gt; comp = new ExecutorCompletionService&lt;Long&gt;(service);
      while(true){
          List&lt;AppRequest&gt; pending = service.findPendingRequests();
          int noPending = pending.size();
          if (noPending &gt; 0) {
              for (AppRequest req : pending) {
                  Callable&lt;Long&gt; worker = new RequestThread(something, req);
                  comp.submit(worker);
              }
          }
          for (int i = 0; i &lt; noPending; i++) {
              try {
                  Future&lt;Long&gt; f = comp.take();
                  long name;
                  try {
                      name = f.get();
                      LOGGER.debug(name + " got completed");
                  } catch (ExecutionException e) {
                      LOGGER.error(e.toString());
                  }
              } catch (InterruptedException e) {
                  LOGGER.error(e.toString());
              }
          }
          TimeUnit.SECONDS.sleep(60);
      }
  
    }
  </code></pre>
  
  <p>My question is most of the processing done by these threads deal with database. And this program will run on a windows machine. What happens to these threads when someone tries to shutdown or logoff the machine.? How to gracefully shutdown the running threads and also the executor.?</p>