title: What is the best way to stop running tasks in threadpoolexecutor?
body: <p>I am implementing Java ThreadPoolExecutor in Android. I am required to stop and remove running tasks from my pool.</p>
  
  <p>I have implemented this by using submit(Runnable) and Future.cancel() methods.</p>
  
  <p>The code for submitting tasks is below:</p>
  
  <pre><code>public Future&lt;?&gt; submitTask(Runnable runnableTask) throws CustomException {
      if (runnableTask == null) {
          throw new CustomException("Null RunnableTask.");
      }
      Future&lt;?&gt; future = threadPoolExecutor.submit(runnableTask);
      return future;
  }
  </code></pre>
  
  <p>The Future returned by submit() is passed to method below.
  The code for cancelling tasks is below:</p>
  
  <pre><code>public void cancelRunningTask(Future&lt;?&gt; future) throws CustomException {
      if (future == null) {
          throw new CustomException("Null Future&lt;?&gt;.");
      }
      if (!(future.isDone() || future.isCancelled())) {
          if (future.cancel(true))
              MyLogger.d(this, "Running task cancelled.");
          else
              MyLogger.d(this, "Running task cannot be cancelled.");
      }
  }
  </code></pre>
  
  <p><strong>Problem :</strong> The tasks are not actually cancelled. Please let me know where I am wrong. Any help would be appreciated.</p>