code: private void shutdownAndAwaitTermination( ExecutorService pool, int queueSize )
      {
          log.info( "Shutting down executor service; queue size={}.", queueSize );
  
          pool.shutdown(); // Disable new tasks from being submitted
  
          log.info( "Waiting for executor service to finish all queued tasks." );
  
          try
          {
              // Wait a while for existing tasks to terminate
              if ( ! pool.awaitTermination( 60, TimeUnit.SECONDS ) )
              {
                  log.info( "Executor service did not terminate before timeout; forcing shutdown." );
  
                  List<Runnable> queuedTasks = pool.shutdownNow(); // Cancel currently executing tasks
  
                  log.info( "Discarded {} tasks that were still queued; still waiting for executor service to terminate."
                          , queuedTasks.size() );
  
                  // Wait a while for tasks to respond to being cancelled
                  if ( ! pool.awaitTermination( 60, TimeUnit.SECONDS ) )
                  {
                      log.error( "Pool did not terminate." );
                  }
                  else
                  {
                      log.info( "Executor service terminated after forced shutdown." );
                  }
              }
              else
              {
                  log.info( "Executor service finished all queued tasks and terminated." );
              }
          }
          catch ( InterruptedException ie )
          {
              log.error( "Interrupted while waiting for pool to terminate; forcing shutdown.", ie );
  
              // (Re-)Cancel if current thread also interrupted
              List<Runnable> queuedTasks = pool.shutdownNow();
  
              log.info( "Discarded {} tasks that were still queued; no longer waiting for executor service to terminate."
                      , queuedTasks.size() );
  
              // Preserve interrupt status
              Thread.currentThread().interrupt();
          }
      }
docs: Adapted from example in javadoc of ExecutorService.