Java_如何等待子线程执行结束
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
</div>public class Threads {public static void main(String[] args) {SubThread thread = new SubThread();thread.start();//主线程处理其他工作,让子线程异步去执行.mainThreadOtherWork();System.out.println("now waiting sub thread done.");//主线程其他工作完毕,等待子线程的结束, 调用join系列的方法即可(可以设置超时时间)try {thread.join();} catch (InterruptedException e) {e.printStackTrace();}System.out.println("now all done.");}private static void mainThreadOtherWork() {System.out.println("main thread work start");try {Thread.sleep(3000L);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("main thread work done.");}public static class SubThread extends Thread{@Overridepublic void run() {working();}private void working() {System.out.println("sub thread start working.");busy();System.out.println("sub thread stop working.");}private void busy() {try {sleep(5000L);} catch (InterruptedException e) {e.printStackTrace();}}}} |
- main thread work start
- sub thread start working.
- main thread work done.
- now waiting sub thread done.
- sub thread stop working.
- now all done.
|
1
2
3
|
public final void join() throws InterruptedException {join(0);} |
public final synchronized void join(long millis)
|
1
2
3
|
while (isAlive()) {wait(0);} |
Tests if this thread is alive. A thread is alive if it has been started and has not yet died.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
public class Threads {static ExecutorService executorService = Executors.newFixedThreadPool(1);@SuppressWarnings("rawtypes")public static void main(String[] args) throws InterruptedException, ExecutionException {SubThread thread = new SubThread();// thread.start();Future future = executorService.submit(thread);mainThreadOtherWork();System.out.println("now waiting sub thread done.");future.get();// try {// thread.join();// } catch (InterruptedException e) {// e.printStackTrace();// }System.out.println("now all done.");executorService.shutdown();}private static void mainThreadOtherWork() {System.out.println("main thread work start");try {Thread.sleep(3000L);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("main thread work done.");}public static class SubThread extends Thread{@Overridepublic void run() {working();}private void working() {System.out.println("sub thread start working.");busy();System.out.println("sub thread stop working.");}private void busy() {try {sleep(5000L);} catch (InterruptedException e) {e.printStackTrace();}}}} |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
public class Threads {// static ExecutorService executorService = Executors.newFixedThreadPool(1);static final BlockingQueue queue = new ArrayBlockingQueue(1);public static void main(String[] args) throws InterruptedException, ExecutionException {SubThread thread = new SubThread(queue);thread.start();// Future future = executorService.submit(thread);mainThreadOtherWork();System.out.println("now waiting sub thread done.");// future.get();queue.take();// try {// thread.join();// } catch (InterruptedException e) {// e.printStackTrace();// }System.out.println("now all done.");// executorService.shutdown();}private static void mainThreadOtherWork() {System.out.println("main thread work start");try {Thread.sleep(3000L);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("main thread work done.");}public static class SubThread extends Thread{private BlockingQueue queue;/*** @param queue*/public SubThread(BlockingQueue queue) {this.queue = queue;}@Overridepublic void run() {try{working();}finally{try {queue.put(1);} catch (InterruptedException e) {e.printStackTrace();}}}private void working() {System.out.println("sub thread start working.");busy();System.out.println("sub thread stop working.");}private void busy() {try {sleep(5000L);} catch (InterruptedException e) {e.printStackTrace();}}}} |


|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
public class Threads {// static ExecutorService executorService = Executors.newFixedThreadPool(1);static final BlockingQueue queue = new ArrayBlockingQueue(1);public static void main(String[] args) throws InterruptedException, ExecutionException {int threads = 5;CountDownLatch countDownLatch = new CountDownLatch(threads);for(int i=0;i<threads;i++){SubThread thread = new SubThread(2000*(i+1), countDownLatch);thread.start();}// Future future = executorService.submit(thread);mainThreadOtherWork();System.out.println("now waiting sub thread done.");// future.get();// queue.take();countDownLatch.await();// try {// thread.join();// } catch (InterruptedException e) {// e.printStackTrace();// }System.out.println("now all done.");// executorService.shutdown();}private static void mainThreadOtherWork() {System.out.println("main thread work start");try {Thread.sleep(3000L);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("main thread work done.");}public static class SubThread extends Thread{// private BlockingQueue queue;private CountDownLatch countDownLatch;private long work;/*** @param queue*/// public SubThread(BlockingQueue queue) {// this.queue = queue;// this.work = 5000L;// }public SubThread(long work, CountDownLatch countDownLatch) {// this.queue = queue;this.countDownLatch = countDownLatch;this.work = work;}@Overridepublic void run() {try{working();}finally{// try {// queue.put(1);// } catch (InterruptedException e) {// e.printStackTrace();// }countDownLatch.countDown();}}private void working() {System.out.println(getName()+" sub thread start working.");busy();System.out.println(getName()+" sub thread stop working.");}private void busy() {try {sleep(work);} catch (InterruptedException e) {e.printStackTrace();}}}} |
Java_如何等待子线程执行结束的更多相关文章
- Java如何等待子线程执行结束
工作中往往会遇到异步去执行某段逻辑, 然后先处理其他事情, 处理完后再把那段逻辑的处理结果进行汇总的产景, 这时候就需要使用线程了. 一个线程启动之后, 是异步的去执行需要执行的内容的, 不会影响主线 ...
- Java主线程如何等待子线程执行结束(转)
工作中往往会遇到异步去执行某段逻辑, 然后先处理其他事情, 处理完后再把那段逻辑的处理结果进行汇总的产景, 这时候就需要使用线程了. 一个线程启动之后, 是异步的去执行需要执行的内容的, 不会影响主线 ...
- Java多线程--让主线程等待子线程执行完毕
使用Java多线程编程时经常遇到主线程需要等待子线程执行完成以后才能继续执行,那么接下来介绍一种简单的方式使主线程等待. java.util.concurrent.CountDownLatch 使用c ...
- Java线程池主线程等待子线程执行完成
今天讨论一个入门级的话题, 不然没东西更新对不起空间和域名~~ 工作总往往会遇到异步去执行某段逻辑, 然后先处理其他事情, 处理完后再把那段逻辑的处理结果进行汇总的产景, 这时候就需要使用线程了. 一 ...
- C#主线程等待子线程运行结束
佐左佑右 原文 C#主线程等待子线程运行结束 由于主程序中调用matlab的dll文件进行计算要用较长的时间,主界面会有很长时间的卡顿,造成的用户感受十分不好,因此我想在调用时,将调用放入子线程中,然 ...
- java多线程实现主线程等待子线程执行完问题
本文介绍两种主线程等待子线程的实现方式,以5个子线程来说明: 1.使用Thread的join()方法,join()方法会阻塞主线程继续向下执行. 2.使用Java.util.concurrent中的C ...
- Java Thread.join()详解--父线程等待子线程结束后再结束
目录(?)[+] 阅读目录 一.使用方式. 二.为什么要用join()方法 三.join方法的作用 join 四.用实例来理解 打印结果: 打印结果: 五.从源码看join()方法 join是Th ...
- Java主线程等待子线程、线程池
public class TestThread extends Thread { public void run() { System.out.println(this.getName() + &qu ...
- Java并发编程原理与实战六:主线程等待子线程解决方案
本文将研究的是主线程等待所有子线程执行完成之后再继续往下执行的解决方案 public class TestThread extends Thread { public void run() { Sys ...
随机推荐
- Runtime - 消息发送原理
Runtime - 消息发送原理. Objective-C运行时的核心就在于消息分派器objc_msgSend,消息分派器把选择器映射为函数指针,并调用被引用的函数. 要想理解objc_msgSend ...
- django Rest Framework----GenericAPIView 通用视图 GenericAPIView源码分析
一.GenericAPIView GenericAPIView扩展了APIView,为标准列表和详细视图添加了常见的行为. 提供的每个具体通用视图都是一个GenericAPIView或多个mixin类 ...
- RabbitMQ--Publish/Subscribe(五)
上篇文章中,我们实现了不同consumer接收不同级别的日志,这篇文章中,不以日志级别,使用不同日志来源.比如kernel.*.*.critical. 这就要使用topic exchange完成了.将 ...
- rsync + inotify 实时同步
1. 前言 2 台 nginx 需要做集群, 静态文件和php文件都在nginx服务器本地. 有三种方案: (1)NFS (2)Rsync + inotify (3)共享存储服务器 第一种:当 nfs ...
- Ubuntu 搭建ELK
一.简介 官网地址:https://www.elastic.co/cn/ 官网权威指南:https://www.elastic.co/guide/cn/elasticsearch/guide/curr ...
- NopCommerce Plugins 不能智能提示的解决方法(MVC 5 & RAZOR 3.0)
分享给需要的朋友: http://mhammadchehab.com/wordpress/2013/12/enabling-intellisense-for-razor-in-class-librar ...
- OA项目CRUD和单元测试(一)
使用ModeFirst方法生成数据库,EntityFramework5.0. 一:Model层的模型:(根据模型生成数据库) 二:Dal层的UserInfo代码: namespace SunOA.EF ...
- 彻底卸载sql2008后重新安装
彻底卸载sql2008方法 --打开控制面板,在控制面板中卸载所有带sql server的程序. --删除C:\Program Files\Microsoft SQL Server这整个文件夹, -- ...
- HTML5移动应用左右滑动touchmove touchmove touchend 实例
也是刚开始接触移动前端,大虾别喷 <!DOCTYPE HTML> <html> <head> <meta name="viewport" ...
- loadrunner 脚本中文乱码
loadrunner 脚本中文乱码 1.新建脚本--->选择协议(Http)-->选项-->高级-->选择“支持字符集”并点选“UTF-8”: 2.在回放脚本之前:Vuser- ...