title: Cancelling Futures in Java
body: <p>Suppose, I have code something like this (note that my syntax is not 100% correct)</p>
  <pre><code>// This is how I am collecting the futures:
  int index = 0;
  for(Future&lt;Boolean&gt; future : futures) {
    try {
     Boolean canCancelOtherTasks = future.get();
     if (canCancelOtherTasks) {
        cancelAllFuturesFromIndex(index + 1, futures);
        break;
     }
    } catch (Exception e) {
       // handle exception
       break;
    }
    index++;
  }
  
  =====
  
  cancelAllFuturesFromIndex(index, futures) {
     for(int k = index; k &lt; futures.size(); k++) {
         futures.get(k).cancel(true);
     } 
  }
  </code></pre>
  <p>For a specific job, I essentially need to run N callables in parallel, and I would like to exit early after the first callable yields in true without having to wait for the other callables in the list. Note that the futures in the list are already in order of importance.</p>
  <p>Would the above work? Is this efficient (i.e. would it help with the overall runtime of my application assuming each callable has a task with different runtimes)?</p>