[英]AsParallel.ForAll vs Parallel.ForEach
Is there any difference between the below code snippets.以下代码片段之间是否有任何区别。 If so, what?如果是这样,是什么?
myList.AsParallel().ForAll(i => { /*DO SOMETHING*/ });
and和
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.在 MVC 应用程序中,如果我在控制器操作中进行并行处理,主线程完成后子线程会发生什么。 Will they be aborted or will they be completed even after the main thread is completed?它们会被中止还是会在主线程完成后完成?
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.在这两种情况下,当前线程将用于执行计算(以及线程池中的一些线程),并且该方法仅在所有处理完成后才会返回。
Here is an explanation in MSDN:这是MSDN中的解释:
https://docs.microsoft.com/en-us/dotnet/standard/parallel-programming/potential-pitfalls-with-plinq#prefer-forall-to-foreach-when-it-is-possible https://docs.microsoft.com/en-us/dotnet/standard/parallel-programming/potential-pitfalls-with-plinq#prefer-forall-to-foreach-when-it-is-possible
Based on what I read, use Parallel.ForEach()
if you want to ensure the list are access sequentially while 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.对于MVC,所有线程都是基于请求的。 The caller thread (main) is blocked until the Parallel() call is completed, that means all child threads should have completed as well.调用者线程(主线程)被阻塞,直到 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. PLINQ 不会抢先中止线程,因为这样做有危险。 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.这意味着查询调用的任何外部方法都将运行完成。
As written in Concurrency in C# Cookbook :正如C# Cookbook中的Concurrency所写:
One difference between
Parallel
and PLINQ is that PLINQ assumes it can use all of the cores on the computer, whileParallel
will dynamically react to changing CPU conditions.Parallel
和 PLINQ 之间的一个区别是 PLINQ 假设它可以使用计算机上的所有内核,而Parallel
会动态地对不断变化的 CPU 条件做出反应。
You have the option to order the elements with AsParallel().AsOrdered().您可以选择使用 AsParallel().AsOrdered() 对元素进行排序。 Ordering in Parallel.ForEach is not out of box, we need do it.在 Parallel.ForEach 中订购不是开箱即用的,我们需要这样做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.