1 join介紹
join()定義在Thread.java中
join()的作用:讓"主線程"等待"子線程"結束之後才能繼續執行,下面看例子
// 主線程
public class Father extends Thread {
public void run() {
Son s = new Son();
s.start();
s.join();
...
}
}
// 子線程
public class Son extends Thread {
public void run() {
...
}
}
上面中有兩個類(主線程類)和Son(子線程類)。因爲Son是在Father中創建並啓動的,所以Father是主線程類,Son是子線程類,在Father主線程中,通過new Son()新建子線程s 接着通過s.start()啓動子線程s,並且調用s.join()之後,Father主線程會一直等待,直到子線程s運行完畢,在子線程s運行完畢之後,Father主線程才能接着運行。這也就是我們所說的"join()的作用",是讓主線程會等待子線程結束之後才能繼續運行,當然了這個主子線程僅僅是相對我們創建的線程的範圍來說的
2join()源碼分析 基於jdk1.7
public final void join() throws InterruptedException {
join(0);
}
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;
}
}
}
從代碼中,我們可以發現。當millis == 0時,會進入到while(isAlive())循環;只要子線程是活的,主線程就不停的等待。
雖然s.join()被調用的地方是發生在“Father主線程”中,但是s.join()是通過“子線程s”去調用的join()。那麼,join()方法中的isAlive()應該是判斷“子線程s”是不是Alive狀態;對應的wait(0)也應該是“讓子線程s”等待纔對。但如果是這樣的話,s.join()的作用怎麼可能是“讓主線程等待,直到子線程s完成爲止”呢,應該是讓"子線程等待纔對(因爲調用子線程對象s的wait方法嘛)"?
答案:wait()的作用是讓"當前線程"等待,而這裏的"當前線程"指的是當前在CPU上運行的線程,所以,雖然調用子線程的wait()方法,但是他通過"主線程去調用的";所以休眠的是主線程,而不是子線程!
3 join()示例
package com.tuhu.filt.javadatasleep;
public class JoinTest {
public static void main(String[] args) {
try{
//新建線程t1
ThreadA2 t1 = new ThreadA2("t1");
//啓動線程t1
t1.start();
//將"線程t1"加入到主線程main()中
//並且主線程main()會等待它的完成
t1.join();
System.out.printf("%s finish\n", Thread.currentThread().getName());
}catch (InterruptedException e){
e.printStackTrace();
}
}
static class ThreadA2 extends Thread{
public ThreadA2(String name){
super(name);
}
public void run(){
System.out.printf("%s start\n", this.getName());
//延時操作
for(int i=0;i<1000000;i++){
;
}
System.out.printf("%s finish\n", this.getName());
}
}
}
01 在主線程main中通過new Thread("t1")新建t1.接着 通過t1.start()啓動"線程t1"並執行t1.join()
02 執行t1.join()之後,"主線程main"會進入"阻塞狀態" 等待t1運行結束 "子線程t1"結束之後 會喚醒"主線程main" "主線程"重新獲得cpu執行權 繼續執行