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 ...
- .Net主线程扑捉子线程中的异常
首先看一段C#代码:运行后发现主线程通过try{}catch{}是不能扑捉子线程中的抛出来的异常. 代码 ); } public void run() { ...
- Java中主线程如何捕获子线程抛出的异常
首先明确线程代码的边界.其实很简单,Runnable接口的run方法所界定的边界就可以看作是线程代码的边界.Runnable接口中run方法原型如下: public void run(); 而所有的具 ...
- c/c++中主线程退出,子线程也会退出
#include <windows.h> #include <process.h> /* _beginthread, _endthread */ #include <io ...
随机推荐
- ubuntu 源更新(sources.list)
首先备份源列表: sudo cp /etc/apt/sources.list /etc/apt/sources.list_backup 而后用gedit或其他编辑器打开(也可以复制到Windows下打 ...
- java 执行bat批处理文件 并关闭cmd窗口
java 执行bat批处理文件 并关闭cmd窗口 import java.io.IOException; public class CmdMain { public static void main( ...
- CAN总线优点
废除传统的站地址编码,代之以对通信数据块进行编码,可以多主方式工作: 采用非破坏性仲裁技术,当两个节点同时向网络上传送数据时,优先级低的节点主动停止数据发送,而优先级高的节点可不受影响继续传输数据,有 ...
- module_param 用于动态开启/关闭 驱动打印信息
1.定义模块参数的方法: module_param(name, type, perm); 其中,name:表示参数的名字; type:表示参数的类型; perm:表示参数的访问权限 ...
- 安装android studio时候弹出unable to access android sdk add-on list解决方法
本文转载自:http://www.cnblogs.com/rancvl/p/6081791.html Android Studio First Run 检测 Android SDK 及更新,由于众所周 ...
- 《转载》ubuntu Sublime text 3 解决中文输入问题
其实,在这个文章之前,网上都有好多教程了.不知道是不是因为复制黏贴的传播太多,导致有些字符串的丢失,导致编译失败,so库文件无法载入,从而不能输入中文.折腾了许久之后,终于搞定了.记录下来,方便自己下 ...
- Maria数据库
项目上要进行数据库选型,业务上来讲,数据是非常结构化的数据,使用传统关系数据库更适合:另外项目采用微服务框架,每个服务的数据库应该尽可能轻量级, 最后考虑Maria数据库. MariaDB简介: Ma ...
- Fragment的陷阱:概述
现在主流的APP都会使用到Fragment,相信你也一定使用过,今天为大家介绍一下我曾经踏过的一个关于Fragment的坑. 以前做过的一个项目,Fragment嵌套高德地图,当再次进入Fragmen ...
- Ansible之Playbooks的when语句
在使用ansible做自动化运维的时候,大多数情况下都执行某些任务的时候都需要依赖某个变量的值或者是上一个任务的执行结果.如,根据facts信息中的系统版本相关的信息来确定使用哪种包管理器安装软件.A ...
- fiddler代理hosts配置
1 需求背景 fidder开启后,C:\Windows\System32\drivers\etc\hosts配置失效问题:fiddler本身代理hosts配置表,修改后,可以省去在手机等代理使用者的系 ...