【发布时间】:2016-01-20 07:40:47
【问题描述】:
我对@987654322@ 方法有点困惑。我看过 THIS MSDN 帖子和一些 SO 帖子,但无法消除这种混乱。
如果有多个线程,是否等待所有线程完成?还是阻塞下一个线程的执行,直到第一个线程完成?假设以下场景:
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?
或
这是否意味着,all the threads in the list will continue execution in PARALLEL manner, and PRINT method will be called after all threads have finished execution?
我的问题总结: 在上述情况下,所有线程都会继续并行执行吗?或者他们会在第一个执行完之后一个一个执行?
【问题讨论】:
-
你为什么不写代码来测试一下?
标签: c# multithreading concurrency parallel-processing