.NET4中多线程并行方法Parallel.ForEach
原文发布时间为:2011-12-10 —— 来源于本人的百度文章 [由搬家工具导入]
namespace ForEachDemo
{
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Linq;
using System.Diagnostics;
class SimpleForEach
{
static void Main()
{
// A simple source for demonstration purposes. Modify this path as necessary.
var watch = new Stopwatch();
var list = Enumerable.Range(0, 10);
// Method signature: Parallel.ForEach(IEnumerable<TSource> source, Action<TSource> body)
watch.Start();
Parallel.ForEach(list, item =>
{
try
{
var t = 2 / item;
Console.WriteLine("Processing {0} on thread {1}", item,
Thread.CurrentThread.ManagedThreadId);
}
catch
{
}
} //close lambda expression
); //close method invocation
watch.Stop();
Console.WriteLine(watch.ElapsedMilliseconds);
// Keep the console window open in debug mode.
Console.WriteLine("Processing complete. Press any key to exit.");
Console.ReadKey();
}
}
}
.NET4中多线程并行方法Parallel.ForEach的更多相关文章
- 网络与多线程---OC中多线程使用方法(一)
小编在此之前,通过一个小例子,简单的形容了一下进程与线程之间的关系,现在网络编程中的多线程说一下!!! *进程的基本概念 每一个进程都是一个应用程序,都有自己独立的内存空间,一般来说一个应用程序存在一 ...
- Unity中使用扩展方法解决foreach导致的GC
对于List这种顺序表,我们解决的时候还是可以使用for代替foreach即可.但是对于非顺序表,比如Dictionary或者Set之类,我们可以扩展方法Foreach,ForeachKey和Fore ...
- C# 多线程 Parallel.For 和 For 谁的效率高?那么 Parallel.ForEach 和 ForEach 呢?
还是那句话:十年河东,十年河西,莫欺少年穷. 今天和大家探讨一个问题:Parallel.For 和 For 谁的效率高呢? 从CPU使用方面而言,Parallel.For 属于多线程范畴,可以开辟多个 ...
- C#中的多线程 - 并行编程 z
原文:http://www.albahari.com/threading/part5.aspx 专题:C#中的多线程 1并行编程Permalink 在这一部分,我们讨论 Framework 4.0 加 ...
- 第九节:深究并行编程Parallel类中的三大方法 (For、ForEach、Invoke)和几大编程模型(SPM、APM、EAP、TAP)
一. 并行编程 1. 区分串行编程和串行编程 ①. 串行编程:所谓的串行编程就是单线程的作用下,按顺序执行.(典型代表for循环 下面例子从1-100按顺序执行) ②. 并行编程:充分利用多核cpu的 ...
- Parallel.ForEach 多线程 声明失败 "未将对象引用设置到对象的实例"
x using System; using System.Collections.Generic; namespace Parallel.ForEach { class Program { //代码结 ...
- Parallel.ForEach() 并行循环
现在的电脑几乎都是多核的,但在软件中并还没有跟上这个节奏,大多数软件还是采用传统的方式,并没有很好的发挥多核的优势. 微软的并行运算平台(Microsoft’s Parallel Computing ...
- Parallel.Foreach的并发问题解决方法-比如爬虫WebClient
场景五:线程局部变量 Parallel.ForEach 提供了一个线程局部变量的重载,定义如下: public static ParallelLoopResult ForEach<TSource ...
- C# 多线程 Parallel.ForEach 和 ForEach 效率问题研究及理解
from:https://blog.csdn.net/li315171406/article/details/78450534 最近要做一个大数据dataTable循环操作,开始发现 运用foreac ...
随机推荐
- iOS-cell的封装和重用
一.通过xib描述cell---(cell的高度一致,cell里面控件布局基本一样): 1.(M) 创建模型: -------------------------------------------- ...
- react+redux状态管理实现排序 合并多个reducer文件
这个demo只有一个reducer 所以合并reducer这个demo用不到 ,但是我写出来这样大家以后可以用到,很好用,管理多个reducer,因为只要用到redux就不会只有一个reducer所以 ...
- luogu4196 [CQOI2006]凸多边形 半平面交
据说pkusc出了好几年半平面交了,我也来水一发 ref #include <algorithm> #include <iostream> #include <cstdi ...
- FileStream流媒体
class Program { static void Main(string[] args) { string source = @"mana.mp4"; string targ ...
- FTP使用心得
1、创建文件夹的函数,一次只能创建一层。 2、没有现成的判断文件夹是否存在的函数,如果文件夹不存在就创建,会报异常。有以下封装好的函数。可以直接调用。 1 2 3 4 5 6 7 8 9 10 11 ...
- 【Hazard of Overfitting】林轩田机器学习基石
首先明确了什么是Overfitting 随后,用开车的例子给出了Overfitting的出现原因 出现原因有三个: (1)dvc太高,模型过于复杂(开车开太快) (2)data中噪声太大(路面太颠簸) ...
- leetcode 【 Remove Duplicates from Sorted List II 】 python 实现
题目: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct ...
- 恢复误删除表黑科技之relay log大法(续)
Preface I've stuck twice in my previous experiments in backing up dropped tables.I am still ...
- java 继承小结
[code=java] //多态的经典例子 //向上转型后,父类只能调用子类和父类的共同方法和的重写方法(方法名相同,参数也相同),不能调用重载方法(方法名相同,但参数不同) class A { pu ...
- Nginx下配置codeigniter框架
原来在winserver+Apache环境下工作良好的一个微信公众号后台迁移到阿里云(环境:Ubuntu 64位 | PHP5.4 | Nginx1.6)下却频出 404,403,只能访问CI rou ...