1.创建异步对象
四个静态方法创建一个异步操作,runXXX都是没有返回结果的,supplyXXX都是有带返回结果的。
分别带有默认线程池或者自定义线程池。其中Supplier是一个函数式接口,代表是一个生成者的意思,传入0个参数,返回一个结果
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.计算完成时回调方法
侧重对该任务结果的处理
- whenComplete处理正常和异常(只能得到异常)的计算结果,exceptionallu处理异常情况并返回自定制
- 带不带Async区别:带了就是新任务提交给线程池处理;不带就是这个线程继续处理
- BiConsumer有两个参数:第一个是结果,第二个是异常
Function
fn:方法:参数是异常,需要返回值。出现异常后默认返回什么。 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.handle方法
对结果处理的更全面
BiFunction方法:两个参数:结果、异常。返回自定义逻辑值。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)
4.线程串行化
thenApply:当一个任务依赖另一个任务时,获取上一个任务的返回结果,并返回当前任务的返回值。接收使用上一个任务结果,返回自己任务结果。
- thenAccept:消费处理结果。接收任务的处理结果并消费,无返回。接收使用上一个任务结果,不返回。
- thenRun:只要上面的任务执行完成就执行,不需要上个任务的返回结果。只是处理完任务后,执行后续操作。不接收上一个任务的 结果,不返回。 ```java public CompletableFuture thenApply(Function<? super T,? extends U> fn) public CompletableFuture thenApplyAsync(Function<? super T,? extends U> fn) public CompletableFuture thenApplyAsync(Function<? super T,? extends U> fn, Executor executor)
public CompletableFuture
public CompletableFuture
<a name="XvKiP"></a>
# 5.两任务组合-都要完成
两个任务都完成,触发该任务<br />thenCombine:组合两个任务,获取使用两个任务结果,返回自己任务结果<br />thenAcceptBoth:组合两个任务,获取使用两个任务结果,没有返回值。<br />runAfterBoth:组合两个任务,不需要两个任务的返回值,没有返回结果。
```java
public CompletableFuture<Void> runAfterBoth(CompletionStage<?> other,
Runnable action)
public CompletableFuture<Void> runAfterBothAsync(CompletionStage<?> other,
Runnable action)
public CompletableFuture<Void> runAfterBothAsync(CompletionStage<?> other,
Runnable 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 <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 static void main(String[] args) throws ExecutionException, InterruptedException {
ExecutorService executorService = Executors.newFixedThreadPool(10);
CompletableFuture<Integer> task1 = CompletableFuture.supplyAsync(() -> {
System.out.println("任务1");
return 1;
}, executorService);
CompletableFuture<Integer> task2 = CompletableFuture.supplyAsync(() -> {
System.out.println("任务2");
return 2;
}, executorService);
CompletableFuture<Integer> task3 = task1.thenCombineAsync(task2, (t1, t2) -> {
System.out.println("任务3");
System.out.println("t1: "+ t1 + ",t2:"+t2);
return t1 + t2;
}, executorService);
System.out.println(task3.get());
}
任务1
任务2
任务3
t1: 1,t2:2
3
6.两任务组合-一个完成
与上面不同的是只要有一个完成了就可以继续执行新任务了,其他都一样。
- applyToEither:接收处理并返回
- acceptEither:只接收处理不返回
runAfterEither:不接收也不返回 ```java public CompletableFuture applyToEither(
CompletionStage<? extends T> other, Function<? super T, U> fn)
public CompletableFuture applyToEitherAsync(
CompletionStage<? extends T> other, Function<? super T, U> fn)
public CompletableFuture applyToEitherAsync(
CompletionStage<? extends T> other, Function<? super T, U> fn,
Executor executor)
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> runAfterEither(CompletionStage<?> other,
Runnable action)
public CompletableFuture<Void> runAfterEitherAsync(CompletionStage<?> other,
Runnable action)
public CompletableFuture<Void> runAfterEitherAsync(CompletionStage<?> other,
Runnable action,
Executor executor)
<a name="V8pAL"></a>
# 7.多任务组合
allOf:等待所有任务都完成,无返回值<br />anyOf:只要有一个任务完成,返回最先完成的值
```java
public static CompletableFuture<Void> allOf(CompletableFuture<?>... cfs)
public static CompletableFuture<Object> anyOf(CompletableFuture<?>... cfs)