網(wǎng)站首頁 編程語言 正文
1.直接異步執(zhí)行一個方法
CompletableFuture.runAsync(() -> {
// 業(yè)務代碼
});
2.多線程并行執(zhí)行
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(() -> {
// 業(yè)務代碼1
// 執(zhí)行邏輯處理, map1填充數(shù)據(jù)
});
CompletableFuture<Map<Integer, String>> completableFuture2 = completableFuture.supplyAsync(() -> {
// 業(yè)務代碼2
// 執(zhí)行邏輯處理, map2填充數(shù)據(jù)
});
CompletableFuture<Map<Integer, String>> completableFuture3 = completableFuture.supplyAsync(() -> {
// 業(yè)務代碼3
// 執(zhí)行邏輯處理, map3填充數(shù)據(jù)
});
// 此時還沒有執(zhí)行其他線程的方法, 還需要調(diào)用執(zhí)行的方法, 相當于多線程的start()方法
// 開啟多線程方法
// allOf表示要等到所有線程執(zhí)行結(jié)束, 主線程才可以繼續(xù)執(zhí)行, 相對還有anyOf, 表示里面的幾個線程只要任何一個線程有返回就直接繼續(xù)執(zhí)行
CompletableFuture.allOf(completableFuture1, completableFuture2, completableFuture3);
CompletableFuture的方法非常多, 還有其他更多的用途可以自行百度
原文鏈接:https://blog.csdn.net/qq_59409315/article/details/140293706
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2022-05-19 redis擊穿?雪崩?穿透超詳細解決方案梳理_Redis
- 2022-07-16 TritonObjDet新增要素的方法
- 2022-03-26 C語言宏定義#define的使用_C 語言
- 2023-01-14 GoLang逃逸分析講解_Golang
- 2022-07-12 git同步fork倉庫同步upstream倉庫
- 2022-08-21 Caffe圖像數(shù)據(jù)轉(zhuǎn)換成可運行l(wèi)eveldb?lmdb文件_其它綜合
- 2022-04-28 C#實現(xiàn)Windows服務測試與調(diào)試_C#教程
- 2022-12-24 Docker自定義網(wǎng)絡詳解_docker
- 欄目分類
-
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支