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. VS 2005部署应用程序提示“应用程序无法正常启动( 0x0150002)” 解决方案

    遇到这个问题,一定是缺少了CRT.MFC.ATL的DLL,不同版本的VS是不一样的.系统自带这些库的Release版,如果没有自带,打补丁就有了:系统不自带这些库的Debug版,所以Debug版的程序 ...

  2. uva 558 Bellman_Ford

    Bellman_Ford算法   求图中是否存在负权值的回路   若图中不存在   则最短路最多经过n-1个结点   若经过超过n-1个节点 则存在负权值的回路  此图永远无法找到最短路  每条边最多 ...

  3. ActivePython2.7 +Firefly1.2.2+WIN7服务器搭建过程(已通过)

    原地址:http://www.9miao.com/question-15-54027.html 1.ActivePython2.7 版本(内部已经包含easy_install,pywin32)2.所需 ...

  4. Injection Attacks-Log 注入

    日志注入(也称日志文件注入) 很多应用都维护着一系列面向授权用户.通过 HTML 界面展示的日志,因而成为了攻击者的首要目标,这些攻击者试图伪装其他攻击.误导日志读者,甚至对阅读和分析日志监测应用的用 ...

  5. php析构函数

    <?php class Page { function __destruct() { echo "__destruct methor invokes<br>"; ...

  6. UrlRewriteFilter 美化器的使用方法 伪静态化的解决方案(转)

    一,URL美化器简介 UrlRewriteFilter是一个用于改写URL的Web过滤器,类似于Apache的mod_rewrite.适用于任何Web应用服务器(如Resin,Orion,Tomcat ...

  7. jmeter 一个可能引起性能严重下降的断言设置

    在添加断言时一定要注意: 1. 红框部分选择 "响应文本", 2.  要断言的内容越短越好

  8. 使用eclipse远程调试Tomcat的方法

    tomcat是一种非常常见的java web应用服务器,有时候服务器可能并不是部署在本地,而是部署在远程其他的机器上,我们用eclispe该如何进行debug调试呢? 1. 在eclispe中新建we ...

  9. inflate方法与findViewById的区别

    LayoutInflater作用是将layout的xml布局文件实例化为View类对象. 对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate()来找 re ...

  10. 使用xilinx的documentation navigator快速查找资料

    2013-06-22 14:56:39 documentation navigator是xilinx的资料导航,是一个小插件,可以到xilinx的官网上下载,我的是Xilinx_DocNav_2013 ...