当前位置: 首页 > news >正文

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}

 

http://www.rkmt.cn/news/29624.html

相关文章:

  • 城市基础设施安全运行监管平台
  • ZR 2025 NOIP 二十连测 Day 7
  • CSP-S 37
  • CSharp: word,excel,powerpoint convert to pdf,hrml etc using Aspose.Office
  • Offsec Nibbles CTF 实战解析:PostgreSQL漏洞利用与权限提升
  • MySQLdump 常用参数说明 - 实践
  • 2025 10 24日报
  • 一天一款实用的AI工具,第9期,AI转黏土风格
  • 生产环节最容易出问题的三个点,老板必须盯紧
  • CS50ai: week2 Uncertainty我的笔记A版 - 实践
  • 2025年搬家纸箱权威推荐榜单:物流包装/电商纸箱/平口纸箱源头厂家精选
  • 【LTDC】在 RGBLCD 屏上实现任意位置画点和读点
  • 使用C# 控制ethercat从站设备
  • 2025 年坡口机源头厂家最新推荐排行榜:欧盟 CE 认证企业领衔,含 15 年工业服务经验品牌,自走式/自动/板材/管道坡口机厂家推荐
  • 实战练习:小软件页面间跳转传值 子页面数据渲染
  • 2025 年气凝胶生产厂家最新推荐排行榜:含气凝胶毡 / 粉 / 隔热板 / 保温罩 / 陶瓷板品牌,优质厂家推荐
  • 详细介绍:Uvicorn - Python ASGI Web 服务器
  • 2025年3d全息投影生产厂家权威推荐榜单:全息投影展厅/全息投影沙盘/全息投影源头厂家精选
  • JMeter Plugin Manager Linux 插件安装命令行
  • 整体理解pai0-具身智能-PyTorch einsum 完全教程-11 - jack
  • 2025年北京奢侈品品牌首饰回收公司权威推荐榜单:钻石回收/黄金回收/钻戒回收源头公司精选
  • 查询每门成绩都大于80分的同学学号
  • NVIDIA与Adobe漏洞深度解析
  • 2025 年退磁器生产厂家最新推荐榜:技术创新、行业适配与服务保障全景对比及权威测评结果强力退磁器/手提退磁器/小型退磁器公司推荐
  • 计算机组成原理:磁盘存储设备 - 实践
  • 题解:P8134 [ICPC 2020 WF] Opportunity Cost
  • 深入解析:数据结构 之 【图的遍历与最小生成树】(广度优先遍历算法、深度优先遍历算法、Kruskal算法、Prim算法实现)
  • qcefview库的使用
  • 2025年项目总延期?这30款项目进度管理软件让我提前交付率85%!
  • STM32软件I2C读写AT24C64 - 指南