请解释一下 AsParallel()
有人可以向我解释一件事吗?据我了解 AsParallel() 在自己的任务中执行。那么,如果查询返回大量数据,那么当“foreach”开始执行 Console.WriteLine 时,变量“d”可以为空吗?
var integerList = Enumerable.Range(1, 100);
var d = from x in integerList.AsParallel()
where x <= 25
select x;
foreach (var v in d)
{
Console.WriteLine(v);
}
Can somebody explain me one thing. As I understand AsParallel() executes in own task. So, if query return huge amount of data the variable 'd' can be empty at time when 'foreach' started to execute Console.WriteLine?
var integerList = Enumerable.Range(1, 100);
var d = from x in integerList.AsParallel()
where x <= 25
select x;
foreach (var v in d)
{
Console.WriteLine(v);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
AsParallel
是一项PLINQ
功能。PLINQ
自动并行化本地LINQ
查询。PLINQ
的优点是易于使用,因为它减轻了框架的工作分区和结果整理的负担。要使用
PLINQ
,只需对输入序列调用AsParallel()
,然后照常继续LINQ
查询。在您的情况下,变量
d
不能不为空,只是因为PLINQ
。如果它为空,则意味着集合中没有满足条件x <= 25
的元素。您可以在此处阅读更多内容
AsParallel
is aPLINQ
feature.PLINQ
automatically parallelizes localLINQ
queries.PLINQ
has the advantage of being easy to use in that it offloads the burden of both work partitioning and result collation to the Framework.To use
PLINQ
, simply callAsParallel()
on the input sequence and then continue theLINQ
query as usual.Variable
d
in your case can not be empty only becausePLINQ
. If it will be empty that means there is no elements in the collection that satisfy the conditionx <= 25
.You can read more here
不会。添加 .AsParallel() 后,PLINQ 将使用经典数据并行评估技术在所有可用处理器上透明地执行Where、OrderBy 和Select。实际上,直到您在 foreach 循环中“触及”查询之前,查询根本不会执行(PLINQ 与 LINQ 一样使用延迟执行)。主线程将停止执行,直到像往常一样从查询执行中返回。
此处有其他信息..
No. Once you have added .AsParallel(), PLINQ will transparently execute the Where, OrderBy, and Select on all of the available processors using classic data parallel evaluation techniques. Actually query isn't executed at all until you 'touch' it in foreach loop (PLINQ uses deffered execution just as LINQ). Main thread will halt execution until return from query execution as usual.
Additional info here..