一、Completable常用方法(二)
- 收尾處理
1.1 whenComplete方法
當(dāng)一個任務(wù)處理結(jié)束后,可以對得到的數(shù)據(jù)進(jìn)行處理。
| 返回值 |
方法名 |
介紹 |
| CompletableFuture<T> |
whenComplete(BiConsumer<? super T, ? super Throwable> action) |
當(dāng)任務(wù)完成時采取的處理并且可以對異常處理,使用ForkJoinPool線程池 |
| CompletableFuture<T> |
whenCompleteAsync(BiConsumer<? super T, ? super Throwable> action) |
當(dāng)任務(wù)完成時采取的處理并且可以對異常處理,使用ForkJoinPool線程池 |
| CompletableFuture<T> |
twhenCompleteAsync(BiConsumer<? super T, ? super Throwable> action, Executor executor) |
當(dāng)任務(wù)完成時采取的處理并且可以對異常處理,自定義線程池 |
CompletableFuture<String> future = CompletableFuture.supplyAsync(()->{
return "hello";
}).thenApply((w)->w+"world"+(1/0))
.whenCompleteAsync((w,throwable)->{
System.out.println(throwable.getMessage());
});
在代碼中有個ArithmeticException,在whenComplete時進(jìn)行了處理。
2.2 handle方法
handle方法與whenComplete方法類似,不同的是handle方法中傳入了一個BiFunction,意味著handle方法可以擁有返回值;
| 返回值 |
方法名 |
介紹 |
| CompletableFuture<U> |
handle( BiFunction<? super T, Throwable, ? extends U> fn) |
當(dāng)任務(wù)完成時采取的處理并且對異??梢蕴幚?,使用ForkJoinPool線程池 |
| CompletableFuture<U> |
handleAsync(BiFunction<? super T, Throwable, ? extends U> fn) |
當(dāng)任務(wù)完成時采取的處理并且對異??梢蕴幚恚褂肍orkJoinPool線程池 |
| CompletableFuture<U> |
handleAsync(BiFunction<? super T, Throwable, ? extends U> fn, Executor executor) |
當(dāng)任務(wù)完成時采取的處理并且對異??梢蕴幚?,自定義線程池 |
CompletableFuture<String> future = CompletableFuture.supplyAsync(()->{
return "hello";
}).thenApply((w)->w+"world"+(1/0))
.handleAsync((w,throwable)->{
System.out.println(throwable.getMessage());
return "好嗨哦";
});
future.get();
1.3 thenAccept方法
thenAccept方法沒有返回值,并且不能對異常進(jìn)行處理。
| 返回值 |
方法名 |
介紹 |
| CompletableFuture<Void> |
thenAccept(Consumer<? super T> action) |
當(dāng)任務(wù)完成時采取的處理,使用ForkJoinPool線程池 |
| CompletableFuture<Void> |
thenAcceptAsync(Consumer<? super T> action) |
當(dāng)任務(wù)完成時采取的處理,使用ForkJoinPool線程池 |
| CompletableFuture<Void> |
thenAcceptAsync(Consumer<? super T> action) |
當(dāng)任務(wù)完成時采取的處理,自定義線程池 |
CompletableFuture<Void> future = CompletableFuture.supplyAsync(()->{
return "hello";
}).thenApply((w)->w+"world")
.thenAcceptAsync((w)->{
System.out.println("真的有意思");
});
System.out.println(future.get());
- one of two
one of two 就是兩者中任意一個完成就發(fā)出通知;
2.1 acceptEither方法
| 返回值 |
方法名 |
介紹 |
| CompletableFuture<Void> |
acceptEither(CompletionStage<? extends T> other, Consumer<? super T> action) |
兩個任務(wù)完成其中一個就開始執(zhí)行action,使用ForkJoinPool線程池 |
| CompletableFuture<Void> |
acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action) |
兩個任務(wù)完成其中一個就開始執(zhí)行action,使用ForkJoinPool線程池 |
| CompletableFuture<Void> |
acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action, Executor executor) |
兩個任務(wù)完成其中一個就執(zhí)行action,自定義線程池 |
CompletableFuture<String> future1 = CompletableFuture.supplyAsync(()->{
return "HELLOW";
}).thenApply((w)->w+"WORLD");
CompletableFuture<String> future = CompletableFuture.supplyAsync(()->{
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "hello";
}).thenApply((w)->w+"world");
CompletableFuture<Void> future2 =
future.acceptEitherAsync(future1,(w)->System.out.println("我看看是哪個Hello world\n"+w));
2.2 applyToEither方法
applyToEither 和acceptEither方法類似,只是applyToEither方法可以返回結(jié)果,applyToEither方法傳入了Function接口
| 返回值 |
方法名 |
介紹 |
| CompletableFuture<U> |
applyToEither(CompletionStage<? extends T> other, Function<? super T, U> fn) |
兩個任務(wù)完成其中一個就開始執(zhí)行action,使用ForkJoinPool線程池 |
| CompletableFuture<U> |
applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T, U> fn) |
兩個任務(wù)完成其中一個就開始執(zhí)行action,使用ForkJoinPool線程池 |
| CompletableFuture<U> |
applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T, U> fn, |
| Executor executor) |
兩個任務(wù)完成其中一個就執(zhí)行action,自定義線程池 |
CompletableFuture<String> future1 = CompletableFuture.supplyAsync(()->{
return "HELLOW";
}).thenApply((w)->w+"WORLD");
CompletableFuture<String> future = CompletableFuture.supplyAsync(()->{
return "hello";
}).thenApply((w)->w+"world");
CompletableFuture<String> future2 =
future.applyToEither(future1,(w)->{return "我看看是哪個Hello world\n"+w;});
System.out.println(future2.get());
3 all of them
上面介紹了兩個任務(wù)完成一個就會通知,下面介紹許多(大于兩個)任務(wù)完成通知和許多任務(wù)完成才會通知。
3.1 allOf
| 返回值 |
方法名 |
介紹 |
| CompletableFuture<Void> |
applyToEitherallOf(CompletableFuture<?>... cfs) |
當(dāng)所有任務(wù)完成時會執(zhí)行此方法 |
CompletableFuture<String> future1 = CompletableFuture.supplyAsync(()->{
return "HELLOW";
}).thenApply((w)->w+"WORLD");
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(()->{
return "真的";
}).thenApply((w)->w+"有意思");
CompletableFuture<String> future = CompletableFuture.supplyAsync(()->{
return "頭發(fā)";
}).thenApply((w)->w+"快沒了");
CompletableFuture
.allOf(future1,future2,future)
.thenApply(v->Stream.of(future1,future2,future)
.map(t -> {
try {
return t.get();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
})
.collect(Collectors.toList()))
.thenAccept(System.out::println);
3.2 anyOf
| 返回值 |
方法名 |
介紹 |
| CompletableFuture<Void> |
anyOf(CompletableFuture<?>... cfs) |
當(dāng)其中一個任務(wù)完成時會執(zhí)行此方法 |
CompletableFuture<String> future1 = CompletableFuture.supplyAsync(()->{
return "HELLOW";
}).thenApply((w)->w+"WORLD");
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(()->{
return "真的";
}).thenApply((w)->w+"有意思");
CompletableFuture<String> future = CompletableFuture.supplyAsync(()->{
return "頭發(fā)";
}).thenApply((w)->w+"快沒了");
CompletableFuture<Object> future3 = CompletableFuture
.anyOf(future1,future2,future);
System.out.println(future3.get());
- 異常
當(dāng)執(zhí)行過程中出現(xiàn)異常,可以對異常進(jìn)行處理,當(dāng)然使用get也可以拋出異常。
| 返回值 |
方法名 |
介紹 |
| CompletableFuture<T> |
exceptionally(Function<Throwable, ? extends T> fn) |
當(dāng)異步任務(wù)執(zhí)行拋出異常時會執(zhí)行此方法 |
CompletableFuture<String> future1 = CompletableFuture.supplyAsync(()->{
return "HELLOW"+1/0;
}).thenApply((w)->w+"WORLD").exceptionally((throwable)->{
System.out.println(throwable);
return null;
});
System.out.println(future1.get());
以上就是CompletableFuture常用的方法,很多基于應(yīng)用,深入底層的很少,希望對想快速入手的朋友有很大的幫助。