您好,登錄后才能下訂單哦!
join
合并線程,插隊(duì)線程,將此線程執(zhí)行完成后,再執(zhí)行其他線程,其他線程阻塞
join是一個(gè)成員方法,必須通過Thread對(duì)象調(diào)用
public class n {
public static void main(String[]args) throws InterruptedException
{
Thread t =new Thread(()-> {
for(int i=0;i<5;i++)
{
System.out.println("a"+i);
}
});
t.start();
for(int i=0;i<5;i++)
{
if(i%2==0)
{
t.join();//插隊(duì),此時(shí)main主線程被阻塞,插隊(duì)線程執(zhí)行完所有步驟再執(zhí)行main
}
System.out.println("b"+i);
}
}
}
例二:
public class n {
public static void main(String[]args) throws InterruptedException
{
new Thread(new father()).start();
}
}
class father extends Thread{
public void run()
{
System.out.println("想抽象,發(fā)現(xiàn)沒了");
System.out.println("讓兒子買中華");
Thread t=new Thread(new son());
t.start(); //不行,各走各的邏輯錯(cuò)誤,再加入join先執(zhí)行完son,再執(zhí)行father剩下的
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("接過煙");
}
}
class son extends Thread{
public void run()
{
System.out.println("拿錢");
System.out.println("路邊玩10秒");
for(int i=0;i<10;i++)
{
System.out.println(i+"秒過去了");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("去買煙");
System.out.println("回家");
}
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。