Class CancellableFuture<T>

  • All Implemented Interfaces:
    java.util.concurrent.CompletionStage<T>, java.util.concurrent.Future<T>

    public class CancellableFuture<T>
    extends java.util.concurrent.CompletableFuture<T>
    JDK's CompletableFuture does not handle cancellation well. When a `cancel` method is called on a derived future, created with a transformation method like `thenApply`, the cancellation is not propagated back to the original future. Try this code to see the difference: ``` public static void main(String[] args) throws InterruptedException { var f1 = CancellableFuture.from(new CompletableFuture<>()); var f2 = f1.thenApply(x ->x); f2.cancel(true); System.out.println(f1.isCancelled()); } ``` If you remove "CancellableFuture.from" call, you will get `false` instead of `true`
    • Nested Class Summary

      • Nested classes/interfaces inherited from class java.util.concurrent.CompletableFuture

        java.util.concurrent.CompletableFuture.AsynchronousCompletionTask
    • Constructor Summary

      Constructors 
      Constructor Description
      CancellableFuture​(java.util.concurrent.CompletableFuture<?> original)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean cancel​(boolean mayInterruptIfRunning)  
      static <U> CancellableFuture<U> from​(java.util.concurrent.CompletableFuture<U> original)  
      <U> java.util.concurrent.CompletableFuture<U> newIncompleteFuture()  
      • Methods inherited from class java.util.concurrent.CompletableFuture

        acceptEither, acceptEitherAsync, acceptEitherAsync, allOf, anyOf, applyToEither, applyToEitherAsync, applyToEitherAsync, complete, completeAsync, completeAsync, completedFuture, completedStage, completeExceptionally, completeOnTimeout, copy, defaultExecutor, delayedExecutor, delayedExecutor, exceptionally, failedFuture, failedStage, get, get, getNow, getNumberOfDependents, handle, handleAsync, handleAsync, isCancelled, isCompletedExceptionally, isDone, join, minimalCompletionStage, obtrudeException, obtrudeValue, orTimeout, runAfterBoth, runAfterBothAsync, runAfterBothAsync, runAfterEither, runAfterEitherAsync, runAfterEitherAsync, runAsync, runAsync, supplyAsync, supplyAsync, thenAccept, thenAcceptAsync, thenAcceptAsync, thenAcceptBoth, thenAcceptBothAsync, thenAcceptBothAsync, thenApply, thenApplyAsync, thenApplyAsync, thenCombine, thenCombineAsync, thenCombineAsync, thenCompose, thenComposeAsync, thenComposeAsync, thenRun, thenRunAsync, thenRunAsync, toCompletableFuture, toString, whenComplete, whenCompleteAsync, whenCompleteAsync
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • CancellableFuture

        public CancellableFuture​(java.util.concurrent.CompletableFuture<?> original)
    • Method Detail

      • cancel

        public boolean cancel​(boolean mayInterruptIfRunning)
        Specified by:
        cancel in interface java.util.concurrent.Future<T>
        Overrides:
        cancel in class java.util.concurrent.CompletableFuture<T>
      • newIncompleteFuture

        public <U> java.util.concurrent.CompletableFuture<U> newIncompleteFuture()
        Overrides:
        newIncompleteFuture in class java.util.concurrent.CompletableFuture<T>
      • from

        public static <U> CancellableFuture<U> from​(java.util.concurrent.CompletableFuture<U> original)