当前位置: 首页 > 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.hskmm.com/?act=detail&tid=38294

相关文章:

  • 城市基础设施安全运行监管平台
  • 第171-172天:代理通讯篇无外网或不可达SockS全协议规则配置C2正反向上线解决方案
  • SpringBoot整合缓存1-Ehcache
  • 【开题答辩全过程】以 M11289生鲜商城为例,具备答辩的问题和答案
  • ZR 2025 NOIP 二十连测 Day 7
  • CSP-S 37
  • Offsec Nibbles CTF 实战解析:PostgreSQL漏洞利用与权限提升
  • 如何在一台 Linux 机器上管理不同版本的 CMake
  • 90 天打造可持续交付:12 条 DevOps 实践要点与避坑
  • CSharp: word,excel,powerpoint convert to pdf,hrml etc using Aspose.Office
  • 2025年新疆喀纳斯旅游服务权威推荐榜单:新疆/阿勒泰/禾木深度游旅行社综合评测
  • 一天一款实用的AI工具,第9期,AI转黏土风格
  • 2025 10 24日报
  • 题解:P8930 「TERRA-OI R1」神,不惧死亡
  • 2025 OSCAR丨与创新者同频!Apache RocketMQ 邀您共赴开源之约
  • 生产环节最容易出问题的三个点,老板必须盯紧
  • 2025年PSA制氮设备厂家权威推荐榜单:电解水制氢设备/氦气纯化系统/氘气回收纯化源头厂家精选
  • 解决git clone只有master分支的问题
  • 一文读懂循环神经网络(RNN):原理、局限与LSTM解决方案 - 指南
  • 2025年搬家纸箱权威推荐榜单:物流包装/电商纸箱/平口纸箱源头厂家精选
  • 大数据案例 -2025/10/24
  • 2025年阳台壁挂太阳能厂家权威推荐榜单:分体式阳台太阳能/阳台壁挂太阳能热水器/分体式阳台太阳能源头厂家精选
  • 使用C# 控制ethercat从站设备
  • 从价值直觉到价值理性:AI元人文演进路径解读
  • 0269-GRPC-使用 prost 编码
  • 0268-GRPC-prost 生成文件到目录
  • 0271-GRPC-prost 带长度的编解码
  • 2025 年坡口机源头厂家最新推荐排行榜:欧盟 CE 认证企业领衔,含 15 年工业服务经验品牌,自走式/自动/板材/管道坡口机厂家推荐
  • 0270-GRPC-使用 prost 解码
  • 完整教程:Java开发者进阶之路