线程的合并指的是:将指定的线程加入到当前的线程之中,可以将两个交替执行的线程合并为顺序执行的线程,如果在线程B中调用了线程A的Join()方法,直到线程A执行完毕后,才会继续执行线程B。

public class JoinThread {

public static void main(String[] args) {
//线程的合并
/*
* 把指定的线程加入到当前线程,可以将两个交替执行的线程合并为顺序执行的线程。
* 比如在线程B中调用了线程A的Join()方法,直到线程A执行完毕后,才会继续执行线程B。
* 比如在下面的例子中在20之前两个线程交替执行,但是当join()后一直在执行加入的线程,
* 直到加入的线程完毕再继续执行其他的线程
*/
Join j = new Join();
j.setName("分线程");
j.start();
for (int i = 0; i < 50; i++) {
if(i == 20){
try {
j.join();//当主线程执行到20时,强行将Join线程合并,等Join线程执行完毕,再执行剩下的数字
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName() + ":" + i);
}
}
}
class Join extends Thread{
@Override
public void run() {
for (int i = 0; i < 50; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
}
}
}


输出的结果:

分线程:0
分线程:1
分线程:2
分线程:3
分线程:4
分线程:5
分线程:6
分线程:7
分线程:8
分线程:9
分线程:10
分线程:11
分线程:12
分线程:13
分线程:14
分线程:15
分线程:16
main:0
分线程: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
main:1
main:2
main:3
main:4
main:5
main:6
main:7
main:8
main:9
main:10
main:11
main:12
main:13
main:14
main:15
main:16
main:17
main:18
main:19
main:20
main:21
main:22
main:23
main:24
main:25
main:26
main:27
main:28
main:29
main:30
main:31
main:32
main:33
main:34
main:35
main:36
main:37
main:38
main:39
main:40
main:41
main:42
main:43
main:44
main:45
main:46
main:47
main:48
main:49

源码中的解释:

<span style="font-size:14px;"> /**
* </span><span style="color:#ff0000;"><strong>Waits at most {@code millis} milliseconds for this thread to
* die. A timeout of {@code 0} means to wait forever.注意看这句话,它说的意思就是加入的线程死亡后才继续执行否则,永远等待</strong></span><span style="font-size:14px;">
*
* <p> This implementation uses a loop of {@code this.wait} calls
* conditioned on {@code this.isAlive}. As a thread terminates the
* {@code this.notifyAll} method is invoked. It is recommended that
* applications not use {@code wait}, {@code notify}, or
* {@code notifyAll} on {@code Thread} instances.
*
* @param millis
* the time to wait in milliseconds
*
* @throws IllegalArgumentException
* if the value of {@code millis} is negative
*
* @throws InterruptedException
* if any thread has interrupted the current thread. The
* <i>interrupted status</i> of the current thread is
* cleared when this exception is thrown.
*/
public final synchronized void join(long millis)
throws InterruptedException {
long base = System.currentTimeMillis();
long now = 0;

if (millis < 0) {
throw new IllegalArgumentException("timeout value is negative");
}

if (millis == 0) {
while (isAlive()) {
wait(0);
}
} else {
while (isAlive()) {
long delay = millis - now;
if (delay <= 0) {
break;
}
wait(delay);
now = System.currentTimeMillis() - base;
}
}
}</span>