code: public static void shutdownAndAwaitTermination(ExecutorService pool, long timeoutMs) {
      pool.shutdown(); // Disable new tasks from being submitted
      try {
        // Wait a while for existing tasks to terminate
        if (!pool.awaitTermination(timeoutMs / 2, TimeUnit.MILLISECONDS)) {
          pool.shutdownNow(); // Cancel currently executing tasks
          // Wait a while for tasks to respond to being cancelled
          if (!pool.awaitTermination(timeoutMs / 2, TimeUnit.MILLISECONDS)) {
            LOG.warn("Pool did not terminate");
          }
        }
      } catch (InterruptedException ie) {
        Thread.currentThread().interrupt();
        // (Re-)Cancel if current thread also interrupted
        pool.shutdownNow();
      }
    }
docs: From https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ExecutorService.html
  
  The following method shuts down an ExecutorService in two phases, first by calling shutdown to
  reject incoming tasks, and then calling shutdownNow, if necessary, to cancel any lingering
  tasks.
  
  @param pool the executor service to shutdown
  @param timeoutMs how long to wait for the service to shut down