前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >如何使用CompletableFuture

如何使用CompletableFuture

作者头像
贪挽懒月
发布2023-10-16 15:33:08
1670
发布2023-10-16 15:33:08
举报
文章被收录于专栏:JavaEEJavaEE

CompletableFuture介绍

CompletableFuture 是Java 8中的一个很重要的类,它是一个实现了 Future 接口的异步处理类,可以帮助我们更加方便地执行异步任务和处理异步任务的结果。 CompletableFuture 的底层实现是基于 Fork/Join 框架和 ConcurrentLinkedQueue ,在Java 8中, CompletableFuture 是在 java.util.concurrent 包下。

CompletableFuture底层实现

CompletableFuture 底层依赖了 Fork/Join 框架和 ConcurrentLinkedQueueFork/Join 框架是Java 7加入的一个框架,它的基本思想是将一个大任务拆成多个小任务并行执行,最后将结果合并起来。而 ConcurrentLinkedQueue 是Java 5中添加的一个线程安全的队列,它支持高并发下的并发读写。 CompletableFuture 底层的异步处理也是基于这两个框架的。异步任务的执行,是将任务拆分成多个小任务,然后并行执行,最后合并结果。同时,异步任务的结果可以是不同类型的对象,如 voidvalueexception 等。

CompletableFuture API

CompletableFuture 类中提供了很多API,常见的有以下几个:

创建CompletableFuture

我们可以通过以下两种方式创建CompletableFuture

代码语言:javascript
复制
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello World");
CompletableFuture<String> future2 = CompletableFuture.completedFuture("Hello World");

如果需要无返回值的CompletableFuture,可以通过以下方式:

代码语言:javascript
复制
CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
    // 做一些不需要返回值的操作
});

在这个示例中,我们创建了一个 CompletableFuture 对象,它使用 runAsync() 方法执行了一个不需要返回值的操作。注意,这个操作是在新的线程中执行的,因此不会阻塞主线程。

可以通过调用 future.join() 来等待这个异步任务的完成。如果不需要等待任务的完成,可以直接返回 future 对象。

通过 supplyAsync 方式创建的CompletableFuture,会执行一个异步任务,并返回结果。而 completedFuture 方式创建的CompletableFuture,则直接返回一个已经完成的CompletableFuture对象。

异步任务之间的关系

CompletableFuture 中有很多方法可以用来描述异步任务之间的关系,如:

  • thenApply
  • thenAccept
  • thenRun
  • thenCompose
  • thenCombine
  • whenComplete

这些方法的使用方式相似,都是通过链式调用的方式,如下示例:

代码语言:javascript
复制
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello World");
CompletableFuture<String> future2 = future.thenApply(str -> str + "Java");
CompletableFuture<Void> future3 = future.thenAccept(str -> System.out.println(str));
CompletableFuture<Void> future4 = future.thenRun(() -> System.out.println("Hello Java"));
CompletableFuture<String> future5 = future.thenCompose(str -> CompletableFuture.completedFuture(str + "Java"));
CompletableFuture<String> future6 = future.thenCombine(future5, (str1, str2) -> str1 + str2);
CompletableFuture<Void> future7 = future.whenComplete((str, ex) -> {
    if (ex != null) {
        System.out.println("Error: " + ex.getMessage());
    } else {
        System.out.println(str);
    }
});
  • thenApply 方法会在 future 任务完成后,将其结果作为参数传递给 thenApply 中的函数,返回另一个 CompletableFuture
  • thenAccept 方法会在 future 任务完成后,将其结果作为参数传递给 thenAccept 中的函数,不返回任何结果。
  • thenRun 方法会在 future 任务完成后,执行 thenRun 中的代码,不接受任何参数,也不返回任何结果。
  • thenCompose 方法会在 future 任务完成后,将其结果作为参数传递给 thenCompose 中的 CompletableFuture ,返回另一个 CompletableFuture
  • thenCombine 方法会在 futurefuture5 任务都完成后,将两个任务的结果作为参数传递给 thenCombine 中的函数,返回另一个 CompletableFuture
  • whenComplete 方法会在 future 任务完成后,执行 whenComplete 中的代码,无论成功或失败都会执行。

