title: Utility of Future.cancel(boolean) method
body: <p>I was simply exploring the java.util.concurrent package.</p>
  
  <p>I learnt that the class '<strong>Future</strong>' has a method <strong>boolean cancel(boolean mayInterruptIfRunning)</strong></p>
  
  <p>Please find attached the test code I wrote :</p>
  
  <pre><code>package com.java.util.concurrent;
  
  import java.util.concurrent.Callable;
  import java.util.concurrent.FutureTask;
  import java.util.concurrent.ScheduledFuture;
  import java.util.concurrent.ScheduledThreadPoolExecutor;
  
  public class FutureTester {
  
  /**
   * @param args
   * @throws InterruptedException
   */
  public static void main(String[] args) throws InterruptedException {
      // TODO Auto-generated method stub
      int poolCnt = 1;
      Callable&lt;NumberPrinter&gt; numberPrinter = null;
      ScheduledThreadPoolExecutor schPool = new ScheduledThreadPoolExecutor(
              poolCnt);
      ScheduledFuture&lt;NumberPrinter&gt;[] numPrinterFutures = new ScheduledFuture[poolCnt];
      FutureTask&lt;NumberPrinter&gt;[] futureTask = new FutureTask[poolCnt];
  
      for (int i = 0; i &lt; poolCnt; i++) {
          numberPrinter = new NumberPrinter();
          futureTask[i] = new FutureTask&lt;NumberPrinter&gt;(numberPrinter);
  
          /*
           * numPrinterFutures[i] = (ScheduledFuture&lt;NumberPrinter&gt;) schPool
           * .schedule(futureTask[i], 0, TimeUnit.MILLISECONDS);
           */
          numPrinterFutures[i] = (ScheduledFuture&lt;NumberPrinter&gt;) schPool
                  .submit(futureTask[i]);
      }
  
      //Thread.sleep(30);
  
      if (numPrinterFutures.length &gt; 0) {
  
          System.out.println("Task completed ? "
                  + numPrinterFutures[0].isDone());
  
          System.out.println("Task cancelled ? "
                  + numPrinterFutures[0].cancel(true));
  
          System.out.println("Is task cancelled ? "
                  + numPrinterFutures[0].isCancelled());
      }
  }
  
  }
  
  class NumberPrinter implements Callable&lt;NumberPrinter&gt; {
  
  private int counter = 10;
  
  @Override
  public NumberPrinter call() throws Exception {
      // TODO Auto-generated method stub
  
      while (counter &gt; 0) {
          if (Thread.interrupted()) {/*OUCH !!!*/
              return null;
          }
          System.out.println("counter = " + (counter--));
      }
  
      return this;
  }
  
  }
  </code></pre>
  
  <p>Intially,I assumed that cancelling a task will also stop the execution of a running thread(<strong><em>the 'OUCH' part NOT included</em></strong>).But I got the output as follows :</p>
  
  <pre><code>counter = 10
  Task completed ? false
  counter = 9
  Task cancelled ? true
  counter = 8
  Is task cancelled ? true
  counter = 7
  counter = 6
  counter = 5
  counter = 4
  counter = 3
  counter = 2
  counter = 1
  </code></pre>
  
  <p>On further reading on stackoverflow itself,it was said that </p>
  
  <ol>
  <li>The 'cancel' method can only stop the 'unstarted' jobs(which contradicts with the api description of the method)</li>
  <li>The cancel method simply interrupts the running thread which then
  must return from the run() method</li>
  </ol>
  
  <p><em><strong>Hence,I included the 'OUCH' part - a while loop checking for interruption</em></strong>;the output was as follows :</p>
  
  <pre><code>Task completed ? false
  counter = 10
  Task cancelled ? true
  Is task cancelled ? true
  </code></pre>
  
  <p><strong>QUESTION :</strong></p>
  
  <p><em>If one is supposed to write something analogous to the 'OUCH' part to stop the running thread,what is the utility/value of the cancel method. How does wrapping a Callable in a FutureTask help if the Thread can't be stopped by cancel? What is the design/conceptual/logical part that I am overlooking?</em></p>