java中join的使用
join()應該是我們在java中經常會用到的一個方法,它主要是將當前線程置為WAITTING狀態,然后等待調用的線程執行完畢或被interrupted。
join()是Thread中定義的方法,我們看下他的定義:
/** * Waits for this thread to die. * * <p> An invocation of this method behaves in exactly the same * way as the invocation * * <blockquote> * {@linkplain #join(long) join}{@code (0)} * </blockquote> * * @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 void join() throws InterruptedException {
join(0);
}
我們看下join是怎么使用的,通常我們需要在線程A中調用線程B.join():
public class JoinThread implements Runnable{
public int processingCount = 0;
JoinThread(int processingCount) {
this.processingCount = processingCount;
log.info("Thread Created");
}
@Override
public void run() {
log.info("Thread " + Thread.currentThread().getName() + " started");
while (processingCount > 0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
log.info("Thread " + Thread.currentThread().getName() + " interrupted");
}
processingCount--;
}
log.info("Thread " + Thread.currentThread().getName() + " exiting");
}
@Test
public void joinTest()
throws InterruptedException {
Thread t2 = new Thread(new JoinThread(1));
t2.start();
log.info("Invoking join");
t2.join();
log.info("Returned from join");
log.info("t2 status {}",t2.isAlive());
}
}
我們在主線程中調用了t2.join(),則主線程將會等待t2執行完畢,我們看下輸出結果:
06:17:14.775 [main] INFO com.flydean.JoinThread - Thread Created
06:17:14.779 [main] INFO com.flydean.JoinThread - Invoking join
06:17:14.779 [Thread-0] INFO com.flydean.JoinThread - Thread Thread-0 started
06:17:15.783 [Thread-0] INFO com.flydean.JoinThread - Thread Thread-0 exiting
06:17:15.783 [main] INFO com.flydean.JoinThread - Returned from join
06:17:15.783 [main] INFO com.flydean.JoinThread - t2 status false
當線程已經執行完畢或者還沒開始執行的時候,join()將會立即返回:
Thread t1 = new SampleThread(0);
t1.join(); //returns immediately
join還有兩個帶時間參數的方法:
public final void join(long millis) throws InterruptedException
public final void join(long millis,int nanos) throws InterruptedException
如果在給定的時間內調用的線程沒有返回,則主線程將會繼續執行:
@Test
public void testJoinTimeout()
throws InterruptedException {
Thread t3 = new Thread(new JoinThread(10));
t3.start();
t3.join(1000);
log.info("t3 status {}", t3.isAlive());
}
上面的例子將會輸出:
06:30:58.159 [main] INFO com.flydean.JoinThread - Thread Created
06:30:58.163 [Thread-0] INFO com.flydean.JoinThread - Thread Thread-0 started
06:30:59.172 [main] INFO com.flydean.JoinThread - t3 status true
Join()還有個happen-before的特性,這就是如果thread t1調用 t2.join(), 那么當t2返回時,所有t2的變動都會t1可見。
之前我們講volatile關鍵詞的時候也提到了這個happen-before規則。 我們看下例子:
@Test
public void testHappenBefore() throws InterruptedException {
JoinThread t4 = new JoinThread(10);
t4.start();
// not guaranteed to stop even if t4 finishes.
do {
log.info("inside the loop");
Thread.sleep(1000);
} while ( t4.processingCount > 0);
}
我們運行下,可以看到while循環一直在進行中,即使t4中的變量已經變成了0。
所以如果我們需要在這種情況下使用的話,我們需要用到join(),或者其他的同步機制。
本文的例子可以參考https://github.com/ddean2009/learn-java-concurrency/tree/master/join
更多教程請參考 flydean的博客