我對Thread.Join()
方法有一點困惑。我曾見過THIS MSDN帖子和幾個SO帖子,但無法清除這種困惑。關於Thread.Join的困惑
在多線程的情況下,是否等待所有線程完成?或者阻止下一個線程的執行直到第一個完成?假設以下情形:
List<Thread> myThreads = new List<Threads>();
while(someCondition == true)
{
Thread thread = new Thread(new ThreadStart(delegate
{
processSomeCalculations(x, y);
}));
thread.Start();
myThreads.Add(thread);
}
foreach (Thread thread in myThreads)
{
thread.Join();
}
Print("all threads completed now");
在上述情況下,當thread.Join()
被調用列表中的第一項(即列表的第一個線程),does it mean that thread 2 (i.e, the second thread of the list) can NEVER continue its execution, until first thread has been completed?
OR
這是否意味着,all the threads in the list will continue execution in PARALLEL manner, and PRINT method will be called after all threads have finished execution?
我的問題的總結:在上面的場景中,所有的線程都會在PARALLEL中繼續執行嗎?或者他們會在1st執行完後一個一個地執行?
你爲什麼不寫代碼來測試呢? – Enigmativity