title: Can't interrupt tasks of ExecutorService
body: <p><strong>Edit:</strong></p>
  
  <p>To test this problem outside of Android environment I've created a Java application that creates an <code>ExecutorService</code>, provides a task of <code>AttackScript</code> (identical class) and then terminates. </p>
  
  <p>This <strong>works 100% as expected</strong>, the thread is interrupted and the task is stopped. </p>
  
  <p>You don't even have to cancel a task by its <code>Future.cancel(true)</code>. <code>ExecutorService.shutdownNow()</code> does the job. Is there something in the Android's <code>Service</code> that somehow messes with the thread pool?</p>
  
  <p><em>Code that works as expcepted:</em></p>
  
  <pre><code>public static void main(String[] args) {
          AttackScript script = new AttackScript("http://ninjaflex.com/");
  
          ExecutorService executor = Executors.newFixedThreadPool(5);
          executor.submit(script);
          executor.submit(script);
          executor.submit(script);
          executor.submit(script);
  
          sleep(1300);
  
          // Automatically interrupts threads in the pool.
          executor.shutdownNow();
      }
  
      private static void sleep(long timeMilli){
          try {
              Thread.sleep(timeMilli);
          } catch(Exception e) {
              System.out.println("Error sleep()");
          }
      }
  </code></pre>
  
  <p><strong>Original post:</strong></p>
  
  <p>I have an Android <code>Service</code> where it includes an <code>ExecutorService</code> field, responsible to run some tasks.</p>
  
  <p>The tasks are objects of the <code>AttackScript</code> class. I cache the <code>Future</code> references in a <code>Map&lt;String,Future&gt;</code>, called tasks, so that I will be able to cancel them later.</p>
  
  <pre><code>Future future = executor.submit(new AttackScript(attack.getWebsite()));
  tasks.put(attack.getPushId(), future);
  </code></pre>
  
  <p>In <code>Service</code>'s<code>onDestroy()</code> (called when user presses a notification button) I am <strong>cancelling all tasks</strong></p>
  
  <pre><code>private void cancelAllTasks() {
      for (Map.Entry&lt;String, Future&gt; futureEntry : tasks.entrySet()) {
          futureEntry.getValue().cancel(true);
      }
  }
  </code></pre>
  
  <p>and then <strong>shutdown the executor</strong>:</p>
  
  <pre><code>private void shutdownThreadPool() {
       // https://www.baeldung.com/java-executor-service-tutorial
       executor.shutdown();
       try {
           if (executor.awaitTermination(800, TimeUnit.MILLISECONDS))
                  executor.shutdownNow();
       } catch (InterruptedException e) {
              executor.shutdownNow();
       }
  }
  </code></pre>
  
  <p>Finally here is the <strong>AttackScript</strong> class:</p>
  
  <pre><code>public class AttackScript implements Runnable {
      private static final String TAG = "AttackScript";
      private URL url;
  
      public AttackScript(String website) {
          initializeUrl(website);
      }
  
      private void initializeUrl(String website) {
          try {
              url = new URL(website);
          } catch (MalformedURLException e) {
              Log.e(TAG, "Wrong url?", e);
          }
      }
  
      @Override
      public void run() {
          while (!Thread.currentThread().isInterrupted()) {
              readUrl();
          }
          Log.d(TAG, "Stopped requesting from " + url + " server.");
      }
  
      private void readUrl() {
          InputStream in = null;
          try {
              in = url.openStream();
          } catch (IOException e) {
              Log.e(TAG, "openStream() error.", e);
          } finally {
              closeInputStream(in);
          }
      }
  
      private void closeInputStream(InputStream in) {
          try {
              in.close();
              Log.d(TAG, "InputStream closed for " + url);
          } catch (IOException e) {
              Log.e(TAG, "Error while closing the input stream.", e);
          }
      }
  }
  </code></pre>
  
  <p>The weird part is that rarely, like 1 out of 10, tasks are interrupted and <code>AttackScript</code>'s execution stops. But the other 9 the tasks were not interrupted, continuing to openStreams() on <code>URL</code>s.</p>