Java線程中Thread方法下的Join方法詳解
等待線程執(zhí)行終止的join方法
在項(xiàng)目中往往會(huì)遇到這樣一個(gè)場(chǎng)景,就是需要等待幾件事情都給做完后才能走下面的事情。這個(gè)時(shí)候就需要用到Thread方法下的Join方法。join方法是無(wú)參且沒(méi)有返回值的。
package com.baidu.onepakage;
public class JoinTest {
public static void main(String[] args) throws InterruptedException {
Thread theadOne = new Thread(() -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("TheadOne run over");
});
Thread threadTwo = new Thread(() -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("TheadTwo run over");
});
theadOne.start();
Thread.sleep(1000);
threadTwo.start();
System.out.println("main 開(kāi)始啟動(dòng)了");
theadOne.join();
threadTwo.join();
System.out.println("main 結(jié)束了");
}
}上面代碼在調(diào)用join方法的時(shí)候,主線程就被被阻塞了,只有當(dāng)調(diào)用join的方法執(zhí)行結(jié)束都才能夠接著往下面執(zhí)行。
執(zhí)行結(jié)果:
System.out.println(“TheadOne run over”);
System.out.println(“TheadTwo run over”);
System.out.println(“main 開(kāi)始啟動(dòng)了”);
System.out.println(“main 結(jié)束了”);
另外線程A調(diào)用線程B的join方法,當(dāng)其他線程調(diào)用了線程A的interrupt()方法,則A線程會(huì)拋出InterruptedException異常而返回。
示例:
package com.baidu.onepakage;
public class JoinTest01 {
public static void main(String[] args) {
Thread threadOne = new Thread(() -> {
for (; ; ) {
}
});
// 獲取主線程
Thread mainThread = Thread.currentThread();
// 線程2
Thread threadTwo = new Thread(() -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 中斷主線程
mainThread.interrupt();
});
threadOne.start();
threadTwo.start();
try {
threadOne.join();
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println(Thread.currentThread().getName() + "發(fā)生了異常");
}
}
}上面是Mian主方法拋出了異常,這是因?yàn)樵谠谡{(diào)用ThreadOne線程,和ThreadTwo線程時(shí)線程one還在執(zhí)行中(死循環(huán)),這個(gè)時(shí)候main方法處于阻塞狀態(tài),當(dāng)調(diào)用主方法的interrupt()方法后,Main方法已經(jīng)被阻塞了,所以就拋出了異常并返回了。
到此這篇關(guān)于Java線程中Thread方法下的Join方法詳解的文章就介紹到這了,更多相關(guān)Thread類下的Join方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java運(yùn)行時(shí)數(shù)據(jù)區(qū)域和類結(jié)構(gòu)詳解
這篇文章主要介紹了java運(yùn)行時(shí)數(shù)據(jù)區(qū)域和類結(jié)構(gòu),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
淺談為什么重寫(xiě)equals()就要重寫(xiě)hashCode()
困擾我很久的問(wèn)題,一直不明白為什么重寫(xiě)equals()方法的時(shí)候要重寫(xiě)hashCode()方法,這次總算弄明白了,作此分享,感興趣的可以了解一下2021-10-10
客戶端Socket與服務(wù)端ServerSocket串聯(lián)實(shí)現(xiàn)網(wǎng)絡(luò)通信
這篇文章主要為大家介紹了客戶端Socket與服務(wù)端ServerSocket串聯(lián)實(shí)現(xiàn)網(wǎng)絡(luò)通信的內(nèi)容詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-03-03
Java8內(nèi)存模型PermGen Metaspace實(shí)例解析
這篇文章主要介紹了Java8內(nèi)存模型PermGen Metaspace實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
spring cloud實(shí)現(xiàn)Eureka注冊(cè)中心的HA的方法
本篇文章主要介紹了spring cloud實(shí)現(xiàn)Eureka注冊(cè)中心的HA的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
SpringCache輕松啟用Redis緩存的全過(guò)程
Spring Cache是Spring提供的一種緩存抽象機(jī)制,旨在通過(guò)簡(jiǎn)化緩存操作來(lái)提高系統(tǒng)性能和響應(yīng)速度,本文將給大家詳細(xì)介紹SpringCache如何輕松啟用Redis緩存,文中有詳細(xì)的代碼示例供大家參考,需要的朋友可以參考下2024-07-07