异常处理

CompletableFuture 中有很多方法可以用来处理异步任务中出现的异常,如:

  • exceptionally
  • handle

这些方法的使用方式相似,都是通过链式调用的方式,如下示例:

代码语言:javascript
复制
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
    if (System.currentTimeMillis() % 2 == 0) {
        throw new RuntimeException();
    }
    return "Hello World";
});
 CompletableFuture<String> future2 = future.exceptionally(ex -> "Error")
                                           .thenApply(str -> str + " Java");
 CompletableFuture<String> future3 = future.handle((str, ex) -> {
    if (ex != null) {
        System.out.println("Error: " + ex.getMessage());
        return "Error";
    } else {
        return str;
    }
});
  • exceptionally 方法会在 future 任务出现异常的时候执行,并返回一个带有默认值的 CompletableFuture
  • handle 方法会在 future 任务完成后,无论成功或失败都会执行,并可以处理异常信息。

CompletableFuture用法示例

创建CompletableFuture

代码语言:javascript
复制
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello World");

通过 supplyAsync 创建一个异步执行任务,返回值为 Hello World

异步任务之间的关系

  • thenApply 示例
代码语言:javascript
复制
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello World");
CompletableFuture<String> future2 = future.thenApply(str -> str + " Java");
System.out.println(future2.get());

输出结果为 Hello World Java

  • thenAccept 示例
代码语言:javascript
复制
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello World");
CompletableFuture<Void> future2 = future.thenAccept(str -> System.out.println(str + " Java"));
future2.get();

输出结果为 Hello World Java

  • thenRun 示例
代码语言:javascript
复制
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello World");
CompletableFuture<Void> future2 = future.thenRun(() -> System.out.println("Hello Java"));
future2.get();

输出结果为 Hello Java

  • thenCompose 示例
代码语言:javascript
复制
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello World");
CompletableFuture<String> future2 = future.thenCompose(str -> CompletableFuture.completedFuture(str + " Java"));
System.out.println(future2.get());

输出结果为 Hello World Java

  • thenCombine 示例
代码语言:javascript
复制
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello");
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> " World");
CompletableFuture<String> future3 = future.thenCombine(future2, (str1, str2) -> str1 + str2);
System.out.println(future3.get());

输出结果为 Hello World

  • whenComplete 示例
代码语言:javascript
复制
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello World");
CompletableFuture<Void> future2 = future.whenComplete((str, ex) -> {
    if (ex != null) {
        System.out.println("Error: " + ex.getMessage());
    } else {
        System.out.println(str);
    }
});
future2.get();

输出结果为 Hello World

异常处理示例

  • exceptionally 示例
代码语言:javascript
复制
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
    if (System.currentTimeMillis() % 2 == 0) {
        throw new RuntimeException();
    }
    return "Hello World";
});
 CompletableFuture<String> future2 = future.exceptionally(ex -> "Error").thenApply(str -> str + " Java");
System.out.println(future2.get());

输出结果为 Error Java

  • handle 示例
代码语言:javascript
复制
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
    if (System.currentTimeMillis() % 2 == 0) {
        throw new RuntimeException();
    }
    return "Hello World";
});
 CompletableFuture<String> future2 = future.handle((str, ex) -> {
    if (ex != null) {
        System.out.println("Error: " + ex.getMessage());
        return "Error";
    } else {
        return str;
    }
});
System.out.println(future2.get());

输出结果为 ErrorHello World

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2023-08-02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • CompletableFuture介绍
  • CompletableFuture底层实现
  • CompletableFuture API
    • 创建CompletableFuture
      • 异步任务之间的关系
        • 异常处理
        • CompletableFuture用法示例
          • 创建CompletableFuture
            • 异步任务之间的关系
              • 异常处理示例
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档