使用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%控制 ...
随机推荐
- 多组件共享-vuex
1.解决多个组件共享同一状态数据问题1)多个视图共享同一状态2)来自不同视图的触发事件需要变更同一状态文档API:https://vuex.vuejs.org/zh/api/ 2.组件与store连接 ...
- prototype的用法
定义: prototype 属性使您有能力向对象添加属性和方法. 语法: object.prototype.name=value 实例: function prot(){ this.name = 'J ...
- Zabbix分布式监控系统实践 自定义配置
https://www.zabbix.com/wiki/templates/start 环境介绍OS: Ubuntu 10.10 Server 64-bitServers:zabbix-server: ...
- centos 6.5 安装 nginx
从nginx官网下载安装包:nginx-1.8.1.tar.gz,解压 tar xvf nginx-1.8.1.tar.gz -C /usr/local 安装依赖 yum install gcc yu ...
- mysql导出数据到csv文件
在日常工作中经常会遇见导出表中的数据到csv文件的操作,这里就简单总结一下导出的操作. 下面对csv文件的描述是摘录: 据RFC4180文档设置的,该文档全称Common Format and MIM ...
- fast
# connect timeout in seconds# default value is 30sconnect_timeout=30 # network timeout in seconds# d ...
- leetcode_1293. Shortest Path in a Grid with Obstacles Elimination_[dp动态规划]
题目链接 Given a m * n grid, where each cell is either 0 (empty) or 1 (obstacle). In one step, you can m ...
- ubuntu 18.04下Chromium设置为系统代理
前言 在ubuntu 18.04下挂上ss后firefox能直接上google了但是chromium上不去 会出现下面两种情况 # This site can't be reached xxxxxx ...
- 【2019ICPC西安邀请赛】J.And And And(点分治,贡献)
题意:给定一棵n个点带边权的树,定义每条路径的值为路径上边权的异或和 如果一条路径的值为0,其对答案的贡献为所有包含这条路径的路径条数 求答案膜1e9+7 n<=1e5,0<=边权< ...
- div上下左右居中几种方式
1.绝对定位(常用于登录模块)备注:前提条件div需要有宽高 #html <div class="box"></div> #css .box{ positi ...