此示例尽量以最简洁的代码演示并行处理的功能,此示例代码中分别用单线程和多线程分别执行5次耗时1秒的操作。打印出执行过程及耗时。

以下为示例代码,.net framework要求4.0以上。

using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks; namespace ParallelDemo
{ class Program
{
static void Main(string[] args)
{
// First do the sequential version.
Console.WriteLine("Executing sequential loop...");
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();

//单线程执行
for (int i = ; i < ; i++)
{
TestMethod();
} stopwatch.Stop();
Console.WriteLine("Sequential loop time in milliseconds: {0}", stopwatch.ElapsedMilliseconds); // Reset timer and results matrix.
stopwatch.Reset(); Console.WriteLine("Executing parallel loop...");
stopwatch.Start();
//多线程执行
Parallel.For(, , i => TestMethod());
stopwatch.Stop();
Console.WriteLine("Parallel loop time in milliseconds: {0}", stopwatch.ElapsedMilliseconds); // Keep the console window open in debug mode.
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
} static void TestMethod()
{
var tid = Thread.CurrentThread.ManagedThreadId;
Console.WriteLine("Thread {0} start", tid);
Thread.Sleep();
Console.WriteLine("Thread {0} end", tid);
}
} }

运行结果

Executing sequential loop...
Thread 9 start
Thread 9 end
Thread 9 start
Thread 9 end
Thread 9 start
Thread 9 end
Thread 9 start
Thread 9 end
Thread 9 start
Thread 9 end
Sequential loop time in milliseconds: 5004
Executing parallel loop...
Thread 9 start
Thread 10 start
Thread 11 start
Thread 12 start
Thread 13 start
Thread 9 end
Thread 10 end
Thread 11 end
Thread 12 end
Thread 13 end
Parallel loop time in milliseconds: 1024
Press any key to exit.

附:官方示例

一个简单示例看懂.Net 并行编程的更多相关文章

  1. web 框架的本质及自定义web框架 模板渲染jinja2 mvc 和 mtv框架 Django框架的下载安装 基于Django实现的一个简单示例

    Django基础一之web框架的本质 本节目录 一 web框架的本质及自定义web框架 二 模板渲染JinJa2 三 MVC和MTV框架 四 Django的下载安装 五 基于Django实现的一个简单 ...

  2. 接口测试之基于LoadRunner的一个简单示例

    这几天一直在捣鼓接口测试,以下总结一下: 1.什么是接口测试:接口是指系统模块与模块之间或者系统与系统之间进行交互,一般我们用的多的是HTTP协议的接口.WebService协议的接口.还有RPC(R ...

  3. Django的下载安装以及实现一个简单示例

    一.Django下载安装 Django下载链接 1. 下载Django: pip3 install django==1.11.9    (大的版本1.11不要错) 2.创建一个django proje ...

  4. asp.net WebService的一个简单示例

    不同的系统之间经常会需要数据的交换对接,而Web Service技术, 能使得运行在不同机器上的不同应用无须借助附加的.专门的第三方软件或硬件, 就可相互交换数据或集成.依据Web Service规范 ...

  5. mvp(2)一个简单示例,加深理解

    参考: http://www.cnblogs.com/liuling/p/mvp-pattern-android.html 架构图: 1.View层 public interface NewsView ...

  6. PGET,一个简单、易用的并行获取数据框架

    使用场景 当我们的服务收到一个请求后,需要大量调用下游服务获取业务数据,然后对数据进行转换.计算后,响应给请求方. 如果我们采用串行获取下游数据,势必会增加响应时长,降低接口的qps.如果是并行获取下 ...

  7. 基于boost的bind与function的一个简单示例消息处理框架

    前两年开始接触boost,boost库真是博大精深:今天简单介绍一下boost中之前用到的的bind与function,感觉挺实用的,分享给大家,我对boost用的也不多,让大家见笑了. 上次文发了一 ...

  8. React的一个简单示例

    首发:个人博客,更新&纠错&回复 React的核心是定义组件类,组件有三个要素:状态.行为.界面. 1.渲染状态到界面:状态由组件对象的state属性持有,从状态到界面的渲染工作由组件 ...

  9. 【转】IEnumerable接口的一个简单示例

    转:https://blog.csdn.net/wang371756299/article/details/8197075 摘要: IEnumerable接口是实现foreach循环的一个重要的接口, ...

随机推荐

  1. org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.

    今天报了这个异常,这是页面报的 org.springframework.dao.DataIntegrityViolationException: could not execute statement ...

  2. 程序包com.sun.image.codec.jpeg不存在

    在pox.xml中引入依赖 <dependency><groupId>rt</groupId><artifactId>rt</artifactId ...

  3. pat1011. World Cup Betting (20)

    1011. World Cup Betting (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Wit ...

  4. go test遇到的一些问题-command-line-arguments undefined: xxxxx

    一 问题是在我写算法题的时候出的,test后缀的文件编译报command-line-arguments undefined: xxxxx 二 没记错,go test是 所有在以_test结尾的源码内以 ...

  5. 吴恩达《Machine Learning Yearning》总结(11-20章)

    11.何时修改开发集.测试集和度量指标 开展一个新项目,尽快选好开发集和测试集:例子,根据度量指标A分类器排在B分类器前面,但是团队认为B分类器在实际产品上优于A分类器,这时就需要考虑修改开发集和测试 ...

  6. 利用pandas生成csv文件

    # -*- coding:UTF-8 -*- import json from collections import OrderedDict with open('dns_status.json',' ...

  7. 【VirtualBox】快照

    一.快照备份 虚拟机系统快照下来,以后就可以恢复到快照之前的系统 右上角->虚拟电脑工具->快照

  8. 调试.NET程序OutOfMemoryException (转载)

    原文地址:http://blog.csdn.net/directionofear/article/details/8009427 应用程序调试,需要有个常规的调试思路,应对各类问题最基本的调试手段是什 ...

  9. mysql数据库将表移动到新数据库,或者说更改数据库名字

    ①创建新的数据库(你要改的名字) CREATE DATABASE new_name; ②更改数据库表的名字 RENAME TABLE  old_name.table TO new_name.table ...

  10. rest-framework框架——认证、权限、频率组件

    一.rest-framework登录验证 1.models.py添加User和Token模型 class User(models.Model): name = models.CharField(max ...