namespace TPLTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
//var list = testFillParallel();
//int i = list.Count(); Stopwatch watch = new Stopwatch();
watch.Start();
for (int i = ; i < ; i++)
{
ProcessLongTime(i);
}
//watch.Stop();
this.label1.Text = string.Format("顺序执行用时:" + watch.ElapsedMilliseconds); watch.Restart();
//watch.Start();
Parallel.For(, , p =>
{
ProcessLongTime(p);
//MessageBox.Show(p.ToString());
});
//watch.Stop();
this.label2.Text = string.Format("并行执行用时:" + watch.ElapsedMilliseconds); //watch.Restart();
////watch.Start();
//for (int i = 1; i <= 5; i++)
//{
// Task myTask = new Task(obj => ProcessLongTime((int)obj), i);
// myTask.Start();
//}
////watch.Stop();
//this.label3.Text = string.Format("并行Task执行用时:" + watch.ElapsedMilliseconds); watch.Restart();
int count = ;
int results;
Semaphore semaphore = new Semaphore(, count);
System.Threading.Tasks.Parallel.For(, count, i =>
{
results = ProcessLongTimeI(i);
semaphore.Release();
}); //for (var i = 0; i <= count; i++)
//{
// semaphore.WaitOne();
// //Console.WriteLine("Got " + i);
//}
watch.Stop();
this.label3.Text = string.Format("并行Task执行用时:" + watch.ElapsedMilliseconds);
} private IEnumerable<Person> testFillParallel()
{
//var list = new List<Person>(9);
var list = new BlockingCollection<Person>(); //必须使用线程安全的集合类型 Enumerable.Range(, ).AsParallel().ForAll(n =>
{
var name = "Person" + n%;
if (list.Count(p => p.Name == name) < ) list.Add(new Person {Id = n, Name = name});
});
this.label1.Text=string.Format("Person's count is {0}", list.Count);
return list;
} private void ProcessLongTime(int mi)
{
for (int i = ; i < ; i++)
{
i++;
i--;
}
} private int ProcessLongTimeI(int mi)
{
for (int i = ; i < ; i++)
{
i++;
i--;
}
return mi;
} private void button2_Click(object sender, EventArgs e)
{
List<int> ls=new List<int>();
for (int i = ; i < ; i++)
{
ls.Add(i);
} Stopwatch watch = new Stopwatch();
watch.Start();
foreach (int i in ls)
{
ProcessLongTime(i);
}
this.label1.Text = string.Format("顺序执行用时:" + watch.ElapsedMilliseconds); watch.Restart();
Parallel.ForEach(ls,p=>
{
ProcessLongTime(p);
//MessageBox.Show(p.ToString());
});
this.label2.Text = string.Format("并行执行用时:" + watch.ElapsedMilliseconds); watch.Restart();
int count = ;
int results;
Semaphore semaphore = new Semaphore(, ls.Count);
System.Threading.Tasks.Parallel.ForEach(ls, p =>
{
results = ProcessLongTimeI(p);
semaphore.Release();
}); watch.Stop();
this.label3.Text = string.Format("并行Task执行用时:" + watch.ElapsedMilliseconds);
}
}

