java中join的使用
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");}@Overridepublic 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");}@Testpublic 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 immediatelyjoin還有兩個帶時間參數的方法:
public final void join(long millis) throws InterruptedException public final void join(long millis,int nanos) throws InterruptedException如果在給定的時間內調用的線程沒有返回,則主線程將會繼續執行:
@Testpublic 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 trueJoin()還有個happen-before的特性,這就是如果thread t1調用 t2.join(), 那么當t2返回時,所有t2的變動都會t1可見。
之前我們講volatile關鍵詞的時候也提到了這個happen-before規則。 我們看下例子:
@Testpublic 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
更多精彩內容且看:
- 區塊鏈從入門到放棄系列教程-涵蓋密碼學,超級賬本,以太坊,Libra,比特幣等持續更新
- Spring Boot 2.X系列教程:七天從無到有掌握Spring Boot-持續更新
- Spring 5.X系列教程:滿足你對Spring5的一切想象-持續更新
- java程序員從小工到專家成神之路(2020版)-持續更新中,附詳細文章教程
更多教程請參考 flydean的博客
總結
以上是生活随笔為你收集整理的java中join的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java中线程的生命周期
- 下一篇: 怎么在java中关闭一个thread