Thread.join方法的解析(转)
原文链接:https://www.cnblogs.com/huangzejun/p/7908898.html
1. join() 的示例和作用
1.1 示例

1 // 父线程
2 public class Parent extends Thread {
3 public void run() {
4 Child child = new Child();
5 child.start();
6 child.join();
7 // ...
8 }
9 }


1 // 子线程
2 public class Child extends Thread {
3 public void run() {
4 // ...
5 }
6 }

上面代码展示了两个类:Parent(父线程类),Child(子线程类)。
在 Parent.run() 中,通过 Child child = new Child(); 新建 child 子线程(此时 child 处于 NEW 状态);
然后再调用 child.start()(child 转换为 RUNNABLE 状态);
再调用 child.join()。
在 Parent 调用 child.join() 后,child 子线程正常运行,Parent 父线程会等待 child 子线程结束后再继续运行。
下图是我总结的 Java 线程状态转换图:

1.2 join() 的作用
让父线程等待子线程结束之后才能继续运行。
我们来看看在 Java 7 Concurrency Cookbook 中相关的描述(很清楚地说明了 join() 的作用):
Waiting for the finalization of a thread
In some situations, we will have to wait for the finalization of a thread. For example, we may have a program that will begin initializing the resources it needs before proceeding with the rest of the execution. We can run the initialization tasks as threads and wait for its finalization before continuing with the rest of the program. For this purpose, we can use the join() method of the Thread class. When we call this method using a thread object, it suspends the execution of the calling thread until the object called finishes its execution.
当我们调用某个线程的这个方法时,这个方法会挂起调用线程,直到被调用线程结束执行,调用线程才会继续执行。
2. join() 源码分析
以下是 JDK 8 中 join() 的源码:

1 public final void join() throws InterruptedException {
2 join(0);
3 }
4
5 public final synchronized void join(long millis)
6 throws InterruptedException {
7 long base = System.currentTimeMillis();
8 long now = 0;
9
10 if (millis < 0) {
11 throw new IllegalArgumentException("timeout value is negative");
12 }
13
14 if (millis == 0) {
15 while (isAlive()) {
16 wait(0);
17 }
18 } else {
19 while (isAlive()) {
20 long delay = millis - now;
21 if (delay <= 0) {
22 break;
23 }
24 wait(delay);
25 now = System.currentTimeMillis() - base;
26 }
27 }
28 }
29
30 public final synchronized void join(long millis, int nanos)
31 throws InterruptedException {
32
33 if (millis < 0) {
34 throw new IllegalArgumentException("timeout value is negative");
35 }
36
37 if (nanos < 0 || nanos > 999999) {
38 throw new IllegalArgumentException(
39 "nanosecond timeout value out of range");
40 }
41
42 if (nanos >= 500000 || (nanos != 0 && millis == 0)) {
43 millis++;
44 }
45
46 join(millis);
47 }