TPL的更多相关文章

  1. 今天再给大家带点html5前端开发的干货模板“text/tpl”怎么用 script template怎么用

    text/tpl 顾名思义就是模板,其实和C++模板函数类似的作用,就是利用他生成一个HMTL内容,然后append或者替换html里面 有什么好处,假如后端返回来的数据都是一样的,但是需要生成不同的 ...

  2. 发布一个开源极致的javascript模板引擎tpl.js

    tpl.js(大家直接去https://git.oschina.net/tianqiq/tpl.js这个上面看) 简介 tpl.js是一个比较极致(极小,极快,极简单)的js模板引擎,可以在各种js环 ...

  3. 如何在tpl模版的div块中加ztree

    ld-ztree.tpl <div class="ld-ztree-container"> <div class="ld-ztree-header te ...

  4. TPL实现Task.WhileAll扩展方法

    文章翻译整理自 Nikola Malovic 两篇博文: Task.WhileAll Awaitable task progress reporting 当 Task.WhenAll 遇见 Task. ...

  5. tpl + ccr

    不是非此即彼的场景.如下混合使用CCR+TPL的代码说明问题:It's not an either/or scenario.You can intermix CCR and TPL code like ...

  6. C#5.0之后推荐使用TPL(Task Parallel Libray 任务并行库) 和PLINQ(Parallel LINQ, 并行Linq). 其次是TAP(Task-based Asynchronous Pattern, 基于任务的异步模式)

    学习书籍: <C#本质论> 1--C#5.0之后推荐使用TPL(Task Parallel Libray 任务并行库) 和PLINQ(Parallel LINQ, 并行Linq). 其次是 ...

  7. Using the Task Parallel Library (TPL) for Events

    Using the Task Parallel Library (TPL) for Events The parallel tasks library was introduced with the ...

  8. TPL(Task Parallel Library)多线程、并发功能

    The Task Parallel Library (TPL) is a set of public types and APIs in the System.Threading and System ...

  9. 一个使用C#的TPL Dataflow Library的例子:分析文本文件中词频

    博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:一个使用C#的TPL Dataflow Library的例子:分析文本文件中词频.

  10. /Home/Tpl/Equipment/rangeIndex.html 里调用魔板

    <pre name="code" class="html">demo:/var/www/DEVOPS# vim ./Home/Tpl/Equipme ...

随机推荐

  1. 微软职位内部推荐-SDE II-MODC-Beijing

    微软近期Open的职位: JOB TITLE: Software Design Engineer IIDEPARTMENT: Microsoft Office Division ChinaIMMEDI ...

  2. python学习笔记15(面向对象编程)

    虽然Python是解释性语言,但是它是面向对象的,能够进行对象编程. 一.如何定义一个类 在进行python面向对象编程之前,先来了解几个术语:类,类对象,实例对象,属性,函数和方法. 类是对现实世界 ...

  3. IOS调用相机和相册时无法显示中文

    调用系统相册.相机发现是英文的系统相簿界面后标题显示“photos”,但是手机语言已经设置显示中文 需要在info.plist做如下设置 info.plist里面添加 Localizedresourc ...

  4. SQL Server 和 Oracle 以及 MySQL 有哪些区别?

    SQL,在这里我理解成SQL Server.三者是目前市场占有率最高(依安装量而非收入)的关系数据库,而且很有代表性.排行第四的DB2(属IBM公司),与Oracle的定位和架构非常相似,就不赘述了. ...

  5. C# DataTable

    http://www.cnblogs.com/xun126/archive/2010/12/30/1921557.html http://msdn.microsoft.com/zh-cn/librar ...

  6. ExtJS4.2学习(20)动态数据表格之前几章总结篇1(转)

    鸣谢:http://www.shuyangyang.com.cn/jishuliangongfang/qianduanjishu/2014-02-18/196.html --------------- ...

  7. Oracle创建触发器实现主键自增

    CREATE OR REPLACE TRIGGER "trigger_empl" before insert on extjsTest1.t_empl for each row b ...

  8. 深入浅出 Java 8 Lambda 表达式

    摘要:此篇文章主要介绍 Java8 Lambda 表达式产生的背景和用法,以及 Lambda 表达式与匿名类的不同等.本文系 OneAPM 工程师编译整理. Java 是一流的面向对象语言,除了部分简 ...

  9. Nagios Apache报Internal Server Error错误的解决方法

    今天配置Nagios的时候遇到了一些麻烦,前面的步骤都一切顺利,nagios运行后,可以看到nagios的主页,但点击左边的菜单时总是提示Internal Server Error错误.错误如下: v ...

  10. SetTimer在无窗口和有窗口线程的使用 . .

    今天犯了一个粗心的错误,在无窗口线程中,SetTimer中设置计时器ID,而WM_TIMER消息响应函数中得到的计时器ID却不是之前设置的计时器ID. 对应计时器ID的输出的是一个随机数字. 原来在m ...