網站首頁 編程語言 正文
1.直接異步執行一個方法
CompletableFuture.runAsync(() -> {
// 業務代碼
});
2.多線程并行執行
AtomicReference<Map<Integer, String>> map1 = new AtomicReference<>(new HashMap<>());
AtomicReference<Map<Integer, String>> map2 = new AtomicReference<>(new HashMap<>());
AtomicReference<Map<Integer, String>> map3 = new AtomicReference<>(new HashMap<>());
CompletableFuture<Map<Integer, String>> completableFuture1 = completableFuture.supplyAsync(() -> {
// 業務代碼1
// 執行邏輯處理, map1填充數據
});
CompletableFuture<Map<Integer, String>> completableFuture2 = completableFuture.supplyAsync(() -> {
// 業務代碼2
// 執行邏輯處理, map2填充數據
});
CompletableFuture<Map<Integer, String>> completableFuture3 = completableFuture.supplyAsync(() -> {
// 業務代碼3
// 執行邏輯處理, map3填充數據
});
// 此時還沒有執行其他線程的方法, 還需要調用執行的方法, 相當于多線程的start()方法
// 開啟多線程方法
// allOf表示要等到所有線程執行結束, 主線程才可以繼續執行, 相對還有anyOf, 表示里面的幾個線程只要任何一個線程有返回就直接繼續執行
CompletableFuture.allOf(completableFuture1, completableFuture2, completableFuture3);
CompletableFuture的方法非常多, 還有其他更多的用途可以自行百度
原文鏈接:https://blog.csdn.net/qq_59409315/article/details/140293706
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2022-11-01 詳解批處理文件語法_DOS/BAT
- 2023-01-09 基于Go語言實現插入排序算法及優化_Golang
- 2022-07-06 C語言for循環嵌套for循環在實踐題目中應用詳解_C 語言
- 2022-10-06 C#中List<T>存放元素的工作機制_C#教程
- 2023-02-18 Flow轉LiveData數據丟失原理詳解_Android
- 2022-09-30 Python測試框架pytest核心庫pluggy詳解_python
- 2022-12-15 C++異步數據交換實現方法介紹_C 語言
- 2024-03-22 解決SpringBoot啟動報錯 :o.s.b.d.LoggingFailureAnalysisRe
- 欄目分類
-
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支