我们可以看到 join() 一共有三个重载版本(都是 final method,无法被子类覆盖):
1 public final void join() throws InterruptedException;
2
3 public final synchronized void join(long millis) throws InterruptedException;
4
5 public final synchronized void join(long millis, int nanos) throws InterruptedException;
其中
a. join() 和 join(long millis, int nanos) 最后都调用了 join(long millis)。
b. 带参数的 join() 都是 synchronized method。
c. join() 调用了 join(0),从源码可以看到 join(0) 不断检查当前线程(join() 所属的线程实例,非调用线程)是否是 Active。
d. join() 和 sleep() 一样,都可以被中断(被中断时,会抛出 InterrupptedException 异常);不同的是,join() 内部调用了 wait(),会出让锁,而 sleep() 会一直保持锁。
以本文开头的代码为例,我们分析一下代码逻辑:
Parent 调用 child.join(),child.join() 再调用 child.join(0) (此时 Parent 会获得 child 实例作为锁,其他线程可以进入 child.join() ,但不可以进入 child.join(0), 因为无法获取锁)。child.join(0) 会不断地检查 child 线程是否是 Active。
如果 child 线程是 Active,则循环调用 child.wait(0)(为了防止 Spurious wakeup, 需要将 wait(0) 放入 for 循环体中;此时 Parent 会释放 child 实例锁,其他线程可以竞争锁并进入 child.join(0)。我们可以得知,可以有多个线程等待某个线程执行完毕)。
一旦 child 线程不为 Active (状态为 TERMINATED), child.join(0) 会直接返回到 child.join(), child.join() 会直接返回到 Parent 父线程,Parent 父线程就可以继续运行下去了。
超下来,说不定下次会看看,为别人引一点流,哈哈
Thread.join方法的解析(转)的更多相关文章
- Thread.join()方法
thread.Join把指定的线程加入到当前线程,可以将两个交替执行的线程合并为顺序执行的线程.比如在线程B中调用了线程A的Join()方法,直到线程A执行完毕后,才会继续执行线程B.t.join() ...
- Java Thread.join()方法
一.使用方式. join是Thread类的一个方法,启动线程后直接调用,例如: Thread t = new AThread(); t.start(); t.join(); 二.为什么要用join() ...
- JAVA THREAD.JOIN方法详解
一.使用方式. join是Thread类的一个方法,启动线程后直接调用,例如: Thread t = new AThread(); t.start(); t.join(); 二.为什么要用join() ...
- 多线程--Thread.join方法
在Thread类的Api中,Join的作用是让当前线程等待目标线程结束之后才继续执行. thread.Join把指定的线程加入到当前线程,可以将两个交替执行的线程合并为顺序执行的线程. 比如在线程B ...
- Java java.lang.Thread#join()方法分析
结论:A 线程调用 B 线程对象的 join 方法,则 A 线程会被阻塞,直到 B 线程 挂掉 (Java Doc 原话: Watis for this thread to die). 一.分析 查看 ...
- thread.join() 方法存在的必要性是什么?
好久远的问题,为什么关注这个问题的人这么少? 或许是用到这个功能的情形比较少吧. 1.等待处理结果 为什么要用join()方法在很多情况下,主线程生成并起动了子线程,如果子线程里要进行大量的耗时的运算 ...
- Thread join方法的用途
主线程中会创建多个子线程做一些事情,主线程要用到这些子线程处理的数据,因此它需要等待所有的子线程处理完之后才继续运行.这就要用到join方法了.
- 浅析Thread的join() 方法
Thread中的 join() 方法在实际开发过程中可能用的不是很多,但是在面试中作为考察基本功知识的扎实与否,经常会被用到.因此,对于 Thread 的 join() 方法进行了一定的研究. 常见的 ...
- Thread的join方法
一个线程在执行的过程中,可能调用另一个线程,前者可以称为调用线程,后者成为被调用线程. Thread.Join方法的使用场景:调用线程挂起,等待被调用线程执行完毕后,继续执行. 如下案列: 当NewT ...
随机推荐
- 【WPF】Silverlight中的Action与Trigger
最近做的Silverlight项目上用到了大量的拖拽,自动跟随等功能,由于赶时间,加上对Silverlight半生不熟,用的是最简单也是最不好维护的方法.项目忙完了闲下来,想重构一下代码,想起了Tri ...
- BIO NIO AIO之间的区别
一.BIO.NIO.AIO的基本定义与类比描述: BIO (Blocking I/O):同步阻塞I/O模式,数据的读取写入必须阻塞在一个线程内等待其完成.这里使用那个经典的烧开水例子,这里假设一个烧开 ...
- Confluence 6 从一个模板中创建一个空间
Confluence 已经存储了一系列的模板,这些模板被称为 空间蓝图(space blueprints),这模板具有一些自定义的主页,边栏或者可能有蓝图页面或一些示例内容来帮助你开始使用 Confl ...
- 160. Intersection of Two Linked Lists(剑指Offer-两个链表的第一个公共结点)
题目: Write a program to find the node at which the intersection of two singly linked lists begins. Fo ...
- Matlab-5:牛顿迭代法工具箱
function [f,L]=Newton(f,a) %this is newton teration whic is used for solving implicit One-dimensiona ...
- css中伪类与伪元素的区别
一:伪类:1:定义:css伪类用于向某些选择器添加特殊效果. 伪类其实与普通的css类相类似,可以为已有的元素添加样式,但是他只有处于dom无法描述的状态下才能为文档树中的元素添加样式,所以将其称为伪 ...
- mysql分组统计以及全部统计union all使用
select '全部' AS `organ_category`, COUNT(*) AS amount FROM `organ_new` WHERE `city_code` ='SZ0755' AND ...
- 【LeetCode】Permutation全排列
1. Next Permutation 实现C++的std::next_permutation函数,重新排列范围内的元素,返回按照 字典序 排列的下一个值较大的组合.若其已经是最大排列,则返回最小排列 ...
- Guarding Bananas
Guarding Bananas Once there was a lazy monkey in a forest. But he loved banana too much. One day the ...
- Legal or Not (判断是否存在环)
Legal or Not Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total ...