Parallel task in C# 4.0
2013-01-10 09:55
369 查看
来源:http://www.dotnetjalps.com/2012/12/Parallel-task-with-task-parallel-library-in-Csharp.html
In today’s computing world is all about Parallel processing. You have multicore CPU where you have different core doing different work parallel or its doing same task parallel. For example I am having 4-core CPU
as follows. So the code that I write should take care of this. C# does provide that kind of facility to write code for multi core CPU with task parallel library. We will explore that in this post.
Now Let’s write a program that will have some will have some CPU intensive code that will take some time to complete work. First we will use normal way of calling this task and measure the time and then we will use
Task Parallel library feature of C# 4.0. Following is a code for that.
Here you can see that it has taken almost 2 seconds to complete this task. To use parallel task library we have to useSystem.Threading.Tasks name space. So I have changed the above code like following
to get advantage of Parallel Task.
delegate to call that. Now let’s run that and see how much time it will take.
Here you can see it has taken 1 second to complete same task as it has completed this tasks parallel. Hope you like it. Stay tuned for more.
-------------------备注--------------------
执行并行任务也可以用Task,与Paralle.Invoke的区别是,Task创建的任务是异步执行,不等待任务执行完毕就返回的,例如下面执行后显示Time consumed: 00:00:00.0008202。
Stopwatch stopWatch =new Stopwatch();
stopWatch.Start();
Task t = new Task(DoSomeWork);
t.Start();
Task.Factory.StartNew(DoSomework2);
stopWatch.Stop();
Console.WriteLine("Time consumed: {0}", stopWatch.Elapsed);
Task的返回类型代码:
Task<int> t = new Task<int>(DoSomeWork);
int result = t.Result;
//......
public int DoSomeWork()
{
return 10;
}
In today’s computing world is all about Parallel processing. You have multicore CPU where you have different core doing different work parallel or its doing same task parallel. For example I am having 4-core CPU
as follows. So the code that I write should take care of this. C# does provide that kind of facility to write code for multi core CPU with task parallel library. We will explore that in this post.
Now Let’s write a program that will have some will have some CPU intensive code that will take some time to complete work. First we will use normal way of calling this task and measure the time and then we will use
Task Parallel library feature of C# 4.0. Following is a code for that.
using System; using System.Threading; using System.Diagnostics; namespaceParrallelTask { classProgram { staticvoid Main(string[] args) { Stopwatch stopWatch =new Stopwatch(); stopWatch.Start(); DoSomeWork(); DoSomework2(); stopWatch.Stop(); Console.WriteLine("Time consumed: {0}", stopWatch.Elapsed); } publicstatic voidDoSomeWork() { Thread.Sleep(1000); Console.WriteLine("Work completed"); } publicstatic voidDoSomework2() { Thread.Sleep(1000); Console.WriteLine("Work completed2"); } } }As you can see in above code that we have two cpu intensive method DoSomeWork and DoSomeWork2 and we have called it one by one. Now let’s run the console application.
Here you can see that it has taken almost 2 seconds to complete this task. To use parallel task library we have to useSystem.Threading.Tasks name space. So I have changed the above code like following
to get advantage of Parallel Task.
using System; using System.Threading; using System.Diagnostics; using System.Threading.Tasks; namespaceParrallelTask { classProgram { staticvoid Main(string[] args) { Stopwatch stopWatch =new Stopwatch(); stopWatch.Start(); Parallel.Invoke( newAction(DoSomeWork), newAction(DoSomework2) ); stopWatch.Stop(); Console.WriteLine("Time consumed: {0}", stopWatch.Elapsed); } publicstatic voidDoSomeWork() { Thread.Sleep(1000); Console.WriteLine("Work completed"); } publicstatic voidDoSomework2() { Thread.Sleep(1000); Console.WriteLine("Work completed2"); } } }So here you can see I have invoked two CPU consuming task parallel invoke and usedaction
delegate to call that. Now let’s run that and see how much time it will take.
Here you can see it has taken 1 second to complete same task as it has completed this tasks parallel. Hope you like it. Stay tuned for more.
-------------------备注--------------------
执行并行任务也可以用Task,与Paralle.Invoke的区别是,Task创建的任务是异步执行,不等待任务执行完毕就返回的,例如下面执行后显示Time consumed: 00:00:00.0008202。
Stopwatch stopWatch =new Stopwatch();
stopWatch.Start();
Task t = new Task(DoSomeWork);
t.Start();
Task.Factory.StartNew(DoSomework2);
stopWatch.Stop();
Console.WriteLine("Time consumed: {0}", stopWatch.Elapsed);
Task的返回类型代码:
Task<int> t = new Task<int>(DoSomeWork);
int result = t.Result;
//......
public int DoSomeWork()
{
return 10;
}
相关文章推荐
- Dynamic in C# 4.0, Part 1
- Dynamic in C# 4.0, Part 2
- Dynamic in C# 4.0, Part 3
- NET 4.0 - C# 4.0 新特性之并行运算(Parallel)
- ASP.NET MVC 2.0 in Vs2010 :使用C# 4.0中使用动态类型来传递ViewData
- 改善C#程序的建议10:用Parallel简化Task
- 精进不休 .NET 4.0 (5) - C# 4.0 新特性之并行运算(Parallel)
- C# 4.0 的新特性之并行运算 Parallel
- Task cancellation in C# and things you should know about it
- Net 4.0 Parallel 编程(九)Task中的数据共享(下)
- 精进不休 .NET 4.0 (5) - C# 4.0 新特性之并行运算(Parallel)
- C#并行开发_Thread/ThreadPool, Task/TaskFactory, Parallel
- C# Task.WhenAll Parallel
- 编写高质量代码改善C#程序的157个建议——建议82:Parallel简化但不等同于Task默认行为
- parallel programming. this causual litery nots represents my recent progress in parallel programming in c#.It`s interesting.
- 《Pro .Net 4 Parallel Programming in C#》笔记
- (转).NET 4.5中使用Task.Run和Parallel.For()实现的C# Winform多线程任务及跨线程更新UI控件综合实例
- Dynamic in C# 4.0, Part 4
- Concept Of Optional Parameter in C# 4.0