C# AsParallel.ForAll 与 Parallel.ForEach
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14206834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AsParallel.ForAll vs Parallel.ForEach
提问by Dhawal
Is there any difference between the below code snippets. If so, what?
以下代码片段之间是否有任何区别。如果是这样,是什么?
myList.AsParallel().ForAll(i => { /*DO SOMETHING*/ });
myList.AsParallel().ForAll(i => { /*DO SOMETHING*/ });
and
和
Parallel.ForEach(mylist, i => { /*DO SOMETHING*/ });
Parallel.ForEach(mylist, i => { /*DO SOMETHING*/ });
Will the main thread wait for all the child threads to complete? In a MVC application, if I'm doing parallel processing in my controller action, what happens to the child threads after the main thread completes. Will they be aborted or will they be completed even after the main thread is completed?
主线程会等待所有子线程完成吗?在 MVC 应用程序中,如果我在控制器操作中进行并行处理,主线程完成后子线程会发生什么。它们会被中止还是会在主线程完成后完成?
回答by svick
Parallel.ForEach()
is intended exactly for this kind of code.
Parallel.ForEach()
正是针对这种代码。
On the other hand, ForAll()
is intended to be used at the end of a (possibly complex) PLINQ query.
另一方面,ForAll()
旨在用于(可能是复杂的)PLINQ 查询的末尾。
Because of that, I think Parallel.ForEach()
is the better choice here.
正因为如此,我认为Parallel.ForEach()
这里是更好的选择。
In both cases, the current thread will be used to perform the computations (along with some threads from the thread pool) and the method will return only after all processing has been completed.
在这两种情况下,当前线程将用于执行计算(以及线程池中的一些线程),并且该方法仅在所有处理完成后才会返回。
回答by thewpfguy
You have the option to order the elements with AsParallel().AsOrdered(). Ordering in Parallel.ForEach is not out of box, we need do it.
您可以选择使用 AsParallel().AsOrdered() 对元素进行排序。在 Parallel.ForEach 中订购不是开箱即用的,我们需要这样做。
回答by dsum
Here is an explanation in MSDN:
这是MSDN中的解释:
Based on what I read, use Parallel.ForEach()
if you want to ensure the list are access sequentiallywhile using AsParallel.ForAll()
does not guarantee the items in the list are accessed in order.
根据我读到的内容,Parallel.ForEach()
如果要确保按顺序访问列表,请使用AsParallel.ForAll()
,而使用不能保证按顺序访问列表中的项目。
For MVC, all thread are request-based. The caller thread (main) is blocked until the Parallel() call is completed, that means all child threads should have completed as well.
对于MVC,所有线程都是基于请求的。调用者线程(主线程)被阻塞,直到 Parallel() 调用完成,这意味着所有子线程也应该已经完成。
If the caller thread is aborting, here is an explain:
如果调用者线程正在中止,这里是一个解释:
http://www.albahari.com/threading/part5.aspx
http://www.albahari.com/threading/part5.aspx
PLINQ doesn't preemptively abort threads, because of the danger of doing so. Instead, upon cancellation it waits for each worker thread to finish with its current element before ending the query. This means that any external methods that the query calls will run to completion.
PLINQ 不会抢先中止线程,因为这样做有危险。相反,在取消时,它会等待每个工作线程在结束查询之前完成其当前元素。这意味着查询调用的任何外部方法都将运行完成。