下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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年小学班主任基本功竞赛试题及答案
- 2026年公务员行政职业能力测验试题及答案
- 2026上海交通大学医学院招聘91人备考题库带答案详解(培优a卷)
- 三级老年人能力评估师试题+答案
- 2026山东威海智慧谷实验幼儿园招聘1人备考题库含答案详解(黄金题型)
- 2026广东广州花都区新华五小附属文德小学临聘教师招聘1人备考题库及答案详解(名师系列)
- 2026山东青岛市人力资源集团有限公司招聘14人备考题库及答案详解(名校卷)
- 2026云南省老干部活动中心昆明银园会务中心招聘备考题库含答案详解ab卷
- 2026上半年安徽事业单位联考泗县招聘39人备考题库带答案详解(巩固)
- 安徽省阜阳市2026届高三上学期1月期末教学质量监测英语试卷(含答案无听力音频有听力原文)
- 2026年商洛市儿童福利院招聘备考题库(6人)附答案详解
- 脐静脉置管课件
- 左半结肠切除术后护理查房
- 工艺联锁-报警管理制度
- DB37∕T 3467-2018 美丽乡村标准化试点建设与验收指南
- 留置针压力性损伤预防
- 2025新沪教版英语(五四学制)七年级下单词默写表
- 高一英语新教材全四册单词表汉译英默写(2019新人教版)
- 2024年保险代理人分级(中级)考前通关必练题库(含答案)
- 用流程复制培训课件
评论
0/150
提交评论