Class CompletableFutureTask<V>

  • All Implemented Interfaces:
    java.lang.Runnable, java.util.concurrent.CompletionStage<V>, java.util.concurrent.Future<V>, java.util.concurrent.RunnableFuture<V>

    public class CompletableFutureTask<V>
    extends java.util.concurrent.FutureTask<V>
    implements java.util.concurrent.CompletionStage<V>
    Created by acurtis on 4/4/17.
    • Constructor Summary

      Constructors 
      Constructor Description
      CompletableFutureTask​(java.lang.Runnable runnable, V result)
      Creates a CompletableFutureTask that will, upon running, execute the given Runnable, and arrange that get will return the given result on successful completion.
      CompletableFutureTask​(java.util.concurrent.Callable<V> callable)
      Creates a CompletableFutureTask that will, upon running, execute the given Callable.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.util.concurrent.CompletionStage<java.lang.Void> acceptEither​(java.util.concurrent.CompletionStage<? extends V> other, java.util.function.Consumer<? super V> action)
      java.util.concurrent.CompletionStage<java.lang.Void> acceptEitherAsync​(java.util.concurrent.CompletionStage<? extends V> other, java.util.function.Consumer<? super V> action)
      java.util.concurrent.CompletionStage<java.lang.Void> acceptEitherAsync​(java.util.concurrent.CompletionStage<? extends V> other, java.util.function.Consumer<? super V> action, java.util.concurrent.Executor executor)
      <U> java.util.concurrent.CompletionStage<U> applyToEither​(java.util.concurrent.CompletionStage<? extends V> other, java.util.function.Function<? super V,​U> fn)
      <U> java.util.concurrent.CompletionStage<U> applyToEitherAsync​(java.util.concurrent.CompletionStage<? extends V> other, java.util.function.Function<? super V,​U> fn)
      <U> java.util.concurrent.CompletionStage<U> applyToEitherAsync​(java.util.concurrent.CompletionStage<? extends V> other, java.util.function.Function<? super V,​U> fn, java.util.concurrent.Executor executor)
      boolean cancel​(boolean mayInterruptIfRunning)
      protected void done()
      Use the methods provided by the CompletionStage interface to perform actions after the completion of the task.
      java.util.concurrent.CompletionStage<V> exceptionally​(java.util.function.Function<java.lang.Throwable,​? extends V> fn)
      V get()
      V get​(long timeout, java.util.concurrent.TimeUnit unit)
      <U> java.util.concurrent.CompletionStage<U> handle​(java.util.function.BiFunction<? super V,​java.lang.Throwable,​? extends U> fn)
      <U> java.util.concurrent.CompletionStage<U> handleAsync​(java.util.function.BiFunction<? super V,​java.lang.Throwable,​? extends U> fn)
      <U> java.util.concurrent.CompletionStage<U> handleAsync​(java.util.function.BiFunction<? super V,​java.lang.Throwable,​? extends U> fn, java.util.concurrent.Executor executor)
      boolean isCancelled()
      boolean isDone()
      java.util.concurrent.CompletionStage<java.lang.Void> runAfterBoth​(java.util.concurrent.CompletionStage<?> other, java.lang.Runnable action)
      java.util.concurrent.CompletionStage<java.lang.Void> runAfterBothAsync​(java.util.concurrent.CompletionStage<?> other, java.lang.Runnable action)
      java.util.concurrent.CompletionStage<java.lang.Void> runAfterBothAsync​(java.util.concurrent.CompletionStage<?> other, java.lang.Runnable action, java.util.concurrent.Executor executor)
      java.util.concurrent.CompletionStage<java.lang.Void> runAfterEither​(java.util.concurrent.CompletionStage<?> other, java.lang.Runnable action)
      java.util.concurrent.CompletionStage<java.lang.Void> runAfterEitherAsync​(java.util.concurrent.CompletionStage<?> other, java.lang.Runnable action)
      java.util.concurrent.CompletionStage<java.lang.Void> runAfterEitherAsync​(java.util.concurrent.CompletionStage<?> other, java.lang.Runnable action, java.util.concurrent.Executor executor)
      protected void set​(V v)
      protected void setException​(java.lang.Throwable t)
      java.util.concurrent.CompletionStage<java.lang.Void> thenAccept​(java.util.function.Consumer<? super V> action)
      java.util.concurrent.CompletionStage<java.lang.Void> thenAcceptAsync​(java.util.function.Consumer<? super V> action)
      java.util.concurrent.CompletionStage<java.lang.Void> thenAcceptAsync​(java.util.function.Consumer<? super V> action, java.util.concurrent.Executor executor)
      <U> java.util.concurrent.CompletionStage<java.lang.Void> thenAcceptBoth​(java.util.concurrent.CompletionStage<? extends U> other, java.util.function.BiConsumer<? super V,​? super U> action)
      <U> java.util.concurrent.CompletionStage<java.lang.Void> thenAcceptBothAsync​(java.util.concurrent.CompletionStage<? extends U> other, java.util.function.BiConsumer<? super V,​? super U> action)
      <U> java.util.concurrent.CompletionStage<java.lang.Void> thenAcceptBothAsync​(java.util.concurrent.CompletionStage<? extends U> other, java.util.function.BiConsumer<? super V,​? super U> action, java.util.concurrent.Executor executor)
      <U> java.util.concurrent.CompletionStage<U> thenApply​(java.util.function.Function<? super V,​? extends U> fn)
      <U> java.util.concurrent.CompletionStage<U> thenApplyAsync​(java.util.function.Function<? super V,​? extends U> fn)
      <U> java.util.concurrent.CompletionStage<U> thenApplyAsync​(java.util.function.Function<? super V,​? extends U> fn, java.util.concurrent.Executor executor)
      <U,​W>
      java.util.concurrent.CompletionStage<W>
      thenCombine​(java.util.concurrent.CompletionStage<? extends U> other, java.util.function.BiFunction<? super V,​? super U,​? extends W> fn)
      <U,​W>
      java.util.concurrent.CompletionStage<W>
      thenCombineAsync​(java.util.concurrent.CompletionStage<? extends U> other, java.util.function.BiFunction<? super V,​? super U,​? extends W> fn)
      <U,​W>
      java.util.concurrent.CompletionStage<W>
      thenCombineAsync​(java.util.concurrent.CompletionStage<? extends U> other, java.util.function.BiFunction<? super V,​? super U,​? extends W> fn, java.util.concurrent.Executor executor)
      <U> java.util.concurrent.CompletionStage<U> thenCompose​(java.util.function.Function<? super V,​? extends java.util.concurrent.CompletionStage<U>> fn)
      <U> java.util.concurrent.CompletionStage<U> thenComposeAsync​(java.util.function.Function<? super V,​? extends java.util.concurrent.CompletionStage<U>> fn)
      <U> java.util.concurrent.CompletionStage<U> thenComposeAsync​(java.util.function.Function<? super V,​? extends java.util.concurrent.CompletionStage<U>> fn, java.util.concurrent.Executor executor)
      java.util.concurrent.CompletionStage<java.lang.Void> thenRun​(java.lang.Runnable action)
      java.util.concurrent.CompletionStage<java.lang.Void> thenRunAsync​(java.lang.Runnable action)
      java.util.concurrent.CompletionStage<java.lang.Void> thenRunAsync​(java.lang.Runnable action, java.util.concurrent.Executor executor)
      java.util.concurrent.CompletableFuture<V> toCompletableFuture()
      java.lang.String toString()
      Returns a string identifying this CompletableFutureTask, as well as its completion state.
      java.util.concurrent.CompletionStage<V> whenComplete​(java.util.function.BiConsumer<? super V,​? super java.lang.Throwable> action)
      java.util.concurrent.CompletionStage<V> whenCompleteAsync​(java.util.function.BiConsumer<? super V,​? super java.lang.Throwable> action)
      java.util.concurrent.CompletionStage<V> whenCompleteAsync​(java.util.function.BiConsumer<? super V,​? super java.lang.Throwable> action, java.util.concurrent.Executor executor)
      • Methods inherited from class java.util.concurrent.FutureTask

        run, runAndReset
      • Methods inherited from class java.lang.Object

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

      • CompletableFutureTask

        public CompletableFutureTask​(java.util.concurrent.Callable<V> callable)
        Creates a CompletableFutureTask that will, upon running, execute the given Callable.
        Parameters:
        callable - the callable task
        Throws:
        java.lang.NullPointerException - if the callable is null
      • CompletableFutureTask

        public CompletableFutureTask​(java.lang.Runnable runnable,
                                     V result)
        Creates a CompletableFutureTask that will, upon running, execute the given Runnable, and arrange that get will return the given result on successful completion.
        Parameters:
        runnable - the runnable task
        result - the result to return on successful completion. If you don't need a particular result, consider using constructions of the form: CompletionStage<?> f = new CompletableFutureTask<Void>(runnable, null)
        Throws:
        java.lang.NullPointerException - if the runnable is null
    • Method Detail

      • isCancelled

        public boolean isCancelled()
        Specified by:
        isCancelled in interface java.util.concurrent.Future<V>
        Overrides:
        isCancelled in class java.util.concurrent.FutureTask<V>
      • isDone

        public boolean isDone()
        Specified by:
        isDone in interface java.util.concurrent.Future<V>
        Overrides:
        isDone in class java.util.concurrent.FutureTask<V>
      • cancel

        public boolean cancel​(boolean mayInterruptIfRunning)
        Specified by:
        cancel in interface java.util.concurrent.Future<V>
        Overrides:
        cancel in class java.util.concurrent.FutureTask<V>
      • get

        public V get()
              throws java.lang.InterruptedException,
                     java.util.concurrent.ExecutionException
        Specified by:
        get in interface java.util.concurrent.Future<V>
        Overrides:
        get in class java.util.concurrent.FutureTask<V>
        Throws:
        java.lang.InterruptedException
        java.util.concurrent.ExecutionException
      • get

        public V get​(long timeout,
                     @Nonnull
                     java.util.concurrent.TimeUnit unit)
              throws java.lang.InterruptedException,
                     java.util.concurrent.ExecutionException,
                     java.util.concurrent.TimeoutException
        Specified by:
        get in interface java.util.concurrent.Future<V>
        Overrides:
        get in class java.util.concurrent.FutureTask<V>
        Throws:
        java.lang.InterruptedException
        java.util.concurrent.ExecutionException
        java.util.concurrent.TimeoutException
      • done

        protected final void done()
        Use the methods provided by the CompletionStage interface to perform actions after the completion of the task. This method is final and may not be overridden.
        Overrides:
        done in class java.util.concurrent.FutureTask<V>
      • set

        protected void set​(V v)
        Overrides:
        set in class java.util.concurrent.FutureTask<V>
      • setException

        protected void setException​(java.lang.Throwable t)
        Overrides:
        setException in class java.util.concurrent.FutureTask<V>
      • thenApply

        public <U> java.util.concurrent.CompletionStage<U> thenApply​(java.util.function.Function<? super V,​? extends U> fn)
        Specified by:
        thenApply in interface java.util.concurrent.CompletionStage<V>
      • thenApplyAsync

        public <U> java.util.concurrent.CompletionStage<U> thenApplyAsync​(java.util.function.Function<? super V,​? extends U> fn)
        Specified by:
        thenApplyAsync in interface java.util.concurrent.CompletionStage<V>
      • thenApplyAsync

        public <U> java.util.concurrent.CompletionStage<U> thenApplyAsync​(java.util.function.Function<? super V,​? extends U> fn,
                                                                          java.util.concurrent.Executor executor)
        Specified by:
        thenApplyAsync in interface java.util.concurrent.CompletionStage<V>
      • thenAccept

        public java.util.concurrent.CompletionStage<java.lang.Void> thenAccept​(java.util.function.Consumer<? super V> action)
        Specified by:
        thenAccept in interface java.util.concurrent.CompletionStage<V>
      • thenAcceptAsync

        public java.util.concurrent.CompletionStage<java.lang.Void> thenAcceptAsync​(java.util.function.Consumer<? super V> action)
        Specified by:
        thenAcceptAsync in interface java.util.concurrent.CompletionStage<V>
      • thenAcceptAsync

        public java.util.concurrent.CompletionStage<java.lang.Void> thenAcceptAsync​(java.util.function.Consumer<? super V> action,
                                                                                    java.util.concurrent.Executor executor)
        Specified by:
        thenAcceptAsync in interface java.util.concurrent.CompletionStage<V>
      • thenRun

        public java.util.concurrent.CompletionStage<java.lang.Void> thenRun​(java.lang.Runnable action)
        Specified by:
        thenRun in interface java.util.concurrent.CompletionStage<V>
      • thenRunAsync

        public java.util.concurrent.CompletionStage<java.lang.Void> thenRunAsync​(java.lang.Runnable action)
        Specified by:
        thenRunAsync in interface java.util.concurrent.CompletionStage<V>
      • thenRunAsync

        public java.util.concurrent.CompletionStage<java.lang.Void> thenRunAsync​(java.lang.Runnable action,
                                                                                 java.util.concurrent.Executor executor)
        Specified by:
        thenRunAsync in interface java.util.concurrent.CompletionStage<V>
      • thenCombine

        public <U,​W> java.util.concurrent.CompletionStage<W> thenCombine​(java.util.concurrent.CompletionStage<? extends U> other,
                                                                               java.util.function.BiFunction<? super V,​? super U,​? extends W> fn)
        Specified by:
        thenCombine in interface java.util.concurrent.CompletionStage<V>
      • thenCombineAsync

        public <U,​W> java.util.concurrent.CompletionStage<W> thenCombineAsync​(java.util.concurrent.CompletionStage<? extends U> other,
                                                                                    java.util.function.BiFunction<? super V,​? super U,​? extends W> fn)
        Specified by:
        thenCombineAsync in interface java.util.concurrent.CompletionStage<V>
      • thenCombineAsync

        public <U,​W> java.util.concurrent.CompletionStage<W> thenCombineAsync​(java.util.concurrent.CompletionStage<? extends U> other,
                                                                                    java.util.function.BiFunction<? super V,​? super U,​? extends W> fn,
                                                                                    java.util.concurrent.Executor executor)
        Specified by:
        thenCombineAsync in interface java.util.concurrent.CompletionStage<V>
      • thenAcceptBoth

        public <U> java.util.concurrent.CompletionStage<java.lang.Void> thenAcceptBoth​(java.util.concurrent.CompletionStage<? extends U> other,
                                                                                       java.util.function.BiConsumer<? super V,​? super U> action)
        Specified by:
        thenAcceptBoth in interface java.util.concurrent.CompletionStage<V>
      • thenAcceptBothAsync

        public <U> java.util.concurrent.CompletionStage<java.lang.Void> thenAcceptBothAsync​(java.util.concurrent.CompletionStage<? extends U> other,
                                                                                            java.util.function.BiConsumer<? super V,​? super U> action)
        Specified by:
        thenAcceptBothAsync in interface java.util.concurrent.CompletionStage<V>
      • thenAcceptBothAsync

        public <U> java.util.concurrent.CompletionStage<java.lang.Void> thenAcceptBothAsync​(java.util.concurrent.CompletionStage<? extends U> other,
                                                                                            java.util.function.BiConsumer<? super V,​? super U> action,
                                                                                            java.util.concurrent.Executor executor)
        Specified by:
        thenAcceptBothAsync in interface java.util.concurrent.CompletionStage<V>
      • runAfterBoth

        public java.util.concurrent.CompletionStage<java.lang.Void> runAfterBoth​(java.util.concurrent.CompletionStage<?> other,
                                                                                 java.lang.Runnable action)
        Specified by:
        runAfterBoth in interface java.util.concurrent.CompletionStage<V>
      • runAfterBothAsync

        public java.util.concurrent.CompletionStage<java.lang.Void> runAfterBothAsync​(java.util.concurrent.CompletionStage<?> other,
                                                                                      java.lang.Runnable action)
        Specified by:
        runAfterBothAsync in interface java.util.concurrent.CompletionStage<V>
      • runAfterBothAsync

        public java.util.concurrent.CompletionStage<java.lang.Void> runAfterBothAsync​(java.util.concurrent.CompletionStage<?> other,
                                                                                      java.lang.Runnable action,
                                                                                      java.util.concurrent.Executor executor)
        Specified by:
        runAfterBothAsync in interface java.util.concurrent.CompletionStage<V>
      • applyToEither

        public <U> java.util.concurrent.CompletionStage<U> applyToEither​(java.util.concurrent.CompletionStage<? extends V> other,
                                                                         java.util.function.Function<? super V,​U> fn)
        Specified by:
        applyToEither in interface java.util.concurrent.CompletionStage<V>
      • applyToEitherAsync

        public <U> java.util.concurrent.CompletionStage<U> applyToEitherAsync​(java.util.concurrent.CompletionStage<? extends V> other,
                                                                              java.util.function.Function<? super V,​U> fn)
        Specified by:
        applyToEitherAsync in interface java.util.concurrent.CompletionStage<V>
      • applyToEitherAsync

        public <U> java.util.concurrent.CompletionStage<U> applyToEitherAsync​(java.util.concurrent.CompletionStage<? extends V> other,
                                                                              java.util.function.Function<? super V,​U> fn,
                                                                              java.util.concurrent.Executor executor)
        Specified by:
        applyToEitherAsync in interface java.util.concurrent.CompletionStage<V>
      • acceptEither

        public java.util.concurrent.CompletionStage<java.lang.Void> acceptEither​(java.util.concurrent.CompletionStage<? extends V> other,
                                                                                 java.util.function.Consumer<? super V> action)
        Specified by:
        acceptEither in interface java.util.concurrent.CompletionStage<V>
      • acceptEitherAsync

        public java.util.concurrent.CompletionStage<java.lang.Void> acceptEitherAsync​(java.util.concurrent.CompletionStage<? extends V> other,
                                                                                      java.util.function.Consumer<? super V> action)
        Specified by:
        acceptEitherAsync in interface java.util.concurrent.CompletionStage<V>
      • acceptEitherAsync

        public java.util.concurrent.CompletionStage<java.lang.Void> acceptEitherAsync​(java.util.concurrent.CompletionStage<? extends V> other,
                                                                                      java.util.function.Consumer<? super V> action,
                                                                                      java.util.concurrent.Executor executor)
        Specified by:
        acceptEitherAsync in interface java.util.concurrent.CompletionStage<V>
      • runAfterEither

        public java.util.concurrent.CompletionStage<java.lang.Void> runAfterEither​(java.util.concurrent.CompletionStage<?> other,
                                                                                   java.lang.Runnable action)
        Specified by:
        runAfterEither in interface java.util.concurrent.CompletionStage<V>
      • runAfterEitherAsync

        public java.util.concurrent.CompletionStage<java.lang.Void> runAfterEitherAsync​(java.util.concurrent.CompletionStage<?> other,
                                                                                        java.lang.Runnable action)
        Specified by:
        runAfterEitherAsync in interface java.util.concurrent.CompletionStage<V>
      • runAfterEitherAsync

        public java.util.concurrent.CompletionStage<java.lang.Void> runAfterEitherAsync​(java.util.concurrent.CompletionStage<?> other,
                                                                                        java.lang.Runnable action,
                                                                                        java.util.concurrent.Executor executor)
        Specified by:
        runAfterEitherAsync in interface java.util.concurrent.CompletionStage<V>
      • thenCompose

        public <U> java.util.concurrent.CompletionStage<U> thenCompose​(java.util.function.Function<? super V,​? extends java.util.concurrent.CompletionStage<U>> fn)
        Specified by:
        thenCompose in interface java.util.concurrent.CompletionStage<V>
      • thenComposeAsync

        public <U> java.util.concurrent.CompletionStage<U> thenComposeAsync​(java.util.function.Function<? super V,​? extends java.util.concurrent.CompletionStage<U>> fn)
        Specified by:
        thenComposeAsync in interface java.util.concurrent.CompletionStage<V>
      • thenComposeAsync

        public <U> java.util.concurrent.CompletionStage<U> thenComposeAsync​(java.util.function.Function<? super V,​? extends java.util.concurrent.CompletionStage<U>> fn,
                                                                            java.util.concurrent.Executor executor)
        Specified by:
        thenComposeAsync in interface java.util.concurrent.CompletionStage<V>
      • exceptionally

        public java.util.concurrent.CompletionStage<V> exceptionally​(java.util.function.Function<java.lang.Throwable,​? extends V> fn)
        Specified by:
        exceptionally in interface java.util.concurrent.CompletionStage<V>
      • whenComplete

        public java.util.concurrent.CompletionStage<V> whenComplete​(java.util.function.BiConsumer<? super V,​? super java.lang.Throwable> action)
        Specified by:
        whenComplete in interface java.util.concurrent.CompletionStage<V>
      • whenCompleteAsync

        public java.util.concurrent.CompletionStage<V> whenCompleteAsync​(java.util.function.BiConsumer<? super V,​? super java.lang.Throwable> action)
        Specified by:
        whenCompleteAsync in interface java.util.concurrent.CompletionStage<V>
      • whenCompleteAsync

        public java.util.concurrent.CompletionStage<V> whenCompleteAsync​(java.util.function.BiConsumer<? super V,​? super java.lang.Throwable> action,
                                                                         java.util.concurrent.Executor executor)
        Specified by:
        whenCompleteAsync in interface java.util.concurrent.CompletionStage<V>
      • handle

        public <U> java.util.concurrent.CompletionStage<U> handle​(java.util.function.BiFunction<? super V,​java.lang.Throwable,​? extends U> fn)
        Specified by:
        handle in interface java.util.concurrent.CompletionStage<V>
      • handleAsync

        public <U> java.util.concurrent.CompletionStage<U> handleAsync​(java.util.function.BiFunction<? super V,​java.lang.Throwable,​? extends U> fn)
        Specified by:
        handleAsync in interface java.util.concurrent.CompletionStage<V>
      • handleAsync

        public <U> java.util.concurrent.CompletionStage<U> handleAsync​(java.util.function.BiFunction<? super V,​java.lang.Throwable,​? extends U> fn,
                                                                       java.util.concurrent.Executor executor)
        Specified by:
        handleAsync in interface java.util.concurrent.CompletionStage<V>
      • toCompletableFuture

        public java.util.concurrent.CompletableFuture<V> toCompletableFuture()
        Specified by:
        toCompletableFuture in interface java.util.concurrent.CompletionStage<V>
      • toString

        public java.lang.String toString()
        Returns a string identifying this CompletableFutureTask, as well as its completion state. The state, in brackets, contains the String "Completed Normally" or the String "Completed Exceptionally", or the String "Not completed" followed by the number of CompletableFutures dependent upon its completion, if any.
        Overrides:
        toString in class java.util.concurrent.FutureTask<V>
        Returns:
        a string identifying this CompletableFutureTask, as well as its state