CompletableFuture API 笔记

CompletableFuture API 笔记

1. 异步开启一个任务

  • supplyAsync
    • Supplier : T get()
    • 无入参,有返回值
  • runAsync
    • Runnable : void run()
    • 无返回值

2. 任务后置处理

  • thenApply
    • Function<? super T,? extends U> : R apply(T t)
    • 一个入参,有返回值
    • thenApplyAsync 异步执行后置任务
  • thenAccept
    • Consumer<? super T> : void accept(T t)
    • 一个入参,无返回值
    • thenAcceptAsync 异步执行后置任务
  • thenRun
    • Runnable : void run()
    • 无入参,无返回值
    • thenRunAsync 异步执行后置任务

3. 连接两个异步任务

  • thenCompose
    • Function<? super T, ? extends CompletionStage> : R apply(T t)
    • 一个入参(前一个任务的结果),有返回值(CompletionStage)

4. 合并两个异步任务

  • thenCombine
    • CompletionStage<? extends U>
    • BiFunction<? super T,? super U,? extends V> : R apply(T t, U u)
    • thenCombineAsync
  • thenAcceptBoth
    • CompletionStage<? extends U>
    • BiConsumer<? super T, ? super U>
    • thenAcceptBothAsync
  • runAfterBoth
    • CompletionStage<? extends U>
    • Runnable
    • runAfterBothAsync

5. 获取最先执行完成的任务

  • applyToEither
    • CompletionStage<? extends T>
    • Function<? super T, U> : R apply(T t)
    • applyToEitherAsync
  • acceptEither
    • CompletionStage<? extends T>
    • Consumer<? super T>
    • acceptEitherAsync
  • runAfterEither
    • CompletionStage<? extends T>
    • Runnable
    • runAfterEitherAsync

6. 异常捕获

  • exceptionally
    • Function<Throwable, ? extends T> : R apply(T t)
    • 一个入参 Throwable,有返回值
  • handle
    • BiFunction<? super T, Throwable, ? extends U> : R apply(T t, U u)
    • handleAsync
  • whenComplete
    • BiConsumer<? super T, ? super Throwable> : void accept(T t, U u)
    • whenCompleteAsync

7. join

  • join

简要案例

    CompletableFuture.runAsync(() -> {
        System.out.println("helo");
    });
    CompletableFuture.supplyAsync(() -> "hello");

    CompletableFuture.supplyAsync(() -> "hello").thenApplyAsync(t -> t + "w");
    CompletableFuture.supplyAsync(() -> "hello").thenAcceptAsync(t -> {
    });
    CompletableFuture.supplyAsync(() -> "hello").thenRunAsync(() -> {
    });

    CompletableFuture.supplyAsync(() -> "hello").thenCombine(CompletableFuture.completedFuture("world"), (x, y) -> x + y);
    CompletableFuture.supplyAsync(() -> "hello").thenAcceptBoth(CompletableFuture.supplyAsync(() -> "world"), (x, y) -> {
    });
    CompletableFuture.supplyAsync(() -> "hello").runAfterBoth(CompletableFuture.supplyAsync(() -> "world"), () -> {
    });

    CompletableFuture.supplyAsync(() -> "hello").applyToEitherAsync(CompletableFuture.completedFuture("world"), x -> x);
    CompletableFuture.supplyAsync(() -> "hello").acceptEitherAsync(CompletableFuture.supplyAsync(() -> "world"), x -> {
    });
    CompletableFuture.supplyAsync(() -> "hello").runAfterEitherAsync(CompletableFuture.supplyAsync(() -> "world"), () -> {
    });

    CompletableFuture.supplyAsync(() -> "hello").exceptionally(e -> null);
    CompletableFuture.supplyAsync(() -> "hello").handle((t, e) -> {
        if (e != null) {
            e.printStackTrace();
            return e.getMessage();
        } else {
            return t + "world";
        }
    });
    CompletableFuture.supplyAsync(() -> "hello").whenComplete((t, e) -> {
        if (e != null) {
            e.printStackTrace();
        } else {
            System.out.println(t);
        }

    });

github demo v2.1.0-CompletableFuture

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值