



下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Java实现主线程等待子线程-电脑资 料 2019-01-01本文介绍两种主线程等待子线程的实现方式,以5个子线程来说明:1、使用Thread的join ()方法,join ()方法会阻塞主线程继续向下 执行, O2、使用 java. util, concurrent 中的 CountDownLatch,是一个倒数计数 器。初始化时先设置一个倒数计数初始值,每调用一次countDown ()方法, 倒数值减一,他的await ()方法会阻塞当前进程,直到倒数至0.join方式代码如下:package com. test, thread;import java. util. ArrayList;
2、import java. util. List;public class MyThread extends Thread(public MyThread (String name)(this. setName (name);)©Overridepublic void run ()System. out. printin (this. getName ( ) + staring);System. out. printin (this. getName ( ) + end"); ) /* ©param args*/public static void main (St
3、ring1 args) (System, out. printin ("main thread starting-'");List list = new ArrayList (); for (int i = 1; i <= 5; i+) (MyThread my = new MyThread ("Thrad + i);my.start (); list, add (my); )try ( for (MyThread my : list) (my. join (); ) ) catch (InterruptedException e)e.printSt
4、ackTrace ();)System, out. printin ("main thread end);)运行结果如下:main thread starting*Thrad 2 staring*Thrad 2 endThrad 4 staring*Thrad 4 end*Thrad 1 staring*Thrad 1 end*Thrad 3 staring*Thrad 3 endThrad 5 staring*Thrad 5 end*main thread end*CountDownLatch方式代码如下:package com. test, thread;import java.
5、 util, concurrent. CountDownLatch;public class MyThread2 extends Threadprivate CountDownLatch count;public MyThread2 (CountDownLatch count, String name)(this, count = count;this. setName (name);)©Overridepublic void run ()(System. out. printin (this. getName ( ) + staring*);System. out. printin
6、 (this. getName ( ) + end");this, count. countDown ();)/* ©param args*/public static void main (String1 args)(System, out. printin ("main thread starting-.");CountDownLatch count = new CountDownLatch (5);for (int i = 1; i <= 5; i+)(MyThread2 my = new MyThread2 (count, “Thread + i);my.start ();try(count, await ();)catch (InterruptedException e)(e.printStackTrace ();)System, out. printin ("main thread end);)运行结果如下:main thread starting*Thread 2 staringThread 2 end*Thread 4 staringThread 4
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年中国3-甲基-2-甲醛噻吩数据监测报告
- 2025至2030年中国顶尖高性能光学平台市场分析及竞争策略研究报告
- 2025至2030年中国铝锌喷剂市场分析及竞争策略研究报告
- 2025至2030年中国选色纸转动轮市场分析及竞争策略研究报告
- 2025至2030年中国网络应用管理平台市场分析及竞争策略研究报告
- 2025至2030年中国碳化钛粉市场分析及竞争策略研究报告
- 2025至2030年中国电子专用模具市场分析及竞争策略研究报告
- 2025至2030年中国汽车锂基脂市场分析及竞争策略研究报告
- 2025至2030年中国散热贴片市场分析及竞争策略研究报告
- 2025至2030年中国弹簧圆规市场分析及竞争策略研究报告
- 站用电400V系统定期切换试验方案
- 初中数学北师大八年级下册(2023年修订) 因式分解岷阳王冬雪提公因式法教学设计
- 金属非金属矿山安全规程
- DB3311∕T 132-2020 住宅小区物业服务规范
- 员工三级安全教育培训记录
- C-TPAT反恐知识培训ppt课件
- 二代征信系统数据采集规范释义
- 河南华泰特种电缆项目可行性分析报告
- 公司员工合理化建议奖励办法
- 加工中心刀具库选择PLC控制系统设计
- 主域故障无法启动,额外域提升Active Directory
评论
0/150
提交评论