详解Java8中CompletableFuture类的使用

作者:程序员无名 时间:2022-06-28 17:39:43 

Java 8中引入了CompletableFuture类,它是一种方便的异步编程工具,可以处理各种异步操作,如网络请求、文件IO和数据库操作等。它是Java的Future接口的扩展,提供了一些有用的方法来创建、操作和组合异步操作。本文将详细介绍CompletableFuture的使用方式。

创建CompletableFuture

CompletableFuture提供了多种方法来创建CompletableFuture对象,如:

1.使用CompletableFuture.supplyAsync()方法创建异步执行的Supplier,Supplier中的代码会在异步线程中执行,代码执行完毕后,CompletableFuture将会得到执行结果。

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

2.使用CompletableFuture.runAsync()方法创建异步执行的Runnable,Runnable中的代码会在异步线程中执行,代码执行完毕后,CompletableFuture将会得到null作为执行结果。

CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
   //异步执行的代码
});

3.使用CompletableFuture.completedFuture()方法创建一个已经完成的CompletableFuture对象。

CompletableFuture<String> future = CompletableFuture.completedFuture("Hello");

4.使用CompletableFuture的构造方法创建CompletableFuture对象。

CompletableFuture<String> future = new CompletableFuture<>();

这种方式通常用于在执行某个操作之前创建一个CompletableFuture对象,并将其传递给其他方法,以便在异步操作完成后将结果传递回来。

处理CompletableFuture的结果

当异步操作完成时,可以通过CompletableFuture的get()方法获取执行结果。

CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello");
String result = future.get();
System.out.println(result); //输出"Hello"

但是,get()方法是一个阻塞的方法,它会一直等待异步操作完成,并返回结果或者抛出异常。如果你不想阻塞当前线程,你可以使用回调函数的方式来处理CompletableFuture的结果。

1.使用thenApply()方法处理CompletableFuture的结果。

CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello");
CompletableFuture<String> future2 = future.thenApply(result -> result + " World");
System.out.println(future2.get()); //输出"Hello World"

在这个例子中,我们使用thenApply()方法来处理CompletableFuture的结果。它接受一个Function函数,用于将CompletableFuture的结果转换为另一个值。

2.使用thenAccept()方法处理CompletableFuture的结果。

CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello");
future.thenAccept(result -> System.out.println(result + " World"));

在这个例子中,我们使用thenAccept()方法来处理CompletableFuture的结果。它接受一个Consumer函数,用于处理CompletableFuture的结果,但是不返回任何结果。

3.使用thenCompose()方法组合多个CompletableFuture。

CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> "Hello");
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> "World");
CompletableFuture<String> future3 = future1.thenCompose(result1 -> future2.thenApply(result2 -> result1 + " " + result2));
    try {
       System.out.println(future3.get());
   } catch (InterruptedException e) {
         throw new RuntimeException(e);
   } catch (ExecutionException e) {
       throw new RuntimeException(e);
   }

在这个例子中,我们使用thenCompose()方法来组合多个CompletableFuture对象。它接受一个Function函数,该函数将CompletableFuture的结果转换为另一个CompletableFuture对象。在这个例子中,我们先使用future1来创建一个新的CompletableFuture对象,然后将future2的结果作为参数传递给该对象的处理函数。

4.使用thenCombine()方法组合多个CompletableFuture。

CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> 10);
CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> 20);
CompletableFuture<Integer> future3 = future1.thenCombine(future2, (result1, result2) -> result1 + result2);
System.out.println(future3.get()); //输出30

在这个例子中,我们使用thenCombine()方法来组合多个CompletableFuture对象。它接受另一个CompletableFuture对象和一个BiFunction函数,该函数用于将两个CompletableFuture的结果合并为一个新的结果。

处理CompletableFuture的异常

当CompletableFuture执行过程中出现异常时,我们需要使用exceptionally()方法来处理异常。

CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
   throw new RuntimeException("异常信息");
});

future.exceptionally(ex -> {
   System.out.println(ex.getMessage()); //输出"异常信息"
   return 0;
});

在这个例子中,我们使用exceptionally()方法来处理CompletableFuture的异常。它接受一个Function函数,用于处理异常并返回一个默认值。

等待多个CompletableFuture执行完毕

有时我们需要等待多个CompletableFuture对象执行完毕后再继续执行下一步操作。我们可以使用CompletableFuture的allOf()方法或anyOf()方法来等待多个CompletableFuture对象执行完毕。

CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> "Hello");
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> "World");
CompletableFuture<Void> allFuture = CompletableFuture.allOf(future1, future2);
allFuture.get();

CompletableFuture<Object> anyFuture = CompletableFuture.anyOf(future1, future2);
System.out.println(anyFuture.get()); //输出"Hello"或"World"

在这个例子中,我们使用allOf()方法来等待所有的CompletableFuture对象执行完毕,并使用anyOf()方法来等待任何一个CompletableFuture对象执行完毕。

来源:https://juejin.cn/post/7221151497974972473

标签:Java8,CompletableFuture
0
投稿

猜你喜欢

  • Unity实现俄罗斯方块(二)

    2022-01-15 06:57:56
  • 一文看懂RabbitMQ消息丢失如何防止

    2022-03-03 20:36:24
  • 分享两种实现Winform程序的多语言支持的多种解决方案

    2023-10-17 21:24:17
  • Java超详细分析抽象类和接口的使用

    2021-06-09 12:19:05
  • Springboot集成graylog及配置过程解析

    2023-06-18 17:15:02
  • 浅谈java 面对对象(抽象 继承 接口 多态)

    2022-04-01 07:23:50
  • flutter Bloc 实现原理示例解析

    2023-07-18 08:00:39
  • opencv 做人脸识别 opencv 人脸匹配分析

    2023-07-09 06:34:44
  • C#实现3步手动建DataGridView的方法

    2021-10-13 22:35:56
  • Java设计模式之java备忘录模式详解

    2023-08-22 19:31:07
  • 详解Java异常处理中finally子句的运用

    2023-11-29 10:10:30
  • 简单谈谈java自定义注解

    2021-07-21 18:40:53
  • Java经典面试题最全汇总208道(四)

    2023-11-08 23:59:26
  • MyBatis-Plus 集成动态多数据源的实现示例

    2023-06-26 17:56:35
  • Java中Lambda表达式和函数式接口的使用和特性

    2023-06-20 20:05:42
  • 解决MyEclipse出现the user operation is waiting的问题

    2022-05-02 21:44:07
  • Java并发编程预防死锁过程详解

    2023-11-09 15:33:58
  • 详解Java中Period类的使用方法

    2023-11-28 21:04:44
  • 解析在C#中接口和类的异同

    2022-04-10 13:02:07
  • Java实现简单登陆界面

    2022-11-25 05:47:47
  • asp之家 软件编程 m.aspxhome.com