打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
CompletableFuture的用法

CompletableFuture介绍

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

CompletableFuture底层实现

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

CompletableFuture API

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

创建CompletableFuture

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

CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello World");
CompletableFuture<String> future2 = CompletableFuture.completedFuture("Hello World");

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

CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
    // 做一些不需要返回值的操作
});

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

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

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

异步任务之间的关系

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

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

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

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 方法会在 future 和 future5 任务都完成后,将两个任务的结果作为参数传递给 thenCombine 中的函数,返回另一个 CompletableFuture 。
  • whenComplete 方法会在 future 任务完成后,执行 whenComplete 中的代码,无论成功或失败都会执行。

异常处理

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

  • exceptionally
  • handle

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

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

CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello World");

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

异步任务之间的关系

  • thenApply 示例
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello World");
CompletableFuture<String> future2 = future.thenApply(str -> str + " Java");
System.out.println(future2.get());

输出结果为 Hello World Java 。

  • thenAccept 示例
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello World");
CompletableFuture<Void> future2 = future.thenAccept(str -> System.out.println(str + " Java"));
future2.get();

输出结果为 Hello World Java 。

  • thenRun 示例
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello World");
CompletableFuture<Void> future2 = future.thenRun(() -> System.out.println("Hello Java"));
future2.get();

输出结果为 Hello Java 。

  • thenCompose 示例
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 示例
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 示例
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 示例
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 示例
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());

输出结果为 Error 或 Hello World 。

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Java异步的2种方式分析
异步编程利器:CompletableFuture详解
Java 8新特性之CompletableFuture(一) | Aoho''s Blog
【第81题】JAVA高级技术-多线程15(CompletableFuture实战)
java并发编程学习14
笑了,面试官问我知不知道异步编程的Future
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服