thread.Join把指定的线程加入到当前线程,可以将两个交替执行的线程合并为顺序执行的线程。比如在线程B中调用了线程A的Join()方法,直到线程A执行完毕后,才会继续执行线程B。
t.join(); //当前线程中加入线程t,当t执行完成后,当前线程才继续进行。
t.join(1000); //等待 t 线程,等待时间是1000毫秒,无论t是否执行完,主线程1000s后都将会执行。
应用一 : 实现多个线程的串行化调度。代码如下:
package com.xx.concurrent.commonUse;
public class SerialThread {
public static void main(String[] args) throws InterruptedException {
Thread[] threads = new Thread[100];
for(int i=0; i< 100; ++i){
if (i == 0 ){
threads[i] = new Thread(new MyThread(i,null));
}else{
threads[i] = new Thread(new MyThread(i,threads[i-1]));
}
threads[i].start();
}
}
}
class MyThread implements Runnable {
private int id ;
private Thread priorThread ;
public MyThread(int id,Thread priorThread ){
this.id = id ;
this.priorThread = priorThread;
}
@Override
public void run() {
if (priorThread != null){
try {
priorThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Thread-" + this.id + " is running over .....");
}
}
输出结果:
Thread-0 is running over .....
Thread-1 is running over .....
Thread-2 is running over .....
Thread-3 is running over .....
Thread-4 is running over .....
Thread-5 is running over .....
Thread-6 is running over .....
Thread-7 is running over .....
Thread-8 is running over .....
Thread-9 is running over .....
Thread-10 is running over .....
Thread-11 is running over .....
Thread-12 is running over .....
Thread-13 is running over .....
Thread-14 is running over .....
Thread-15 is running over .....
Thread-16 is running over .....
Thread-17 is running over .....
Thread-18 is running over .....
Thread-19 is running over .....
Thread-20 is running over .....
Thread-21 is running over .....
Thread-22 is running over .....
Thread-23 is running over .....
Thread-24 is running over .....
Thread-25 is running over .....
Thread-26 is running over .....
Thread-27 is running over .....
Thread-28 is running over .....
Thread-29 is running over .....
Thread-30 is running over .....
Thread-31 is running over .....
Thread-32 is running over .....
Thread-33 is running over .....
Thread-34 is running over .....
Thread-35 is running over .....
Thread-36 is running over .....
Thread-37 is running over .....
Thread-38 is running over .....
Thread-39 is running over .....
Thread-40 is running over .....
Thread-41 is running over .....
Thread-42 is running over .....
Thread-43 is running over .....
Thread-44 is running over .....
Thread-45 is running over .....
Thread-46 is running over .....
Thread-47 is running over .....
Thread-48 is running over .....
Thread-49 is running over .....
Thread-50 is running over .....
Thread-51 is running over .....
Thread-52 is running over .....
Thread-53 is running over .....
Thread-54 is running over .....
Thread-55 is running over .....
Thread-56 is running over .....
Thread-57 is running over .....
Thread-58 is running over .....
Thread-59 is running over .....
Thread-60 is running over .....
Thread-61 is running over .....
Thread-62 is running over .....
Thread-63 is running over .....
Thread-64 is running over .....
Thread-65 is running over .....
Thread-66 is running over .....
Thread-67 is running over .....
Thread-68 is running over .....
Thread-69 is running over .....
Thread-70 is running over .....
Thread-71 is running over .....
Thread-72 is running over .....
Thread-73 is running over .....
Thread-74 is running over .....
Thread-75 is running over .....
Thread-76 is running over .....
Thread-77 is running over .....
Thread-78 is running over .....
Thread-79 is running over .....
Thread-80 is running over .....
Thread-81 is running over .....
Thread-82 is running over .....
Thread-83 is running over .....
Thread-84 is running over .....
Thread-85 is running over .....
Thread-86 is running over .....
Thread-87 is running over .....
Thread-88 is running over .....
Thread-89 is running over .....
Thread-90 is running over .....
Thread-91 is running over .....
Thread-92 is running over .....
Thread-93 is running over .....
Thread-94 is running over .....
Thread-95 is running over .....
Thread-96 is running over .....
Thread-97 is running over .....
Thread-98 is running over .....
Thread-99 is running over .....
Thread中的join源码如下:
/**
* Waits at most <code>millis</code> milliseconds for this thread to
* die. A timeout of <code>0</code> means to wait forever.
*
* @param millis the time to wait in milliseconds.
* @exception 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;
}
}
}
join方法首先判断线程t的状态,线程状态为已启动状态下,才有效,因此t.join()一般在t.start()之后。主线程t.join时候,会获得线程对象t的锁(wait 意味着拿到该对象的锁),或者调用该对象的wait(等待时间),直到该对象唤醒主线程。可以看出t线程的运行结束即可唤醒主线程。