比如直接用ThreadPool更优化,而且编程更方便
好处是可以隐式地实用Task,更方便。
可以使用AggregateException(合并的异常)
try
{Task.WaitAll(task1,task2,task3);
}
catch (AggregateException ex)
{foreach (Exception inner in ex.InnerException){Console.WriteLine("Exception type{0} from {1}",inner.GetType(),inner.Source);}
}
Parallel.Invoke(Action[] actions);//并行执行多个任务,直到完成
Parallel.For(0,100,i>={…})
Parallel.ForEach(list,item=>{…})
var a=(from n in persons.AsParallel()
where n.Age>20&&n.Age<25
select n)
.ToList();
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;namespace 使用Task
{internal class Program{static void Main(string[] args){Task[] tasks ={Task.Run(()=>SomeFun()),Task.Run(()=>SomeFun()),};Thread.Sleep(1);for(int i=0;iConsole.WriteLine(tasks[i].Status);//可以查看状态Console.WriteLine(tasks[i].Result);//取Result时,会等到计算结束}Task.WaitAll(tasks);//也可以用这句,来等结束Console.ReadKey();}static void DoSometing() { }static double SomeFun() { Thread.Sleep(50);return 0; }}
}
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace 并行计算矩阵乘法
{internal class Program{static void Main(string[] args){int m = 100, n = 400, t = 1000;double[,] ma = new double[m, n];double[,] mb = new double[n, t];double[,] r1 = new double[m, t];double[,] r2 = new double[m, t];InitMatrix(ma);InitMatrix(mb);InitMatrix(r1);InitMatrix(r2);Console.WriteLine("矩阵乘法");Stopwatch sw = new Stopwatch();sw.Start();MultiMatrixNormal(ma, mb, r1);sw.Stop();Console.WriteLine("普通方法用时" + sw.ElapsedMilliseconds);sw.Restart();MultiMatrixParallel(ma, mb, r2);sw.Stop();Console.WriteLine("并行方法用时" + sw.ElapsedMilliseconds);bool ok = CompareMatrix(r1, r2);Console.WriteLine("结果相同" + ok);Console.ReadKey();}static Random rnd = new Random();static void InitMatrix(double[,] matA){int m = matA.GetLength(0);int n = matA.GetLength(1);for (int i = 0; i < m; i++){for (int j = 0; j < n; j++){matA[i, j] = rnd.Next();}}}static void MultiMatrixNormal(double[,] matA, double[,] matB, double[,] result){int m = matA.GetLength(0);int n = matA.GetLength(1);int t = matB.GetLength(1);for (int i = 0; i < m; i++){for (int j = 0; j < t; j++){double temp = 0;for (int k = 0; k < n; k++){temp += matA[i, k] * matB[k, j];}result[i, j] = temp;}}}static void MultiMatrixParallel(double[,] matA, double[,] matB, double[,] result){int m = matA.GetLength(0);int n = matA.GetLength(1);int t = matB.GetLength(1);Parallel.For(0, m, i =>{for (int j = 0; j < t; j++){double temp = 0;for (int k = 0; k < n; k++){temp += matA[i, k] * matB[k, j];}result[i, j] = temp;}});}static bool CompareMatrix(double[,] matA,double[,] matB){int m = matA.GetLength(0);int n = matA.GetLength(1);for(int i=0;ifor(int j=0;jif (Math.Abs(matA[i, j] - matB[i, j]) > 0.1)return false;}}return true;}}
}
上一篇:day30【代码随想录】回溯之分割回文串、复原IP地址、子集
下一篇:Cocos2dx:如何实现精灵的坐标转换,ConvertToNodeSpace、ConvertToWorldSpace两个函数的使用及注意事项