AsParallel.ForAll 与 Parallel.ForEach [英] AsParallel.ForAll vs Parallel.ForEach

查看:116
本文介绍了AsParallel.ForAll 与 Parallel.ForEach的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码片段之间是否有任何区别.如果是,那是什么?

Is there any difference between the below code snippets. If so, what?

myList.AsParallel().ForAll(i => {/*DO SOMETHING*/});

Parallel.ForEach(mylist, i => {/*DO SOMETHING*/});

主线程会等待所有子线程完成吗?在 MVC 应用程序中,如果我在控制器操作中进行并行处理,主线程完成后子线程会发生什么.它们会被中止还是会在主线程完成后完成?

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?

推荐答案

Parallel.ForEach() 正是针对这种代码的.

Parallel.ForEach() is intended exactly for this kind of code.

另一方面,ForAll() 旨在用于(可能是复杂的)PLINQ 查询的末尾.

On the other hand, ForAll() is intended to be used at the end of a (possibly complex) PLINQ query.

因此,我认为 Parallel.ForEach() 在这里是更好的选择.

Because of that, I think Parallel.ForEach() is the better choice here.

在这两种情况下,当前线程将用于执行计算(以及线程池中的一些线程),并且该方法仅在所有处理完成后才会返回.

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.

这篇关于AsParallel.ForAll 与 Parallel.ForEach的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