【Java并发】10个线程处理完任务后,再合并处理,CountDownLatch的用法

news/2024/11/28 23:33:34/

代码

线程一起执行:

import java.util.*;
import java.util.concurrent.*;public class Main {public static List<String> getExecutorService() throws InterruptedException{System.out.println("开始执行多线程...");long startTime = System.currentTimeMillis();List<String> list = new ArrayList<>(); //存放返回结果CountDownLatch countDownLatch = new CountDownLatch(10);ExecutorService executorService = new ThreadPoolExecutor(10, 10, 0L,TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());for (int i = 0; i < 10; i++) {System.out.println("线程准备中...");Runnable runnable = new Runnable(){@Overridepublic void run() {try {Thread.sleep(3000);list.add(UUID.randomUUID().toString());System.out.println("当前线程name : " + Thread.currentThread().getName());} catch (InterruptedException e) {e.printStackTrace();} finally {countDownLatch.countDown();}}};executorService.execute(runnable);}System.out.println("countDownLatch.getCount(): " + countDownLatch.getCount());countDownLatch.await();  // 主线程阻塞,等计数器==0,唤醒主线程往下执行。System.out.println("countDownLatch.getCount(): " + countDownLatch.getCount());System.out.println("submit总共cost 时间:" + (System.currentTimeMillis() - startTime)/1000 + "秒");executorService.shutdown();  // 等内部线程执行完,停止线程return list;}public static void main(String[] args) {List<String> res = new ArrayList<>();try {res = getExecutorService();} catch (InterruptedException e) {e.printStackTrace();}System.out.println(res);}
}

一个一个线程执行

import java.util.*;
import java.util.concurrent.*;public class Main {public static List<String> getExecutorService() throws InterruptedException{System.out.println("开始执行多线程...");long startTime = System.currentTimeMillis();List<String> list = new ArrayList<>();  // 存放返回结果CountDownLatch countDownLatch = new CountDownLatch(10);ExecutorService executorService = new ThreadPoolExecutor(10, 10, 0L,TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());for (int i = 0; i < 10; i++) {System.out.println("线程准备中...");Runnable runnable = new Runnable(){@Overridepublic void run() {list.add(UUID.randomUUID().toString());System.out.println("当前线程name : " + Thread.currentThread().getName());countDownLatch.countDown();}};executorService.execute(runnable);try {Thread.sleep(1500);} catch (InterruptedException e) {e.printStackTrace();}}System.out.println("countDownLatch.getCount(): " + countDownLatch.getCount());countDownLatch.await();  // 主线程阻塞,等计数器==0,唤醒主线程往下执行。System.out.println("countDownLatch.getCount(): " + countDownLatch.getCount());System.out.println("submit总共cost 时间:" + (System.currentTimeMillis()-startTime)/1000 + "秒");executorService.shutdown();  // 等内部线程执行完,停止线程return list;}public static void main(String[] args) {List<String> res = new ArrayList<>();try {res = getExecutorService();} catch (InterruptedException e) {e.printStackTrace();}System.out.println(res);}
}

结果

线程一起执行:

Connected to the target VM, address: '127.0.0.1:65178', transport: 'socket'
开始执行多线程...
线程准备中...
线程准备中...
线程准备中...
线程准备中...
线程准备中...
线程准备中...
线程准备中...
线程准备中...
线程准备中...
线程准备中...
countDownLatch.getCount(): 10
当前线程name : pool-1-thread-6
当前线程name : pool-1-thread-10
当前线程name : pool-1-thread-3
当前线程name : pool-1-thread-9
当前线程name : pool-1-thread-8
当前线程name : pool-1-thread-5
当前线程name : pool-1-thread-2
当前线程name : pool-1-thread-4
当前线程name : pool-1-thread-1
当前线程name : pool-1-thread-7
countDownLatch.getCount(): 0
submit总共cost 时间:3秒
[9f8d1b1d-21a3-4ba3-8a2c-9701126e900f, d37227bd-bf9b-4c18-a7ab-e8f2a621f8c6, 71c8b258-b973-46e5-b4dc-5aba9c3f4fb5, ad1d8d4a-bdb8-4a14-8d36-3b4c62a06f2f, 50be045c-0cec-4d52-bb23-1476d7e40bf9, e87f4359-4382-4b2f-b520-ae598a0ce53e, 7e854a60-2566-4797-a2f8-1217b0c19214, efb2ab95-d632-4dd8-bade-57635c65e3ea, a1a88c3a-7563-4110-bc97-9b7eb257994b, 27d7eb60-c83b-4a21-8f46-1b5170aa49c3]

线程一个一个执行:

开始执行多线程...
线程准备中...
当前线程name : pool-1-thread-1
线程准备中...
当前线程name : pool-1-thread-2
线程准备中...
当前线程name : pool-1-thread-3
线程准备中...
当前线程name : pool-1-thread-4
线程准备中...
当前线程name : pool-1-thread-5
线程准备中...
当前线程name : pool-1-thread-6
线程准备中...
当前线程name : pool-1-thread-7
线程准备中...
当前线程name : pool-1-thread-8
线程准备中...
当前线程name : pool-1-thread-9
线程准备中...
当前线程name : pool-1-thread-10
countDownLatch.getCount(): 0
countDownLatch.getCount(): 0
submit总共cost 时间:15秒
[2176b202-744d-4e8b-9ce7-dab3e02738ef, 40f1cd8b-22c8-4260-8d71-6e1c9c9f15db, 5dce872b-4aef-4ef8-8558-b5d5935e95b3, 2fd450ac-8db6-4f6b-8caf-62c6fa667253, 0846b4d2-5a42-4cbc-bdde-32ad5cbc03ec, 2451fade-1af0-4c0e-adf2-84469f599d64, 5285a9a3-3075-4f8a-99a5-5adee23662be, 4f867908-6e1b-4840-a050-945c59481f1c, 70ecacd3-fc5b-4896-8b5d-cb2ec7e434d8, d5b2a439-0a86-4a5f-94f5-07bf53b674f9]

http://www.ppmy.cn/news/125955.html

相关文章

中兴ZTE_B860AV2.1-A、2.1-M、2.1-B_1g-2g_通刷语音固件包

中兴ZTE_B860AV2.1-A、2.1-M、2.1-B_1g-2g_通刷语音固件超级桌面 固件特点&#xff1a; 1、修改dns&#xff0c;三网通用&#xff1b; 2、开放原厂固件屏蔽的市场安装和u盘安装apk&#xff1b; 3、无开机广告&#xff0c;无系统更新&#xff0c;不在被强制升级&#xff1b;…

微星 B660M 迫击炮 评测

微星 B660M 迫击炮主板搭载了 1 个 PCIe 4.0 *16 显卡插槽和一个 PCIe 3.0 *16 显卡插槽&#xff0c;支持 DDR5-6200 超频内存&#xff0c;配备两个 PCIe 4.0 的 M.2 插槽。微星 B660M 迫击炮更多使用感受和评价&#xff1a;http://www.adiannao.cn/du 后置 IO 面板方面&…

浏览器用xpath获取一直为空

这两天闲的无聊&#xff0c;随便爬了点小说和趣图&#xff0c;因为好久没使用xpath了&#xff0c;所以遇到了点问题&#xff0c;就是xpath值一直为空&#xff0c;举个例子: 爬取小说网站。 使用xpath工具查询小说网站导航栏的xpath路径是这样子的: 值是酱紫的: 用python测试下…

解决Idea Translation插件翻译失败 更新TKK失败 time out 问题

idea 版本&#xff1a;2019.3.3 Translation版本&#xff1a;v2.9.3-193 错误截图&#xff1a; 错误日志&#xff1a; >Plugin v2.9.3-193 >IntelliJ IDEA 2019.3.3 >Build #IU-193.6494.35, built on February 11, 2020 >Runtime version: 11.0.510-b5…

联想微型计算机怎么开盖,联想b520一体机拆机图解

联想b520一体机拆机图解 一体机又称一体台式机&#xff0c;这一概念最先由联想集团提出&#xff0c;是指将传统分体台式机的主机集成到显示器中&#xff0c;从而形成一体台式机。联想是中国第一个拥有百万一体台式机用户的品牌厂商&#xff0c;并以其领先分体台式机的主机集成到…

【无标题】三星Xpress M2020打印机刷免芯片

三星Xpress M2020打印机刷免芯片 1.拆下打印机主板 2.按照图片改电阻或拆下电阻 拆掉此电阻 3.将主板安装回打印机&#xff08;发现打印机&#xff0c;只亮一个绿灯 不预热&#xff09; 4. 先把电源线拔掉 USB线要接上&#xff0c;按住面板上两个键不放&#xff0c;接上电源线…

(2022版)一套教程搞定k8s安装到实战 | Docker基本命令

视频来源&#xff1a;B站《&#xff08;2022版&#xff09;最新、最全、最详细的Kubernetes&#xff08;K8s&#xff09;教程&#xff0c;从K8s安装到实战一套搞定》 一边学习一边整理老师的课程内容及试验笔记&#xff0c;并与大家分享&#xff0c;侵权即删&#xff0c;谢谢支…

11月25日科技资讯|网易回应裁撤生病员工:确实存在简单粗暴不近人情行为

「极客头条」—— 技术人员的新闻圈&#xff01; CSDN 的读者朋友们早上好哇&#xff0c;「极客头条」来啦&#xff0c;快来看今天都有哪些值得我们技术人关注的重要新闻吧。扫描上方二维码进入 CSDN App 可以收听御姐萌妹 Style 的人工版音频哟。 一分钟速览新闻点&#xff…