java 中Thread.join()的使用方法
更新時間:2017年04月06日 09:59:04 投稿:lqh
這篇文章主要介紹了java 中Thread.join()的使用方法的相關(guān)資料,需要的朋友可以參考下
java 中Thread.join()的使用方法
如果一個線程A執(zhí)行了thread.join()語句,其含義是:當(dāng)前線程A等待thread線程終止之后才從thread.join()返回。
import java.util.concurrent.TimeUnit; /** * 6-13 */ public class Join { public static void main(String[] args) throws Exception { Thread previous = Thread.currentThread(); for (int i = 0; i < 10; i++) { // 每個線程擁有前一個線程的引用,需要等待前一個線程終止,才能從等待中返回 Thread thread = new Thread(new Domino(previous), String.valueOf(i)); thread.start(); previous = thread; } TimeUnit.SECONDS.sleep(5); System.out.println(Thread.currentThread().getName() + " terminate."); } static class Domino implements Runnable { private Thread thread; public Domino(Thread thread) { this.thread = thread; } public void run() { try { thread.join(); } catch (InterruptedException e) { } System.out.println(Thread.currentThread().getName() + " terminate."); } } }
執(zhí)行結(jié)果:
main terminate. 0 terminate. 1 terminate. 2 terminate. 3 terminate. 4 terminate. 5 terminate. 6 terminate. 7 terminate. 8 terminate. 9 terminate.
相關(guān)文章
線程池ThreadPoolExecutor并行處理實現(xiàn)代碼
這篇文章主要介紹了線程池ThreadPoolExecutor并行處理實現(xiàn)代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-11-11SpringBoot+Redis執(zhí)行l(wèi)ua腳本的5種方式總結(jié)
Lua是一種快速、輕量級的腳本語言,廣泛應(yīng)用于各種領(lǐng)域,包括數(shù)據(jù)庫,Redis作為一個內(nèi)嵌Lua解釋器的NoSQL數(shù)據(jù)庫,允許通過Lua腳本在服務(wù)器端執(zhí)行一些復(fù)雜的操作,本文給大家介紹了使用SpringBoot Redis執(zhí)行l(wèi)ua腳本的五種方式,需要的朋友可以參考下2023-11-11Springboot項目出現(xiàn)java.lang.ArrayStoreException的異常分析
這篇文章介紹了Springboot項目出現(xiàn)java.lang.ArrayStoreException的異常分析,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-12-12