下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026中国联通湖北分公司春季校园招聘140人易考易错模拟试题(共500题)试卷后附参考答案
- 2026中国移动福建公司春季校园招聘易考易错模拟试题(共500题)试卷后附参考答案
- 2026年土地登记代理人之土地登记相关法律知识考前冲刺训练试卷附答案详解【轻巧夺冠】
- 2026年期货从业资格模考模拟试题【历年真题】附答案详解
- 2026年林业有害生物防治员题库及完整答案详解【夺冠系列】
- 10.2 中华水塔-三江源地区课件(28张)人教版 地理八下
- 半导体厂务工程师笔试真题及答案
- 2026年顶碗少年测试题及答案
- 2026年公顷的认识测试题及答案
- 2026年公文综合写作测试题及答案
- 新型农业经营主体培育与创新课件
- 免疫调节剂(口腔科临床用药课件)
- 出国留学-话题education英语演讲PPT
- 【10套试卷】厦门市外国语学校小升初模拟考试数学试题含答案
- 最后一战-励志高考冲刺30天主题班会 高考倒计时主题班会课件
- 杭州师范大学堪培拉教育领导与管理硕士项目
- GB/T 28686-2012燃气轮机热力性能试验
- GB 29415-2013耐火电缆槽盒
- GA/T 1532-2018赤足足迹检验技术规范
- GA/T 1444-2017法庭科学笔迹检验样本提取规范
- 诺和龙诺和龙在糖尿病心脑血管方面的作用专家讲座
评论
0/150
提交评论