title: How to interrupt underlying execution of CompletableFuture
body: <p>I know that <code>CompletableFuture</code> design does not control its execution with interruptions, but I suppose some of you might have this problem. <code>CompletableFuture</code>s are very good way to compose async execution, but given the case when you want the underlying execution to be interrupted or stopped when future is canceled, how do we do that? Or we must just accept that any canceled or manually completed <code>CompletableFuture</code> will not impact the thread working out there to complete it?</p>
  
  <p>That is, in my opinion, obviously a useless work that takes time of executor worker. I wonder what approach or design might help in this case?</p>
  
  <p><strong>UPDATE</strong> </p>
  
  <p>Here is a simple test for this</p>
  
  <pre><code>public class SimpleTest {
  
    @Test
    public void testCompletableFuture() throws Exception {
      CompletableFuture&lt;Void&gt; cf = CompletableFuture.runAsync(()-&gt;longOperation());
  
      bearSleep(1);
  
      //cf.cancel(true);
      cf.complete(null);
  
      System.out.println("it should die now already");
      bearSleep(7);
    }
  
    public static void longOperation(){
      System.out.println("started");
      bearSleep(5);
      System.out.println("completed");
    }
  
    private static void bearSleep(long seconds){
      try {
        TimeUnit.SECONDS.sleep(seconds);
      } catch (InterruptedException e) {
        System.out.println("OMG!!! Interrupt!!!");
      }
    }
  }
  </code></pre>