// 阻塞式等待任务执行完成,获得执行结果或异常,抛出的异常为checked exception public T get() // 等待超时时间后,抛出TimeoutException public T get(long timeout, TimeUnit unit) //立即获得结果,如果结果已经计算完则返回结果或者抛出异常,否则返回给定的valueIfAbsent值。 public T getNow(T valueIfAbsent) // 阻塞式等待任务执行完成,获得执行结果或异常,抛出的异常为unchecked exception(CompletionException) public T join()
//If not already completed, sets the value returned by get() and related methods to the given value. publicbooleancomplete(T value) //f not already completed, causes invocations of get() and related methods to throw the given exception. publicbooleancompleteExceptionally(Throwable ex) //Forcibly sets or resets the value subsequently returned by method get() and related methods, whether or not already completed. This method is designed for use only in error recovery actions, and even in such situations may result in ongoing dependent completions using established versus overwritten outcomes. publicvoidobtrudeValue(T value) //Forcibly causes subsequent invocations of method get() and related methods to throw the given exception, whether or not already completed. This method is designed for use only in error recovery actions, and even in such situations may result in ongoing dependent completions using established versus overwritten outcomes. publicvoidobtrudeException(Throwable ex)
当completableFuture计算结果完成,或者抛出异常的时候,可以通过如下方法指定特定的Action。action参数的类型为(BiConsumer<? super T,? super Throwable>,代表可以处理正常的计算结果,也可以处理异常情况。when开头的方法当action执行完成后,会返回原始的CompletableFuture计算结果或异常,而exceptionally则返回一个新的CompletableFuture对象。exceptionally用来处理当原始CompletableFuture抛出异常的时候,触发计算,否则以原始CompletableFuture作为返回结果。方法不以Async结尾,意味着Action使用相同的线程执行,而Async可能会使用其它的线程去执行(如果使用相同的线程池,也可能会被同一个线程选中执行)
1 2 3 4
public CompletableFuture<T> whenComplete(BiConsumer<? super T,? super Throwable> action) public CompletableFuture<T> whenCompleteAsync(BiConsumer<? super T,? super Throwable> action) public CompletableFuture<T> whenCompleteAsync(BiConsumer<? super T,? super Throwable> action, Executor executor) public CompletableFuture<T> exceptionally(Function<Throwable, ? extends T> fn)
public <U> CompletableFuture<U> handle(BiFunction<? super T,Throwable,? extends U> fn) public <U> CompletableFuture<U> handleAsync(BiFunction<? super T,Throwable,? extends U> fn) public <U> CompletableFuture<U> handleAsync(BiFunction<? super T,Throwable,? extends U> fn, Executor executor)
java.util.concurrent.CompletionException: java.lang.ArithmeticException: / by zero java.util.concurrent.CompletionException: java.lang.ArithmeticException: / by zero
public <U> CompletableFuture<U> thenApply(Function<? super T,? extends U> fn) public <U> CompletableFuture<U> thenApplyAsync(Function<? super T,? extends U> fn) public <U> CompletableFuture<U> thenApplyAsync(Function<? super T,? extends U> fn, Executor executor)
public CompletableFuture<Void> thenAccept(Consumer<? super T> action) public CompletableFuture<Void> thenAcceptAsync(Consumer<? super T> action) public CompletableFuture<Void> thenAcceptAsync(Consumer<? super T> action, Executor executor)
public <U> CompletableFuture<Void> thenAcceptBoth(CompletionStage<? extends U> other, BiConsumer<? super T,? super U> action) public <U> CompletableFuture<Void> thenAcceptBothAsync(CompletionStage<? extends U> other, BiConsumer<? super T,? super U> action) public <U> CompletableFuture<Void> thenAcceptBothAsync(CompletionStage<? extends U> other, BiConsumer<? super T,? super U> action, Executor executor)
public CompletableFuture<Void> thenRun(Runnable action) public CompletableFuture<Void> thenRunAsync(Runnable action) public CompletableFuture<Void> thenRunAsync(Runnable action, Executor executor) public CompletableFuture<Void> runAfterBoth(CompletionStage<?> other, Runnable action)
public <U> CompletableFuture<U> thenCompose(Function<? super T,? extends CompletionStage<U>> fn) public <U> CompletableFuture<U> thenComposeAsync(Function<? super T,? extends CompletionStage<U>> fn) public <U> CompletableFuture<U> thenComposeAsync(Function<? super T,? extends CompletionStage<U>> fn, Executor executor)