java中的CompletableFuture怎么用
发表于:2025-02-24 作者:千家信息网编辑
千家信息网最后更新 2025年02月24日,这篇"java中的CompletableFuture怎么用"文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面
千家信息网最后更新 2025年02月24日java中的CompletableFuture怎么用
这篇"java中的CompletableFuture怎么用"文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇"java中的CompletableFuture怎么用"文章吧。
说明
1、JDK 8中引入了 CompletableFuture 类,实现了Future和CompletionStage接口.
为异步编程提供了一些列方法,如supplyAsync、runAsync和thenApplyAsync等。
2、功能是可以让两个或者多个进行运算来产生结果。
实例
/** * @author mghio * @since 2021-08-01 */public class CompletableFutureDemo { public static CompletableFuturedoOneThing() { return CompletableFuture.supplyAsync(() -> { try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } return "doOneThing"; }); } public static CompletableFuture doOtherThing(String parameter) { return CompletableFuture.supplyAsync(() -> { try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } return parameter + " " + "doOtherThing"; }); } public static void main(String[] args) throws ExecutionException, InterruptedException { StopWatch stopWatch = new StopWatch("CompletableFutureDemo"); stopWatch.start(); // 异步执行版本 testCompletableFuture(); stopWatch.stop(); System.out.println(stopWatch); } private static void testCompletableFuture() throws InterruptedException, ExecutionException { // 先执行 doOneThing 任务,后执行 doOtherThing 任务 CompletableFuture resultFuture = doOneThing().thenCompose(CompletableFutureDemo::doOtherThing); // 获取任务结果 String doOneThingResult = resultFuture.get(); // 获取执行结果 System.out.println("DoOneThing and DoOtherThing execute finished. result = " + doOneThingResult); } }
以上就是关于"java中的CompletableFuture怎么用"这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注行业资讯频道。
内容
任务
结果
文章
知识
篇文章
两个
价值
功能
多个
大部分
实例
就是
接口
方法
更多
步骤
版本
知识点
行业
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
网络安全保障体系及措施
如何自学建立数据库
查询数据库部署环境
数据库投标方案
数据库min
数据库有服务端和客户端
东城区节能软件开发服务技术规范
网络安全网络图
日照市网络安全支撑单位
网络安全专业技术人员培训
网络安全法的形成背景
数据库设计命名规则
红米6pro服务器异常
e5v3戴尔服务器
微信平台软件开发哪个公司好
互联网运营科技开发哪个有前途
c 中如何定时保存数据库
软件开发考研好考吗
什么是hp服务器
进不了公司服务器
街道口软件开发公司
武汉网络安全监测
微邮付服务器
计算机服务器结构图
光电子网络技术
gp数据库修改表分布健
u82000版数据库
全国排课软件开发
昌吉市软件开发
网络技术书籍pdf