/**
  * Main execution method for delayed or periodic tasks. If pool is shut down, rejects the task.
  * Otherwise adds task to queue and starts a thread, if necessary, to run it. (We cannot prestart
  * the thread to run the task because the task (probably) shouldn't be run yet,) If the pool is
  * shut down while the task is being added, cancel and remove it if required by state and
  * run-after-shutdown parameters.
  *
  * @param task the task
  */
 private void delayedExecute(RunnableScheduledFuture<?> task) {
   if (isShutdown()) reject(task);
   else {
     super.getQueue().add(task);
     if (isShutdown() && !canRunInCurrentRunState(task.isPeriodic()) && remove(task))
       task.cancel(false);
     else ensurePrestart();
   }
 }
 /**
  * Cancels and clears the queue of all tasks that should not be run due to shutdown policy.
  * Invoked within super.shutdown.
  */
 @Override
 void onShutdown() {
   BlockingQueue<Runnable> q = super.getQueue();
   boolean keepDelayed = getExecuteExistingDelayedTasksAfterShutdownPolicy();
   boolean keepPeriodic = getContinueExistingPeriodicTasksAfterShutdownPolicy();
   if (!keepDelayed && !keepPeriodic) {
     for (Object e : q.toArray())
       if (e instanceof RunnableScheduledFuture<?>) ((RunnableScheduledFuture<?>) e).cancel(false);
     q.clear();
   } else {
     // Traverse snapshot to avoid iterator exceptions
     for (Object e : q.toArray()) {
       if (e instanceof RunnableScheduledFuture) {
         RunnableScheduledFuture<?> t = (RunnableScheduledFuture<?>) e;
         if ((t.isPeriodic() ? !keepPeriodic : !keepDelayed)
             || t.isCancelled()) { // also remove if already cancelled
           if (q.remove(t)) t.cancel(false);
         }
       }
     }
   }
   tryTerminate();
 }