head first Thread.join()
阿新 • • 發佈:2018-12-25
友情推薦:
不使用Thread.join() 測試執行緒
先上程式碼:
/**
* Created by Zero on 2017/8/23.
*/
public class TestJoin implements Runnable {
public static int a = 0;
@Override
public void run() {
for (int i = 0; i < 5; i++) {
a = a + 1 ;
}
}
public static void main(String[] args) throws InterruptedException {
TestJoin j = new TestJoin();
Thread thread = new Thread(j);
thread.start();
System.out.println(a);
}
}
以上示例會輸出5嗎?可能性不大,有可能永遠輸出為0,之前在執行緒池原理的那篇就提到過,執行緒的啟動和銷燬都需要時間,此處因為thread還沒啟動好,或者正在為它分配資源準備執行,就已經執行完輸出了。
怎樣才能確保每次都能輸出5呢?現在有請我們的主角join方法閃亮登場,程式碼如下:
/**
* Created by apple on 2017/8/23.
*/
public class TestJoin implements Runnable {
public static int a = 0;
@Override
public void run() {
for (int i = 0; i < 5; i++) {
a = a + 1;
}
}
public static void main (String[] args) throws InterruptedException {
TestJoin j = new TestJoin();
Thread thread = new Thread(j);
thread.start();
/**
* 測試join方法的作用,與下面的threadAgain執行緒作對比。
*/
thread.join();
System.out.println(a);
a = 0;
Thread threadAgain = new Thread(j);
threadAgain.start();
System.out.println(a);
}
}
輸出的結果將是5和0。
Thread.join()作用
Thread.join(),之前看資料的時候,有些人說可以理解成“將兩個執行緒合併成一個執行緒”,我是覺得這樣說是很不科學的,雖然這樣通俗易懂,但這確實是兩個不同的執行緒,只是在呼叫Thread.join()後,會先執行完Thread執行緒後再去執行當前執行緒,即上述的在主執行緒中執行到thread.join();後,先去執行thread,直到thread執行完後再去執行主執行緒。
測試Thread.join(long millis)
/**
* Created by apple on 2017/8/23.
*/
public class TestJoin implements Runnable {
public static int a = 0;
@Override
public void run() {
for (int i = 0; i < 5; i++) {
a = a + 1;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws InterruptedException {
TestJoin j = new TestJoin();
Thread thread = new Thread(j);
thread.start();
/**
* 測試join方法的作用
*/
thread.join(3000);
System.out.println("thread執行緒結果為:"+a);
a = 0;
Thread threadAgain = new Thread(j);
threadAgain.start();
System.out.println("threadAgain執行緒結果為:"+a);
}
}
輸出:
thread執行緒結果為:3
threadAgain執行緒結果為:0
先上一段原始碼再來分析:
/**
* Waits at most {@code millis} milliseconds for this thread to
* die. A timeout of {@code 0} means to wait forever.
*
* <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;
}
}
}
原始碼爸爸說了,孩子,我給你millis這麼長的時間,能不能完成任務那是你的事情了,能提前完成,咱就提前走下去,不能完成,過期不候,自己看著辦吧。
預設情況下,Thread.join()即Thread.join(0),當為0的時候,那才叫真愛呢,執行緒會一直等下去,知道執行結束為止,才會繼續朝下執行。
isAlive():用來測試執行緒是否處於活動狀態,相當於 run 是否還在執行。
微信掃我^_^