简体   繁体   English

AsParallel.ForAll 与 Parallel.ForEach

[英]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, while Parallel 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.

相关问题 使用.AsParallel()。ForAll或Parallel.ForEach性能问题并行化任务 - Parallelizing a task using .AsParallel().ForAll or Parallel.ForEach performance issue Parallel.ForEach()与foreach(IEnumerable <T> .AsParallel()) - Parallel.ForEach() vs. foreach(IEnumerable<T>.AsParallel()) 为什么Parallel.ForEach比AsParallel()。ForAll()要快得多,即使MSDN另有建议? - Why is Parallel.ForEach much faster then AsParallel().ForAll() even though MSDN suggests otherwise? 在虚拟机上使用AsParallel()和/或Parallel.ForEach - Using AsParallel() and/or Parallel.ForEach on a virtual machine 使用AsParallel或Parallel.ForEach控制线程数 - Controlling number of threads using AsParallel or Parallel.ForEach Parallel.ForEach 与异步 lambda 等待所有迭代完成 - Parallel.ForEach with async lambda waiting forall iterations to complete parallel linq:AsParallel()。forAll()使一些对象为空 - parallel linq: AsParallel().forAll() nulls some objects Parallel.Foreach与Foreach的数据迁移过程 - Parallel.Foreach vs Foreach for data migration process 为什么foreach与Parallel.ForEach之间存在这种差异? - Why this difference between foreach vs Parallel.ForEach? Parallel.Foreach与Foreach和局部变量中的Task - Parallel.Foreach vs Foreach and Task in local variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM