1.创建异步对象

四个静态方法创建一个异步操作,runXXX都是没有返回结果的,supplyXXX都是有带返回结果的。
分别带有默认线程池或者自定义线程池。其中Supplier是一个函数式接口,代表是一个生成者的意思,传入0个参数,返回一个结果

  1. public static CompletableFuture<Void> runAsync(Runnable runnable){}
  2. public static CompletableFuture<Void> runAsync(Runnable runnable,
  3. Executor executor)
  4. public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier)
  5. public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier,
  6. Executor executor)

2.计算完成时回调方法

侧重对该任务结果的处理

  • whenComplete处理正常和异常(只能得到异常)的计算结果,exceptionallu处理异常情况并返回自定制
  • 带不带Async区别:带了就是新任务提交给线程池处理;不带就是这个线程继续处理
  • BiConsumer有两个参数:第一个是结果,第二个是异常
  • Function fn:方法:参数是异常,需要返回值。出现异常后默认返回什么。

    1. public CompletableFuture<T> whenComplete(BiConsumer<? super T,? super Throwable> action)
    2. public CompletableFuture<T> whenCompleteAsync(BiConsumer<? super T,? super Throwable> action)
    3. public CompletableFuture<T> whenCompleteAsync(BiConsumer<? super T,? super Throwable> action, Executor executor)
    4. public CompletableFuture<T> exceptionally(Function<Throwable,? extends T> fn)

    3.handle方法

    对结果处理的更全面
    BiFunction方法:两个参数:结果、异常。返回自定义逻辑值。

    1. public <U> CompletableFuture<U> handle(BiFunction<? super T,Throwable,? extends U> fn)
    2. public <U> CompletableFuture<U> handleAsync(BiFunction<? super T,Throwable,? extends U> fn)
    3. 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 thenAccept(Consumer<? super T> action) public CompletableFuture thenAcceptAsync(Consumer<? super T> action) public CompletableFuture thenAcceptAsync(Consumer<? super T> action, Executor executor)

public CompletableFuture thenRun(Runnable action) public CompletableFuture thenRunAsync(Runnable action) public CompletableFuture thenRunAsync(Runnable action, Executor executor)

  1. <a name="XvKiP"></a>
  2. # 5.两任务组合-都要完成
  3. 两个任务都完成,触发该任务<br />thenCombine:组合两个任务,获取使用两个任务结果,返回自己任务结果<br />thenAcceptBoth:组合两个任务,获取使用两个任务结果,没有返回值。<br />runAfterBoth:组合两个任务,不需要两个任务的返回值,没有返回结果。
  4. ```java
  5. public CompletableFuture<Void> runAfterBoth(CompletionStage<?> other,
  6. Runnable action)
  7. public CompletableFuture<Void> runAfterBothAsync(CompletionStage<?> other,
  8. Runnable action)
  9. public CompletableFuture<Void> runAfterBothAsync(CompletionStage<?> other,
  10. Runnable action,
  11. Executor executor)
  12. public <U> CompletableFuture<Void> thenAcceptBoth(
  13. CompletionStage<? extends U> other,
  14. BiConsumer<? super T, ? super U> action)
  15. public <U> CompletableFuture<Void> thenAcceptBothAsync(
  16. CompletionStage<? extends U> other,
  17. BiConsumer<? super T, ? super U> action)
  18. public <U> CompletableFuture<Void> thenAcceptBothAsync(
  19. CompletionStage<? extends U> other,
  20. BiConsumer<? super T, ? super U> action, Executor executor)
  21. public <U,V> CompletableFuture<V> thenCombine(
  22. CompletionStage<? extends U> other,
  23. BiFunction<? super T,? super U,? extends V> fn)
  24. public <U,V> CompletableFuture<V> thenCombineAsync(
  25. CompletionStage<? extends U> other,
  26. BiFunction<? super T,? super U,? extends V> fn)
  27. public <U,V> CompletableFuture<V> thenCombineAsync(
  28. CompletionStage<? extends U> other,
  29. BiFunction<? super T,? super U,? extends V> fn, Executor executor)
  1. public static void main(String[] args) throws ExecutionException, InterruptedException {
  2. ExecutorService executorService = Executors.newFixedThreadPool(10);
  3. CompletableFuture<Integer> task1 = CompletableFuture.supplyAsync(() -> {
  4. System.out.println("任务1");
  5. return 1;
  6. }, executorService);
  7. CompletableFuture<Integer> task2 = CompletableFuture.supplyAsync(() -> {
  8. System.out.println("任务2");
  9. return 2;
  10. }, executorService);
  11. CompletableFuture<Integer> task3 = task1.thenCombineAsync(task2, (t1, t2) -> {
  12. System.out.println("任务3");
  13. System.out.println("t1: "+ t1 + ",t2:"+t2);
  14. return t1 + t2;
  15. }, executorService);
  16. System.out.println(task3.get());
  17. }
  1. 任务1
  2. 任务2
  3. 任务3
  4. t1: 1,t2:2
  5. 3

6.两任务组合-一个完成

与上面不同的是只要有一个完成了就可以继续执行新任务了,其他都一样。

  • applyToEither:接收处理并返回
  • acceptEither:只接收处理不返回
  • runAfterEither:不接收也不返回 ```java public CompletableFuture applyToEither(

    1. CompletionStage<? extends T> other, Function<? super T, U> fn)

    public CompletableFuture applyToEitherAsync(

    1. CompletionStage<? extends T> other, Function<? super T, U> fn)

    public CompletableFuture applyToEitherAsync(

    1. CompletionStage<? extends T> other, Function<? super T, U> fn,
    2. Executor executor)
  1. public CompletableFuture<Void> acceptEither(
  2. CompletionStage<? extends T> other, Consumer<? super T> action)
  3. public CompletableFuture<Void> acceptEitherAsync(
  4. CompletionStage<? extends T> other, Consumer<? super T> action)
  5. public CompletableFuture<Void> acceptEitherAsync(
  6. CompletionStage<? extends T> other, Consumer<? super T> action,
  7. Executor executor)
  8. public CompletableFuture<Void> runAfterEither(CompletionStage<?> other,
  9. Runnable action)
  10. public CompletableFuture<Void> runAfterEitherAsync(CompletionStage<?> other,
  11. Runnable action)
  12. public CompletableFuture<Void> runAfterEitherAsync(CompletionStage<?> other,
  13. Runnable action,
  14. Executor executor)
  1. <a name="V8pAL"></a>
  2. # 7.多任务组合
  3. allOf:等待所有任务都完成,无返回值<br />anyOf:只要有一个任务完成,返回最先完成的值
  4. ```java
  5. public static CompletableFuture<Void> allOf(CompletableFuture<?>... cfs)
  6. public static CompletableFuture<Object> anyOf(CompletableFuture<?>... cfs)