多執行緒 JOIN

2021-05-10 14:30:17 字數 639 閱讀 3984

join就是阻塞呼叫執行緒,直到該執行緒結束後,呼叫執行緒才能繼續執行。

該影響只存在於執行join方法的執行緒和呼叫該執行緒的執行緒之間

如在t1執行緒中呼叫t2.join(),則需要t2執行緒執行完後t1方能繼續執行

試一試:執行緒的插入

//《c#初學課堂》

//注意新增命名空間

using system.threading;

static void main(string args)

}});

//執行緒b

thread threadb = new thread(delegate()

}//在這裡插入執行緒a

threada.join();

for (int i = 0; i <= 50000000; i++)

}});

//啟動執行緒

threada.start();

threadb.start();

}

執行結果如下,你能分析清楚為什麼嗎?

由執行結果可以看出,一開始兩個執行緒交替進行,當執行緒b執行到語句「threada.join()」時,執行緒a被插入到執行緒b之前,兩個執行緒合併到一起,變為順序執行,直到執行完執行緒a中的所有語句,才去執行執行緒b中剩餘的語句。

C 多執行緒 Join

using system using system.collections.generic using system.linq using system.text using system.threading namespace test console.writeline thread.curre...

Python多執行緒 join

宣告 本文介紹介紹的是thread.threading的 join方法 join timeout none wait until the thread terminates.this blocks the calling thread until the thread whose join meth...

多執行緒Join方法

天意憐幽草,人間重晩晴 a.sleep 5000 讓執行緒睡5秒但是,如果你不知道執行緒b需要執行多長時間,並且在a中需要使用到b中的結果,那麼,這時你就可以使用join方法 下面是具體的例子 可以看到,join long time 方法內部其實是呼叫了wait long time 方法,我們了解到...