使用join和CountDownLatch来等待线程结束
1.join方法的实现
join只能在start()之后调用,
join 某个线程A,会使当前线程B进入等待,直到线程A结束生命周期(isAlive()==false) ,或者达到给定的时间.
在此期间内当前线程B处理Waiting(调用 wait()方法),而不是线程A.
join方法本身只检测线程A的状态,而不影响线程A的执行.
调用线程会等待子线程结束后再运行
public class ThreadJoinTest {
public static void main(String[] args) {
int count = 100;
Thread[] threads = new Thread[count];
IntStream.range(0, count)
.forEach(i -> {
threads[i] = new Thread("thread" + i) {
@Override
public void run() {
ThreadJoinTest.sleep(1);
System.out.println(Thread.currentThread().getName() + ":" + i);
}
};
threads[i].start();
});
//join只能在start()之后调用
//调用线程会等待子线程结束后再运行
Arrays.stream(threads).forEach(ThreadJoinTest::join);
System.out.println("over");
}
private static void sleep(int seconds) {
try {
TimeUnit.SECONDS.sleep(seconds);
} catch (InterruptedException ex) {
}
}
private static void join(Thread thread) {
try {
thread.join();
} catch (InterruptedException ex) {
}
}
测试结果如下
thread2:2
thread10:10
thread6:6
thread14:14
thread18:18
thread34:34
thread22:22
thread26:26
thread30:30
thread38:38
thread42:42
thread46:46
thread50:50
thread54:54
thread62:62
thread66:66
thread58:58
thread70:70
thread74:74
thread78:78
thread82:82
thread86:86
thread90:90
thread94:94
thread98:98
thread5:5
thread1:1
thread9:9
thread13:13
thread17:17
thread21:21
thread25:25
thread29:29
thread33:33
thread37:37
thread41:41
thread45:45
thread49:49
thread61:61
thread57:57
thread53:53
thread77:77
thread73:73
thread69:69
thread65:65
thread93:93
thread89:89
thread85:85
thread81:81
thread97:97
thread0:0
thread12:12
thread4:4
thread8:8
thread16:16
thread20:20
thread24:24
thread28:28
thread32:32
thread36:36
thread40:40
thread44:44
thread60:60
thread56:56
thread52:52
thread48:48
thread76:76
thread72:72
thread68:68
thread64:64
thread92:92
thread88:88
thread84:84
thread80:80
thread96:96
thread3:3
thread7:7
thread11:11
thread99:99
thread95:95
thread91:91
thread87:87
thread83:83
thread79:79
thread75:75
thread67:67
thread63:63
thread71:71
thread59:59
thread51:51
thread55:55
thread23:23
thread27:27
thread31:31
thread35:35
thread39:39
thread43:43
thread47:47
thread15:15
thread19:19
over
2. java.util.concurrent.CountDownLatch
public class ThreadJoinTest {
public static void main(String[] args) {
int count = 100;
Thread[] threads = new Thread[count];
java.util.concurrent.CountDownLatch countDownLatch = new java.util.concurrent.CountDownLatch(count);
IntStream.range(0, count)
.forEach(i -> {
threads[i] = new Thread("thread" + i) {
@Override
public void run() {
ThreadJoinTest.sleep(1);
System.out.println(Thread.currentThread().getName() + ":" + i);
//只能在run方法的最后一句,或者在 try..finally的 finally里
countDownLatch.countDown();
}
};
threads[i].start();
});
try {
countDownLatch.await();
} catch (InterruptedException ex) {
}
System.out.println("over");
}
使用join和CountDownLatch来等待线程结束的更多相关文章
- Qt线程QThread简析(8个线程等级,在UI线程里可调用thread->wait()等待线程结束,exit()可直接退出线程,setStackSize设置线程堆栈,首次见到Qt::HANDLE,QThreadData和QThreadPrivate)
QThread实例代表一个线程,我们可以重新实现QThread::run(),要新建一个线程,我们应该先继承QThread并重新实现run()函数. 需要注意的是: 1.必须在创建QThread对象之 ...
- 线程创建,属性设置与获得,等待线程结束,线程中fork,以及执行exec()
这篇博客的形式我想以分析代码不同情况为主: 点击(此处)折叠或打开 #include<stdio.h> #include<pthread.h> #include<time ...
- Windows10 VS2017 C++多线程传参和等待线程结束
#include "pch.h" #include <iostream> #include <windows.h> using namespace std; ...
- Win32线程——等待另一个线程结束
转载: https://blog.csdn.net/yss28/article/details/53646627 <Win32多线程程序设计>–Jim Beveridge & Ro ...
- C# 多线程join的用法,等待多个子线程结束后再执行主线程
等待多个子线程结束后再执行主线程 class MultiThread{ #region join test public void MultiThreadTest() { Thread[] ths = ...
- Java Thread.join()详解--父线程等待子线程结束后再结束
目录(?)[+] 阅读目录 一.使用方式. 二.为什么要用join()方法 三.join方法的作用 join 四.用实例来理解 打印结果: 打印结果: 五.从源码看join()方法 join是Th ...
- join当前线程等待指定的线程结束后才能继续运行
模拟一个QQ游戏大厅斗地主 /** sleep(休眠.睡眠) join当前线程等待指定的线程结束后才能继续运行 */ class Player extends Thread{ private Stri ...
- Java 如何实现线程间通信?(notify、join、CountdownLatch、CyclicBarrier、FutureTask、Callable )
转自:https://mp.weixin.qq.com/s?__biz=MzI4Njc5NjM1NQ==&mid=2247486499&idx=1&sn=d3f2d6959df ...
- java并发系列(二)-----线程之间的协作(wait、notify、join、CountDownLatch、CyclicBarrier)
在java中,线程之间的切换是由操作系统说了算的,操作系统会给每个线程分配一个时间片,在时间片到期之后,线程让出cpu资源,由其他线程一起抢夺,那么如果开发想自己去在一定程度上(因为没办法100%控制 ...
随机推荐
- alert(1) to win 3
function escape(s) { s = JSON.stringify(s); return '<script>console.log(' + s + ');</script ...
- php函数漏洞
1.ereg — 正则表达式匹配 此函数遇 %00 截断. <?php $a = $_GET['pwd']; var_dump(ereg ("^[0-9]+$", $a)); ...
- js关于小数点失精算法修正0.07*100竟然=7.000000000000001
转发 https://blog.csdn.net/iteye_13003/article/details/82645716
- linux系统基础的优化以及常用命令
编辑网卡配置文件 vim /etc/sysconfig/network-scripts/ifcfg-eth0 修改配置参数 ONBOOT= yes启动或者关闭ipsystemctl restart/s ...
- 运行 tensorboard
使用下面命令总是报错: tensorboard --logdir=mylogdir tensorboard --logdir='./mylogdir' 正确命令 tensorboard --logdi ...
- 使用Fabric在tomcat中部署应用的问题总结
关闭tomcat时 A.为什么调用shutdown时,报错连接拒绝 结论——很可能是因为tomcat没启动或没完全启动:而这个时候调用shutdown就会出现此类报错 解决方法:time.sleep ...
- 分享一个知乎答案 最详细易懂的 js闭包
作者:大闲人柴毛毛 链接:https://www.zhihu.com/question/34210214/answer/136673471 来源:知乎 著作权归作者所有.商业转载请联系作者获得授权,非 ...
- linux运维、架构之路-linux磁盘管理
一.企业中磁盘选购: 1.线上的业务,用SAS磁盘 2.线下的业务,用SATA磁盘,磁带库 3.线上高并发.小容量(多人浏览力图片)的业务,SSD磁盘 4.根据数据的访问热度,智能分析分层存储,SAT ...
- CF1073G Yet Another LCP Problem 后缀自动机 + 虚树 + 树形DP
题目描述 记 $lcp(i,j)$ 表示 $i$ 表示 $i$ 这个后缀和 $j$ 这个后缀的最长公共后缀长度给定一个字符串,每次询问的时候给出两个正整数集合 $A$ 和 $B$,求$\sum_{i\ ...
- Java后端进阶教程
https://www.cnblogs.com/caoleiCoding/p/6170555.html 传智播客Java Spring框架:https://www.bilibili.com/video ...