微前端架构如何改变企业的开发模式与效率提升
833
2022-11-06
C# 4.0 新特性之并行运算(Parallel)
介绍C# 4.0 的新特性之并行运算
Parallel.For - for 循环的并行运算Parallel.ForEach - foreach 循环的并行运算Parallel.Invoke - 并行调用多个任务Task - 任务,基于线程池。其使我们对并行编程变得更简单,且不用关心底层是怎么实现的PLINQ - 用于对内存中的数据做并行运算,也就是说其只支持 LINQ to Object 的并行运算LIST等泛型启用并行计算很简单,使用AsParallel()即可
示例1、Parallel.For 的 DemoParallel/ParallelFor.aspx.cs
代码
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;namespace CSharp.Parallel{ public partial class ParallelFor : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Normal(); ParallelForDemo(); } private void Normal() { DateTime dt = DateTime.Now; for (int i = 0; i < 20; i++) { GetData(i); } Response.Write((DateTime.Now - dt).TotalMilliseconds.ToString()); Response.Write("
"); Response.Write("
"); } private void ParallelForDemo() { DateTime dt = DateTime.Now; // System.Threading.Tasks.Parallel.For - for 循环的并行运算 System.Threading.Tasks.Parallel.For(0, 20, (i) => { GetData(i); }); Response.Write((DateTime.Now - dt).TotalMilliseconds.ToString()); Response.Write("
"); } private int GetData(int i) { System.Threading.Thread.Sleep(100); Response.Write(i.ToString()); Response.Write("
"); return i; } }}/*运行结果:0123456789101112131415161718192000.0514013119712186281014416531517911300.0077*/2、Parallel.ForEach 的 DemoParallel/ParallelForEach.aspx.cs代码using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;namespace CSharp.Parallel{ public partial class ParallelForEach : System.Web.UI.Page { private List
"); Response.Write("
"); } private void ParallelForEachDemo() { DateTime dt = DateTime.Now; // System.Threading.Tasks.Parallel.ForEach - foreach 循环的并行运算 System.Threading.Tasks.Parallel.ForEach(_data, (index) => { GetData(index); }); Response.Write((DateTime.Now - dt).TotalMilliseconds.ToString()); Response.Write("
"); } private int GetData(int i) { System.Threading.Thread.Sleep(100); Response.Write(i.ToString()); Response.Write("
"); return i; } }}/*运行结果:0123456789101112131415161718192000.0514061218127131943814951510161117600.0154*/3、Parallel.Invoke 的 DemoParallel/ParallelInvoke.aspx.cs代码using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Threading;namespace CSharp.Parallel{ public partial class ParallelInvoke : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { var tasks = new Action[] { () => Task1(), () => Task2(), () => Task3() }; // System.Threading.Tasks.Parallel.Invoke - 并行调用多个任务 System.Threading.Tasks.Parallel.Invoke(tasks); } private void Task1() { Thread.Sleep(3000); Response.Write("Task1 - " + "ThreadId:" + Thread.CurrentThread.ManagedThreadId.ToString() + " - " + DateTime.Now.ToString("HH:mm:ss")); Response.Write("
"); } private void Task2() { System.Threading.Thread.Sleep(3000); Response.Write("Task2 - " + "ThreadId:" + Thread.CurrentThread.ManagedThreadId.ToString() + " - " + DateTime.Now.ToString("HH:mm:ss")); Response.Write("
"); } private void Task3() { System.Threading.Thread.Sleep(3000); Response.Write("Task3 - " + "ThreadId:" + Thread.CurrentThread.ManagedThreadId.ToString() + " - " + DateTime.Now.ToString("HH:mm:ss")); Response.Write("
"); } }}/*运行结果:Task2 - ThreadId:26 - 09:11:58Task1 - ThreadId:25 - 09:11:58Task3 - ThreadId:24 - 09:11:58*/4、Task 的 DemoParallel/ParallelTask.aspx.cs代码/*Task - 任务,基于线程池。其使我们对并行编程变得更简单,且不用关心底层是怎么实现的*/using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Threading;using System.Threading.Tasks;namespace CSharp.Parallel{ public partial class ParallelTask : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { /* * CancellationTokenSource - 取消任务的操作需要用到的一个类 * Token - 一个 CancellationToken 类型的对象,用于通知取消指定的操作 * IsCancellationRequested - 是否收到了取消操作的请求 * Cancel() - 结束任务的执行 * ParallelOptions - 并行运算选项 * CancellationToken - 设置一个 Token,用于取消任务时的相关操作 * MaxDegreeOfParallelism - 指定一个并行循环最多可以使用多少个线程 */ CancellationTokenSource cts = new CancellationTokenSource(); ParallelOptions pOption = new ParallelOptions() { CancellationToken = cts.Token }; pOption.MaxDegreeOfParallelism = 10; Response.Write("开始执行,3.5 秒后结束"); Response.Write("
"); /* * Task - 任务类 * Factory.StartNew() - 创建并开始一个或一批新任务 * ContinueWith() - 此任务完成后执行指定的另一个任务 * AsyncState - 此任务的上下文对象 * Wait() - 阻塞,直到任务完成 */ Task task0 = Task.Factory.StartNew(() => { Thread.Sleep(3500); cts.Cancel(); Response.Write("结束"); Response.Write("
"); }); // 通过 System.Threading.Tasks.Parallel.Invoke 执行任务的时候,可以加入 ParallelOptions 参数,用于对此并行运算做一些配置 System.Threading.Tasks.Parallel.Invoke(pOption, () => Task1(pOption.CancellationToken), () => Task2(pOption.CancellationToken)); /* * 一个 Task 内可以包含多个 Task Task tasks = new Task(() => { Task.Factory.StartNew(() => Method()); Task.Factory.StartNew(() => Method2()); Task.Factory.StartNew(() => Method3()); }); tasks.Start(); // 阻塞,直到整个任务完成 tasks.Wait(); */ /* * 带返回值的 Task Func
注:关于并行运算的实例可以参考http://code.msdn.microsoft.com/ParExtSamples
龙腾一族至尊龙骑
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~