CompletableFuture的使用
2020-11-23 11:12:56 28 举报
AI智能生成
CompletableFuture 的使用
作者其他创作
大纲/内容
说明
是一个具体的类。实现了接口 Future 和 CompletionStage。不像FutureTask实现了Runnable接口,所以需要传入任务才可以执行
因为实现了Future接口,所以get() 等阻塞方法还可以使用
建议使用异步方法
构造方法
传入静态值
用来返回一个已经计算好的CompltableFuture
public static <U> CompletableFuture<U> completedFuture(U value)
传入异步执行代码
public static CompletableFuture<Void> runAsync(Runnable runnable)
public static CompletableFuture<Void> runAsync(Runnable runnable, Executor executor)
public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier)
public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor)
public static CompletableFuture<Void> runAsync(Runnable runnable, Executor executor)
public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier)
public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor)
注意
以Async结尾并且没有指定Executor的方法会使用ForkJoinPool.commonPool()作为它的线程池执行异步代码。
runAsync方法也好理解,它以Runnable函数式接口类型为参数,所以CompletableFuture的计算结果为空。
supplyAsync方法以Supplier<U>函数式接口类型为参数,CompletableFuture的计算结果类型为U。
阻塞获取结果
转换
说明
任务计算完成后,可以传给接下来的任务,返回最后任务的执行结果
方法
* 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 <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 CompletableFuture<Void> thenAcceptAsync(Consumer<? super T> action)
* public CompletableFuture<Void> thenAcceptAsync(Consumer<? super T> action, Executor executor)
组合另一个CompletionStage,纯消费
* 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> runAfterBoth(CompletionStage<?> other, Runnable 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> runAfterBoth(CompletionStage<?> other, Runnable action)
忽略任务的结果
* public CompletableFuture<Void> thenRun(Runnable action)
* public CompletableFuture<Void> thenRunAsync(Runnable action)
* public CompletableFuture<Void> thenRunAsync(Runnable action, Executor executor)
* public CompletableFuture<Void> thenRunAsync(Runnable action)
* public CompletableFuture<Void> thenRunAsync(Runnable action, Executor executor)
使用
组合
* 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)
* 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)
并行执行
* public <U,V> CompletableFuture<V> thenCombine(CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> fn)
* public <U,V> CompletableFuture<V> thenCombineAsync(CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> fn)
* public <U,V> CompletableFuture<V> thenCombineAsync(CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> fn, Executor executor)
* public <U,V> CompletableFuture<V> thenCombineAsync(CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> fn)
* public <U,V> CompletableFuture<V> thenCombineAsync(CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> fn, Executor executor)
返回新的CompletableFuture
使用
Either
acceptEither
当任意一个CompletionStage完成的时候,action这个消费者就会被执行。这个方法返回CompletableFuture<Void>
* public CompletableFuture<Void> acceptEither(CompletionStage<? extends T> other, Consumer<? super T> action)
* public CompletableFuture<Void> acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action)
* public CompletableFuture<Void> acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action, Executor executor)
* public CompletableFuture<Void> acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action)
* public CompletableFuture<Void> acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action, Executor executor)
applyToEither
当任意一个CompletionStage完成的时候,fn会被执行,它的返回值会当作新的CompletableFuture<U>的计算结果。
* public <U> CompletableFuture<U> applyToEither(CompletionStage<? extends T> other, Function<? super T,U> fn)
* public <U> CompletableFuture<U> applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T,U> fn)
* public <U> CompletableFuture<U> applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T,U> fn, Executor executor)
* public <U> CompletableFuture<U> applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T,U> fn)
* public <U> CompletableFuture<U> applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T,U> fn, Executor executor)
使用
辅助方法
allOf
allOf方法是当所有的CompletableFuture都执行完后执行计算。
public static CompletableFuture<Void> allOf(CompletableFuture<?>... cfs)
anyOf
anyOf方法是当任意一个CompletableFuture执行完后就会执行计算,计算的结果相同。
public static CompletableFuture<Object> anyOf(CompletableFuture<?>... cfs)
使用
0 条评论
下一页