详解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


猜你喜欢
- Json格式是常见的读写形式。读写Json文件也是常用的操作。这次来实践一下Json文件的读写。首先在SD卡上的读写权限是一定要申请的。那么
- 在项目中使用Maven管理jar包依赖,往往会出现以下状况:1、国内访问maven默认远程中央镜像特别慢;2、使用阿里的镜像替代远程中央镜像
- 今天学习了Spinner组件的使用,非常好用的一款组件,相当于从下拉列表中选择项目,今天收获颇多,下面给大家演示一下Spinner的使用(分
- 单线程实现文件分割在老的FAT32文件系统中,最大的单个文件大小必须保存在4G内,对于经常看电影的我这个是不能允许的。不过现在Windows
- Java是一种面向对象的编程语言,由Sun Microsystems公司在1995年的时候正式发布。直到今天,Java都一直是最受欢迎的编程
- 泛型是Java中一个非常重要的内容,对于Java进阶学习是必须要掌握的知识点之所以说这个知识点重要,如果你有过阅读过一些开源框架的代码,那你
- 前言《摩尔庄园》前段时间上线, 持续超出市场预期,相信也有不错的收益。游戏好玩,所有玩家看到了前端,但是做一款游戏,离不开后台游戏服务器的支
- 本文实例讲述了C#清除WebBrowser中Cookie缓存的方法。分享给大家供大家参考,具体如下:最近用C#写一个程序,用一个窗体中的We
- 在用unity进行游戏开发时我们有时需要一些物体在场景切换时不需要被销毁这时我们可以用官方给的DontDestroyOnLoad()方法,这
- 一、申请你的AppIDhttp://open.weixin.qq.com/ 友情提示:推荐使用eclipse打包软件最后一步的M
- 懒加载 ,也称为嵌套查询 需要查询关联信息时,使用 Mybatis 懒加载特性可有效的减
- Spring容器可以自动装配相互协作bean之间的关系,这有助于减少对XML配置,而无需编写一个大的基于Spring应用程序的较多的<
- 利用Android的ApiDemos的Rotate3dAnimation实现了个图片3D旋转的动画,围绕Y轴进行旋转,还可以实现Z轴的缩放。
- 在做在线编程题目的时候,需要了解一下数据的输入格式。这样可以对数据处理有比较好的把握,不需要把太多的时间放在这个上面,注重主要的算法逻辑即可
- 使用示例:package cn.hackcoder.beautyreader.db;import android.content.Conte
- 一、堆排序1、什么是堆排序(1)堆排序:堆排序(Heapsort)是指利用堆这种数据结构所设计的一种排序算法。堆积是一个近似完全二叉树的结构
- 将10个整数按由小到大的顺序排列#include <iostream>using namespace std;int main(
- 这篇文章是android开发人员的必备知识,是我特别为大家整理和总结的,不求完美,但是有用。1.背景自适应且不失真问题的存在制作自适应背景图
- 本文实例讲述了c#分页读取GB文本文件的方法。分享给大家供大家参考。具体如下:一、应用场景:① .我在做BI开发测试的时候,有可能面对sou
- 本篇随笔主要介绍用Java实现简单的装饰器设计模式:先来看一下装饰器设计模式的类图:从图中可以看到,我们可以装饰Component接口的任何