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. c# Oracle 远程连接方式 plsql 连接oracle 11g 64位

    1.本地连接字符串:   string connect = "Data Source=orcl;user=XXX;password=XXX;Persist Security Info=Tru ...

  2. Microsoft.Web.Administration in IIS

    http://blogs.msdn.com/b/carlosag/archive/2006/04/17/microsoftwebadministration.aspx 最好使用在IIS8中,因为为每一 ...

  3. VS2012编译出来的程序,在XP上运行,出现“.exe 不是有效的 win32 应用程序” “not a valid win32 application”

    升级vs2010到vs2012,突然发现build出来的应用程序无法运行,提示“不是有效的 win32 应用程序” or “not a valid win32 application”. 参考CSDN ...

  4. js检测对象的类型

    在JavaScript中,想要判断某个对象值属于哪种内置类型,最靠谱的做法就是通过Object.prototype.toString方法. 示例: var array=[1,2,3]; Object. ...

  5. CSS+DIV:父DIV相对定位+子DIV绝对定位

    如何在一个div内将一个div进行绝对定位呢?很简单,把父div的position属性设为relative,子div的position属性设为absolute就可以了... 示例: <html& ...

  6. Usermod 命令详解

    参考资料:usermod manpage usermod - 修改用户帐户信息 modify a user account usermod [options] user_name usermod 命令 ...

  7. Tomcat多次部署

    http://blog.csdn.net/knityster/article/details/6300804

  8. HDU2697+DP

    dp[i][j]:从前i个中挑出某些且cost不超过j的最大val. dp[i][j]:应该有1到i-1的dp[k][j-?]来更新!! /* DP dp[i][j]:从前i个中挑出某些且cost不超 ...

  9. P151、面试题27:二叉搜索树与双向链表

    题目:输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表.要求不能创建任何新的结点,只能调整树中结点指针的指向.(本质是中序遍历)二叉树结点的定义如下:struct BinaryTreeNod ...

  10. movzbl和movsbl

    汇编语言中最最常用的指令 -- 数据传送指令,也是我们接触的第一种类别的汇编指令.其指令的格式为:“mov 源操作数, 目的操作数”.mov系列支持从最小一个字节到最大双字的访问与传送.其中movb用 ...