尧图网站建设 尧图网络
  • 首页
  • 关于我们
  • 服务项目
  • 案例展示
  • 建站流程
  • 资讯中心
  • 联系我们
首页/资讯中心/详情

CompletableFuture串联多个异步任务实践

CompletableFuture串联多个异步任务实践
📅 发布时间:2026/6/19 14:29:35

CompletableFuture串联多个异步任务实践

  java 多线程中对于一个任务A完成了,任务B才开始;任务B完成了,任务C才开始;’任务C完成,任务D才开始;每个任务都是一个异步任务列。
废话少说,直接看代码,调试理解
    static ThreadPoolExecutor executor = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors() * 2,(int)(Runtime.getRuntime().availableProcessors() /0.1), 120,TimeUnit.SECONDS, new ArrayBlockingQueue<>(2000));public static void main(String[] args) {List<Integer> numberList = IntStream.rangeClosed(1, 10).boxed() // 将int转换为Integer
                .toList();CompletableFuture<Void> voidCompletableFuture = taskA(numberList).thenRun(() -> taskB(numberList).join()) // 此处和下面两行可以去掉join()测试观察异同.thenRun(() -> taskC(numberList).join()).thenRun(() -> taskD(numberList).join()).thenRunAsync(() -> {executor.shutdown();});// voidCompletableFuture.join();  
    }private static CompletableFuture<Void> taskD(List<Integer> numberList) {System.out.println();List<CompletableFuture<Void>> futureList = numberList.stream().map(x -> CompletableFuture.runAsync(() -> {try {Thread.sleep(2000);System.out.print("taskD--" + x);} catch (InterruptedException e) {throw new RuntimeException(e);}}, executor)).toList();return CompletableFuture.allOf(futureList.toArray(new CompletableFuture[0]));}private static CompletableFuture<Void> taskC(List<Integer> numberList) {System.out.println();List<CompletableFuture<Void>> futureList = numberList.stream().map(x -> CompletableFuture.runAsync(() -> {try {Thread.sleep(2000);System.out.print("taskC--" + x);} catch (InterruptedException e) {throw new RuntimeException(e);}}, executor)).toList();return CompletableFuture.allOf(futureList.toArray(new CompletableFuture[0]));}private static CompletableFuture<Void> taskB(List<Integer> numberList) {System.out.println();List<CompletableFuture<Void>> futureList = numberList.stream().map(x -> CompletableFuture.runAsync(() -> {try {Thread.sleep(2000);System.out.print("taskB--" + x);} catch (InterruptedException e) {throw new RuntimeException(e);}}, executor)).toList();return CompletableFuture.allOf(futureList.toArray(new CompletableFuture[0]));}private static CompletableFuture<Void> taskA(List<Integer> numberList) {System.out.println();List<CompletableFuture<Void>> futureList = numberList.stream().map(x -> CompletableFuture.runAsync(() -> {try {Thread.sleep(2000);System.out.print("taskA--" + x);} catch (InterruptedException e) {throw new RuntimeException(e);}}, executor)).toList();return CompletableFuture.allOf(futureList.toArray(new CompletableFuture[0]));}

执行结果

{988EF5F1-F1EE-42D4-B56F-53F21CFF01F8}

 

相关新闻

  • 城市基础设施安全运行监管平台
  • ZR 2025 NOIP 二十连测 Day 7
  • CSP-S 37

最新新闻

  • 嵌入式GUI开发实战:emWin进度条、二维码与单选按钮控件详解
  • 工具失败时怎么办:重试、回滚、人工确认和风险提示
  • 从麦克斯韦方程到仿真工具:FDFD光子仿真工具箱构建指南
  • 在M系列Mac上运行Windows程序的5个简单步骤:Whisky完全指南
  • 终极指南:用RyzenAdj解锁你的AMD笔记本隐藏性能
  • 2026年宁波AI搜索优化公司全面权威横向评测与选型决策指南 - 品牌报告

日新闻

  • 信任的进化:技术实现详解——如何用JavaScript构建博弈论模拟器
  • Terrakube自定义工作流:如何集成OPA、Infracost等工具扩展IaC能力
  • grunt-concurrent快速入门:5分钟学会并行运行Grunt任务

周新闻

  • 3步解锁iOS设备:applera1n激活锁绕过完全指南
  • 39 2026 人工智能证书终极盘点,普通人选 AI 证书可以从这些方向入手
  • Redis 暴露公网有多危险?从端口检查到补救步骤

月新闻

  • 【总结】入门篇:50句话让你记住架构核心概念
  • WeChatMsg技术方案解析:实现Mac微信数据自主管理的完整解决方案
  • WeChatMsg:革新性微信数据备份方案,打造你的专属数字记忆库

关于尧图

  • 公司简介
  • 团队介绍
  • 企业文化
  • 荣誉资质

服务项目

  • 定制开发
  • 电商建站
  • UI 设计
  • 运维服务

快速链接

  • 案例展示
  • 建站流程
  • 常见问题
  • 资讯中心

联系方式

  • 📍北京市朝阳区互联网产业园 A 座 10 层
  • 📞400-888-8888
  • ✉️contact@rkmt.cn
  • 🕐周一至周日 9:00-21:00

© 2024 北京尧图网络科技有限公司 版权所有 | 京 ICP 备 XXXXXXXX 号