详解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
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Unity实现俄罗斯方块(二)
![](https://img.aspxhome.com/file/2023/0/69780_0s.jpg)
一文看懂RabbitMQ消息丢失如何防止
![](https://img.aspxhome.com/file/2023/9/64139_0s.jpg)
分享两种实现Winform程序的多语言支持的多种解决方案
Java超详细分析抽象类和接口的使用
![](https://img.aspxhome.com/file/2023/6/70366_0s.png)
Springboot集成graylog及配置过程解析
![](https://img.aspxhome.com/file/2023/7/64167_0s.png)
浅谈java 面对对象(抽象 继承 接口 多态)
flutter Bloc 实现原理示例解析
opencv 做人脸识别 opencv 人脸匹配分析
![](https://img.aspxhome.com/file/2023/8/101078_0s.jpg)
C#实现3步手动建DataGridView的方法
Java设计模式之java备忘录模式详解
![](https://img.aspxhome.com/file/2023/4/58434_0s.png)
详解Java异常处理中finally子句的运用
简单谈谈java自定义注解
Java经典面试题最全汇总208道(四)
![](https://img.aspxhome.com/file/2023/5/59065_0s.png)
MyBatis-Plus 集成动态多数据源的实现示例
Java中Lambda表达式和函数式接口的使用和特性
解决MyEclipse出现the user operation is waiting的问题
![](https://img.aspxhome.com/file/2023/0/64750_0s.gif)
Java并发编程预防死锁过程详解
详解Java中Period类的使用方法
解析在C#中接口和类的异同
![](https://img.aspxhome.com/file/2023/6/68096_0s.png)
Java实现简单登陆界面
![](https://img.aspxhome.com/file/2023/8/63398_0s.png)