在 PLINQ 中,还可以使用 foreach 执行查询以及循环访问结果。 但是,foreach 本身不会并行运行,因此,它要求将所有并行任务的输出合并回该循环正在上面运行的线程中。 在 PLINQ 中,在必须保留查询结果的最终排序,以及以按串行方式处理结果时,例如当为每个元素调用 Console.WriteLine 时,则可以使用 foreach。 为了在无需顺序暂留以及可自行并行处理结果时更快地执行查询,请使用 ForAll 方法执行 PLINQ 查询。

ForEach:进行顺序迭代,不执行并行

ForAll:并行任务

测试代码:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace parallelTests
{
class MainClass
{ public static object _locker = new object ();
public static void Main (string[] args)
{
Console.WriteLine ("Hello World!"); var lstData = new List<string>{ "a","b","c","d" }; lstData.AsParallel().ForEach (x => { Thread.Sleep(*); Console.WriteLine("this is:{0}",x); }); Console.ReadKey ();
return; //非并行
lstData.ForEach (x => {
lock (_locker) {
Thread.Sleep(*);
}
Console.WriteLine("this is:{0}",x); }); // 并行 lstData.AsParallel().ForAll (x => { //1 同步locker
lock (_locker) {//--并行locker
Thread.Sleep(*);
} //2 use 并行
Thread.Sleep(*); Console.WriteLine("this is:{0}",x); }); Console.ReadKey (); }
} public static class ForEachExtension
{
//示范: this.GetControls<Button>(null).ForEach(b => b.Enabled = false);
/// <summary>
/// 变量枚举元素 并执行Action
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source"></param>
/// <param name="action"></param>
public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
{
if (source is ParallelQuery<T>)
{
throw new Exception("ParallelQuery Not Support This Extentsion!");
}
foreach (var item in source)
action(item);
} /// <summary>
/// 在使用可迭代类型结合进行Count()的替代
/// 防止 yield return 带来的性能问题
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source"></param>
/// <returns></returns> public static bool IsEmpty<T>(this IEnumerable<T> source)
{
if (null==source)
{
return true;
}
return !source.Any();
}
public static bool IsNotEmpty<T>(this IEnumerable<T> source)
{
if (null == source)
{
return false;
}
return source.Any();
} public static T[] Remove<T>(this T[] objects, Func<T, bool> condition)
{
var hopeToDeleteObjs = objects.Where(condition); T[] newObjs = new T[objects.Length - hopeToDeleteObjs.Count()]; int counter = ;
for (int i = ; i < objects.Length; i++)
{
if (!hopeToDeleteObjs.Contains(objects[i]))
{
newObjs[counter] = objects[i];
counter += ;
}
} return newObjs;
}
} }

PLINQ的 ForAll 对比集合的ForEach的更多相关文章

  1. MyBatis参数传入集合之foreach动态sql

    foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合.foreach元素的属性主要有item,index,collection,open,separator,close.ite ...

  2. MyBatis参数传入集合之foreach用法

    传入集合list // 账户类型包括门店和分公司 List<Object> scopeList = new ArrayList<Object>(); scopeList.add ...

  3. 使用yield关键字让自定义集合实现foreach遍历

    一般来说当我们创建自定义集合的时候为了让其能支持foreach遍历,就只能让其实现IEnumerable接口(可能还要实现IEnumerator接口) 但是我们也可以通过使用yield关键字构建的迭代 ...

  4. .net学习之集合、foreach原理、Hashtable、Path类、File类、Directory类、文件流FileStream类、压缩流GZipStream、拷贝大文件、序列化和反序列化

    1.集合(1)ArrayList内部存储数据的是一个object数组,创建这个类的对象的时候,这个对象里的数组的长度为0(2)调用Add方法加元素的时候,如果第一次增加元神,就会将数组的长度变为4往里 ...

  5. 集合、拆箱、装箱、自定义集合的foreach

    集合部分 参考:http://msdn.microsoft.com/zh-cn/library/0ytkdh4s(v=vs.110).aspx 集合类型是诸如哈希表.队列.堆栈.包.字典和列表等数据集 ...

  6. C# 通过IEnumberable接口和IEnumerator接口实现自定义集合类型foreach功能

    1.IEnumerator和IEnumerable的作用 其实IEnumerator和IEnumerable的作用很简单,就是让除数组和集合之外的类型也能支持foreach循环,至于foreach循环 ...

  7. MyBatis 批量操作、集合遍历-foreach

    在使用mybatis操作数据库时,经常会使用到批量插入.IN条件查询的情况,这时就难免要使用到foreach元素.下面一段话摘自mybatis官网: foreach 元素的功能是非常强大的,它允许你指 ...

  8. Java中数组和集合的foreach操作编译后究竟是啥

    今天和同事在关于foreach编译后是for循环还是迭代器有了不同意见,特做了个Demo,了解一下. 是啥自己来看吧! public class Demo { public static void m ...

  9. C# 通过IEnumberable接口和IEnumerator接口实现泛型和非泛型自定义集合类型foreach功能

    IEnumerator和IEnumerable的作用 其实IEnumerator和IEnumerable的作用很简单,就是让除数组和集合之外的类型也能支持foreach循环,至于foreach循环,如 ...

随机推荐

  1. gcc如何生成预编译头文件(.gch)

    1 建立comm.h 2 main.c中包含comm.h : #include "comm.h" 3 gcc -o comm.h.gch comm.h(低版本gcc会有bug) 4 ...

  2. jQuery事件控制点击内容下拉

    1.设计实例 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. ...

  3. 一个CSS背景颜色问题

    div{ background: rgba(0,0,0,.1);     background-color: #19FFFFFF; } 安卓下会显示上面的透明颜色,而在iOS上则显示下面的颜色,并且其 ...

  4. JFinal配合Shiro权限控制在FreeMarker模板引擎中控制到按钮粒度的使用

    实现在FreeMarker模板中控制对应按钮的显示隐藏主要用到了Shiro中的hasRole, hasAnyRoles, hasPermission以及Authenticated等方法,我们可以实现T ...

  5. C4 垃圾回收

    使用C4垃圾回收器可以有效提升对低延迟有要求的企业级Java应用程序的伸缩性. 到目前为止,stop-the-world式的垃圾回收视为影响Java应用程序伸缩性的一大障碍,而伸缩性又是现代企业级Ja ...

  6. Postgresql 启动could not create listen socket for "localhost"错误的解决

    新装的postgresql在第一次启动时可能会遇到错误,日志中的记录是: could not create listen socket for "localhost" 到/etc/ ...

  7. mysql select column default value if is null

    mysql select column default value if is null SELECT `w`.`city` AS `city`, `w`.`city_en` AS `city_en` ...

  8. 安装RabbitMQ编译erlang时,checking for c compiler default output file name... configure:error:C compiler cannot create executables See 'config.log' for more details.

    checking for c compiler default output file name... configure:error:C compiler cannot create executa ...

  9. leetCode刷题(找出数组里的两项相加等于定值)

    最近被算法虐了一下,刷一下leetcode,找找存在感 如题: Given an array of integers, return indices of the two numbers such t ...

  10. 看完这篇Linux基本的操作就会了

    前言 只有光头才能变强 这个学期开了Linux的课程了,授课的老师也是比较负责任的一位.总的来说也算是比较系统地学习了一下Linux了~~~ 本文章主要是总结Linux的基础操作以及一些简单的概念~如 ...