publicclassTest { publicstaticvoidMainMethod() { var students = new List<Student>() { new Student(){Age=10 },new Student(){ Age=9},new Student(){ Age=9},new Student(){ Age=9} new Student(){ Age=12}};
并行执行任务的算法:pipelining、 stop and go、inverted enumeration
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
//采用 stop & go 算法 var filterStudents3 = (from m in students.AsParallel() where m.Age < 10 selectSetStudent(m)).ToList(); //采用 stop & go 算法 var filterStudents4 = (from m in students.AsParallel() where m.Age < 10 selectSetStudent(m)).ToArray();
//采用 inverted enumeration 算法 var filterStudents5 = from m in students.AsParallel() where m.Age < 10 selectSetStudent(m); //一边计算结果 一边对已经计算出来的元素并行地操作 filterStudents5.ForAll(item => Console.WriteLine(item));
LINQ to Objects 是以懒性的方式对查询操作进行求值,只有真正用到查询结果某个值的时候,系统才会去生成。
namespaceConsoleApp1 { classProgram { staticvoidMain(string[] args) { var answer = from n in Enumerable.Range(0, 300) where n.SomeTest() select n.SomeProjection();
var iter = answer.GetEnumerator(); Console.WriteLine("start iterating"); while (iter.MoveNext()) { Console.WriteLine("called MoveNext"); Console.WriteLine(iter.Current); } } }
namespaceConsoleApp1 { classProgram { staticvoidMain(string[] args) { var answer = from n in ParallelEnumerable.Range(0, 300) where n.SomeTest() select n.SomeProjection();
var iter = answer.GetEnumerator(); Console.WriteLine("start iterating"); while (iter.MoveNext()) { Console.WriteLine("called MoveNext"); Console.WriteLine(iter.Current); } } }
publicstaticstringSomeProjection(thisint input) { Console.WriteLine($"projecting an element:{input}"); return$"Delivered {input} at {DateTime.Now:T}"; } } }
截取部分结果说明: start iterating testing element:0;当前线程:4 testing element:150;当前线程:5 testing element:75;当前线程:3 testing element:225;当前线程:6 testing element:226;当前线程:6 testing element:227;当前线程:6 testing element:228;当前线程:6 testing element:229;当前线程:6 testing element:230;当前线程:6 projecting an element:230 projecting an element:0 projecting an element:150 testing element:76;当前线程:3 testing element:77;当前线程:3 testing element:78;当前线程:3 testing element:79;当前线程:3 testing element:80;当前线程:3 projecting an element:80 testing element:151;当前线程:5 testing element:152;当前线程:5 testing element:153;当前线程:5 testing element:154;当前线程:5 testing element:155;当前线程:5 testing element:156;当前线程:5 testing element:157;当前线程:5 testing element:158;当前线程:5 testing element:159;当前线程:5 testing element:160;当前线程:5 projecting an element:160 testing element:161;当前线程:5 testing element:162;当前线程:5 testing element:231;当前线程:6 testing element:232;当前线程:6 testing element:233;当前线程:6 testing element:234;当前线程:6 testing element:81;当前线程:3 testing element:1;当前线程:4 testing element:163;当前线程:5 testing element:235;当前线程:6 ...... called MoveNext Delivered 230 at 16:05:15 testing element:223;当前线程:5 testing element:131;当前线程:3 testing element:132;当前线程:3 testing element:224;当前线程:5 called MoveNext Delivered 20 at 16:05:15 testing element:133;当前线程:3 testing element:134;当前线程:3 testing element:135;当前线程:3 testing element:136;当前线程:3 testing element:137;当前线程:3 testing element:138;当前线程:3 testing element:139;当前线程:3 testing element:140;当前线程:3 called MoveNext projecting an element:140 testing element:141;当前线程:3 testing element:142;当前线程:3 testing element:143;当前线程:3 testing element:144;当前线程:3 testing element:145;当前线程:3 Delivered 150 at 16:05:15 called MoveNext Delivered 240 at 16:05:15 called MoveNext Delivered 30 at 16:05:15 called MoveNext Delivered 160 at 16:05:15 called MoveNext 出现了交叉情况 testing element:146;当前线程:3 testing element:147;当前线程:3 testing element:148;当前线程:3 testing element:149;当前线程:3 Delivered 250 at 16:05:15 called MoveNext Delivered 40 at 16:05:15 called MoveNext Delivered 80 at 16:05:15 called MoveNext Delivered 170 at 16:05:15 called MoveNext Delivered 260 at 16:05:15 called MoveNext Delivered 50 at 16:05:15 called MoveNext Delivered 90 at 16:05:15 called MoveNext Delivered 180 at 16:05:15 called MoveNext Delivered 270 at 16:05:15 called MoveNext Delivered 60 at 16:05:15 called MoveNext Delivered 100 at 16:05:15 called MoveNext Delivered 190 at 16:05:15 called MoveNext Delivered 280 at 16:05:15 called MoveNext Delivered 70 at 16:05:15 called MoveNext Delivered 110 at 16:05:15 called MoveNext Delivered 200 at 16:05:15 called MoveNext Delivered 290 at 16:05:15 called MoveNext Delivered 120 at 16:05:15 called MoveNext Delivered 210 at 16:05:15 called MoveNext Delivered 130 at 16:05:15 called MoveNext Delivered 220 at 16:05:15 called MoveNext Delivered 140 at 16:05:15 请按任意键继续. . .
classProgram { staticvoidMain(string[] args) { try { var answer = (from n in ParallelEnumerable.Range(0, 300) where n.SomeTest() select n.SomeProjection()).Skip(10).Take(20);
var iter = answer.GetEnumerator(); Console.WriteLine("start iterating"); while (iter.MoveNext()) { Console.WriteLine("called MoveNext"); Console.WriteLine(iter.Current); } } catch(AggregateException ex) { ReportAggregateException(ex); //如果有多项并行操作,该递归,否则不递归也可以,递归的可以通用 } }
BackgroundWorker worker = new BackgroundWorker(); //运行 worker.DoWork += (object sender, DoWorkEventArgs e) => { BackgroundWorker backgroundWorker = sender as BackgroundWorker; int sum = 0; for (int i = 0; i <= (int)e.Argument; i++) { sum += i; } Thread.Sleep(3000); if (worker.CancellationPending == true) //取消判断 { return; } for (int i = 0; i <= (int)e.Argument; i++) { sum += i; } };
BackgroundWorker worker = new BackgroundWorker(); //运行 worker.DoWork += (object sender, DoWorkEventArgs e) => { BackgroundWorker backgroundWorker = sender as BackgroundWorker; int sum = 0; for (int i = 0; i <= (int)e.Argument; i++) { sum += i; } Thread.Sleep(3000); if (worker.CancellationPending == true) //取消判断 { return; } for (int i = 0; i <= (int)e.Argument; i++) { sum += i; } };