我对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