code: public static void shutdownAndAwaitTermination(ExecutorService pool,
                                                     long timeToWait4ShutDown,
                                                     long timeToWait4ShutDownNow) {
          synchronized (pool) {
              // Disable new tasks from being submitted
              pool.shutdown();
          }
          try {
              // Wait a while for existing tasks to terminate
              if (!pool.awaitTermination(timeToWait4ShutDown, TimeUnit.SECONDS)) {
                  synchronized (pool) {
                      pool.shutdownNow(); // Cancel currently executing tasks
                  }
                  // Wait a while for tasks to respond to being cancelled
                  if (!pool.awaitTermination(timeToWait4ShutDownNow, TimeUnit.SECONDS)) {
                      Log.e(Log.TAG_DATABASE, "Pool did not terminate");
                  }
              }
          } catch (InterruptedException ie) {
              // (Re-)Cancel if current thread also interrupted
              synchronized (pool) {
                  pool.shutdownNow();
              }
              // Preserve interrupt status
              Thread.currentThread().interrupt();
          }
      }
docs: 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:
  http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/ExecutorService.html
  
  @param timeToWait4ShutDown    - Seconds
  @param timeToWait4ShutDownNow - Seconds