本文主要是介绍CompletableFuture类总结),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
CompletableFuture类总结
```java
public class CompletableFuture<T> implements Future<T>, CompletionStage<T> {// 1、创建一个异步操作:无返回值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);// 2、计算结果完成时的回调方法:异步线程执行完毕就执行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);// 3、当一个线程依赖另一个线程时,可以使用 thenApply 方法来把这两个线程串行化。// T:上一个任务返回结果的类型// 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) // 4、执行任务完成时对结果的处理。// handle 方法和 thenApply 方法处理方式基本一样。不同的是 handle 是在任务完成后再执行,还可以处理异常的任务。thenApply 只可以执行正常的任务,任务出现异常则不执行 thenApply 方法。public <U> CompletionStage<U> handle(BiFunction<? super T, Throwable, ? extends U> fn);public <U> CompletionStage<U> handleAsync(BiFunction<? super T, Throwable, ? extends U> fn);public <U> CompletionStage<U> handleAsync(BiFunction<? super T, Throwable, ? extends U> fn,Executor executor); // 5、接收任务的处理结果,并消费处理,无返回结果。public CompletionStage<Void> thenAccept(Consumer<? super T> action);public CompletionStage<Void> thenAcceptAsync(Consumer<? super T> action);public CompletionStage<Void> thenAcceptAsync(Consumer<? super T> action,Executor executor); // 6、跟 thenAccept 方法不一样的是,不关心任务的处理结果。只要上面的任务执行完成,就开始执行 thenAccept 。public CompletionStage<Void> thenRun(Runnable action);public CompletionStage<Void> thenRunAsync(Runnable action);public CompletionStage<Void> thenRunAsync(Runnable action,Executor executor);// 7、合并任务public <U,V> CompletionStage<V> thenCombine(CompletionStage<? extends U> other,BiFunction<? super T,? super U,? extends V> fn);public <U,V> CompletionStage<V> thenCombineAsync(CompletionStage<? extends U> other,BiFunction<? super T,? super U,? extends V> fn);public <U,V> CompletionStage<V> thenCombineAsync(CompletionStage<? extends U> other,BiFunction<? super T,? super U,? extends V> fn,Executor executor);// 8、当两个CompletionStage都执行完成后,把结果一块交给thenAcceptBoth来进行消费public <U> CompletionStage<Void> thenAcceptBoth(CompletionStage<? extends U> other,BiConsumer<? super T, ? super U> action);public <U> CompletionStage<Void> thenAcceptBothAsync(CompletionStage<? extends U> other,BiConsumer<? super T, ? super U> action);public <U> CompletionStage<Void> thenAcceptBothAsync(CompletionStage<? extends U> other,BiConsumer<? super T, ? super U> action, Executor executor);// 9、两个CompletionStage,谁执行返回的结果快,我就用那个CompletionStage的结果进行下一步的转化操作。public <U> CompletionStage<U> applyToEither(CompletionStage<? extends T> other,Function<? super T, U> fn);public <U> CompletionStage<U> applyToEitherAsync(CompletionStage<? extends T> other,Function<? super T, U> fn);public <U> CompletionStage<U> applyToEitherAsync(CompletionStage<? extends T> other,Function<? super T, U> fn,Executor executor);// 10、两个CompletionStage,谁执行返回的结果快,我就用那个CompletionStage的结果进行下一步的消耗操作。public CompletionStage<Void> acceptEither(CompletionStage<? extends T> other,Consumer<? super T> action);public CompletionStage<Void> acceptEitherAsync(CompletionStage<? extends T> other,Consumer<? super T> action);public CompletionStage<Void> acceptEitherAsync(CompletionStage<? extends T> other,Consumer<? super T> action,Executor executor);// 11、两个CompletionStage,任何一个完成了都会执行下一步的操作(Runnable)public CompletionStage<Void> runAfterEither(CompletionStage<?> other,Runnable action);public CompletionStage<Void> runAfterEitherAsync(CompletionStage<?> other,Runnable action);public CompletionStage<Void> runAfterEitherAsync(CompletionStage<?> other,Runnable action,Executor executor);// 12、两个CompletionStage,都完成了计算才会执行下一步的操作(Runnable)public CompletionStage<Void> runAfterBoth(CompletionStage<?> other,Runnable action);public CompletionStage<Void> runAfterBothAsync(CompletionStage<?> other,Runnable action);public CompletionStage<Void> runAfterBothAsync(CompletionStage<?> other,Runnable action,Executor executor);// 13、thenCompose 方法允许你对两个 CompletionStage 进行流水线操作,第一个操作完成时,将其结果作为参数传递给第二个操作。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);// 14、allOf 可以把多个任务都完成之后执行public static CompletableFuture<Void> allOf(CompletableFuture<?>... cfs);// 15、anyOf多个任务中有一个任务完成就执行:只有其中的一个值public static CompletableFuture<Object> anyOf(CompletableFuture<?>... cfs);
}
示例```java
package com.qiqitrue.java.juc;import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;/*** @description:* @author: laiqi* @created: 2020-09-24 19:57*/
public class CompletableFutureExample {public static void main(String[] args) throws ExecutionException, InterruptedException {
// handle();// then();
// runAfterBothAsync();
// runAfterEitherAsync();allOf();}/*** 1、创建异步对象:无返回值的线程*/public static void runAsync() {System.out.println("主线程开始");CompletableFuture.runAsync(() -> {System.out.println("线程开始..");int a = 10 / 2;System.out.println("线程结束..");});System.out.println("主线程退出");}/*** 1、创建异步对象:有返回值的线程** @throws ExecutionException* @throws InterruptedException*/public static void supplyAsync() throws ExecutionException, InterruptedException {System.out.println("主线程开始");CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {System.out.println("线程开始..");int a = 10 / 2;System.out.println("线程结束..");return a;});Integer integer = completableFuture.get();System.out.println("主线程退出");}/*** 完成时回调执行:* 1、whenComplete:异步线程执行完毕就执行* 2、exceptionally:只有发生了异常才执行** @throws ExecutionException* @throws InterruptedException*/public static void whenComplete() throws ExecutionException, InterruptedException {System.out.println("主线程开始");CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {System.out.println("线程开始..");int a = 10 / 2;System.out.println("线程结束..");return a;}).whenComplete((integer, throwable) -> {System.out.println("我是whenComplete()方法:异步线程执行完毕之后就轮到我执行了,我可以获取返回值及异常信息,不能修改返回结果");}).exceptionally(throwable -> {System.out.println("我是exceptionally()方法:我只有在发生异常才会执行并返回出错时提供默认结果");return 0;});Integer integer = completableFuture.get();System.out.println("线程执行完毕返回值:" + integer);System.out.println("主线程退出");}/*** 完成时回调执行:* handle:* 当有异常时:whenComplete-->exceptionally-->handle* 当无异常时:whenComplete-->handle** @throws ExecutionException* @throws InterruptedException*/public static void handle() throws ExecutionException, InterruptedException {System.out.println("主线程开始");CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {System.out.println("线程开始..");int a = 10 / 2;System.out.println("线程结束..");return a;}).whenComplete((integer, throwable) -> {System.out.println("我是whenComplete()方法:异步线程执行完毕之后就轮到我执行了,我可以获取返回值及异常信息,不能修改返回结果");}).exceptionally(throwable -> {System.out.println("我是exceptionally()方法:我只有在发生异常才会执行并返回出错时提供默认结果");return 0;}).handle((integer, throwable) -> {System.out.println("我是handle()方法:我可以获取返回值及异常信息,并且可以修改返回结果");return 1;});Integer integer = completableFuture.get();System.out.println("线程执行完毕返回值:" + integer);System.out.println("主线程退出");}/*** 线程串行化* thenRunAsync:不可以接收前面的,自己不能有返回值* thenAcceptAsync:可以接收前面的返回值,自己不能有返回值* thenApplyAsync:可以接收前面的返回值,自己也需要返回值** @throws ExecutionException* @throws InterruptedException*/public static void then() throws ExecutionException, InterruptedException {String result = CompletableFuture.supplyAsync(() -> {System.out.println("线程A执行完毕");return "A";}).thenRunAsync(() -> {System.out.println("线程B执行完毕,我是没有返回值的");}).thenAcceptAsync(unused -> {System.out.println("线程C执行完毕,获取上个线程执行的返回值 = " + unused);}).thenApplyAsync(unused -> {System.out.println("线程D执行完毕,获取上个线程执行的返回值 = " + unused);return "D";}).thenApplyAsync(unused -> {System.out.println("线程E执行完毕,获取上个线程执行的返回值 = " + unused);return "E";}).get();System.out.println("result = " + result);}/*** 两任务组合-都要完成** @throws ExecutionException* @throws InterruptedException*/public static void runAfterBothAsync() throws ExecutionException, InterruptedException {// 异步任务一:返回1CompletableFuture<Integer> supplyAsync1 = CompletableFuture.supplyAsync(() -> {System.out.println("异步任务一");return 1;});// 异步任务二:返回2CompletableFuture<Integer> supplyAsync2 = CompletableFuture.supplyAsync(() -> {System.out.println("异步任务二");return 2;});// // runAfterBothAsync:表示需要两个任务都执行完,才触发任务;但是不能接收前面任务的返回值,自己也不需要返回值
// supplyAsync1.runAfterBothAsync(supplyAsync2, () -> {
// System.out.println("runAfterBothAsync:需要异步任务一、异步任务二都执行之后我才执行;但是不能接收前面任务的返回值,自己也不需要返回值");
// });// runAfterBothAsync:表示需要两个任务都执行完,才触发任务;可以接收前面任务的返回值,自己也不需要返回值
// supplyAsync1.thenAcceptBothAsync(supplyAsync2, (integer, integer2) -> {
// System.out.println("thenAcceptBothAsync:需要异步任务一、异步任务二都执行之后我才执行;可以接收前面任务的返回值,【任务一:"+integer+",任务二:"+integer2+"】,自己也不需要返回值");
// });// thenCombineAsync:表示需要两个任务都执行完,才触发任务;可以接收前面任务的返回值,自己也需要返回值supplyAsync1.thenCombineAsync(supplyAsync2, (integer, integer2) -> {System.out.println("thenAcceptBothAsync:需要异步任务一、异步任务二都执行之后我才执行;可以接收前面任务的返回值,【任务一:" + integer + ",任务二:" + integer2 + "】,自己也不需要返回值");return integer + integer2;});}/*** 两任务组合-两个任务中有一个任务完成就执行** @throws ExecutionException* @throws InterruptedException*/public static void runAfterEitherAsync() throws ExecutionException, InterruptedException {// 异步任务一:返回1CompletableFuture<Integer> supplyAsync1 = CompletableFuture.supplyAsync(() -> {System.out.println("异步任务一");return 1;});// 异步任务二:返回2CompletableFuture<Integer> supplyAsync2 = CompletableFuture.supplyAsync(() -> {try {// 为了效果明显起见,增加下任务二慢点执行Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("异步任务二");return 2;});// // runAfterEitherAsync:表示两个任务其一执行完就触发任务;但是不能接收前面任务的返回值,自己也不需要返回值
// supplyAsync1.runAfterEitherAsync(supplyAsync2, () -> {
// System.out.println("runAfterEitherAsync:需要异步任务一、异步任务二其中一个我就可以执行;但是不能接收前面任务的返回值,自己也不需要返回值");
// });// // acceptEitherAsync:表示两个任务其一执行完就触发任务;可以接收前面任务的返回值,自己也需要返回值
// supplyAsync1.acceptEitherAsync(supplyAsync2, integer -> {
// System.out.println("acceptEitherAsync:需要异步任务一、异步任务二其中一个我就可以执行;可以接收前面任务的返回值,自己也不需要返回值,前面值:" + integer);
// });// applyToEitherAsync:表示两个任务其一执行完就触发任务;可以接收前面任务的返回值,自己也需要返回值supplyAsync1.applyToEitherAsync(supplyAsync2, integer -> {System.out.println("applyToEitherAsync:需要异步任务一、异步任务二其中一个我就可以执行;可以接收前面任务的返回值,自己也需要返回值,前面值:" + integer);return 1 + integer;});}/*** 多个任务全部都执行完* allOf:全部都执行* anyOf: 执行完任一个就行** @throws ExecutionException* @throws InterruptedException*/public static void allOf() throws ExecutionException, InterruptedException {CompletableFuture<String> supplyAsyncA = CompletableFuture.supplyAsync(() -> {System.out.println("异步任务一");return "A";});CompletableFuture<String> supplyAsyncB = CompletableFuture.supplyAsync(() -> {System.out.println("异步任务二");return "B";});CompletableFuture<String> supplyAsyncC = CompletableFuture.supplyAsync(() -> {System.out.println("异步任务三");return "C";});// // allOf可以把多个任务都完成之后执行
// final CompletableFuture<Void> allOf = CompletableFuture.allOf(supplyAsyncA, supplyAsyncB, supplyAsyncC);
// // 阻塞等待
// allOf.get();
// System.out.println("异步任务一结果:" + supplyAsyncA.get());
// System.out.println("异步任务二结果:" + supplyAsyncB.get());
// System.out.println("异步任务三结果:" + supplyAsyncC.get());// anyOf多个任务中有一个任务完成就执行:只有其中的一个值final CompletableFuture<Object> anyOf = CompletableFuture.anyOf(supplyAsyncA, supplyAsyncB, supplyAsyncC);// 阻塞等待final Object rsult = anyOf.get();System.out.println("获取:" + rsult);}}
这篇关于CompletableFuture类总结)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!