#region parallel.invoke
/*
资料参考
https://www.cnblogs.com/Leo_wl/p/6218247.html
parallel.invoke()方法 提供了任务并行性模式(也就是多个任务并行运行),
stopWatch.Start(); //开始计时
stopWatch.Stop(); //停止计时
stopWatch.Reset(); //重置StopWatch
stopWatch.Restart(); //重新启动被停止的StopWatch
stopWatch.ElapsedMilliseconds //获取stopWatch从开始到现在的时间差,单位是毫秒
*/
//public class RarallelDemo
//{
// private Stopwatch stopwatch = new Stopwatch();
// public void Run1()
// {
// Thread.Sleep(2000);
// Console.WriteLine("Task 1 is cost 2 sec");
// }
// public void Rum2()
// {
// Thread.Sleep(3000);
// Console.WriteLine("Task 2 is cost 3 sec");
// }
// public static void Main()
// {
// RarallelDemo n = new RarallelDemo();
// n.ParallelInvokeMethod();
// }
// public void ParallelInvokeMethod()
// {
// stopwatch.Start();
// Parallel.Invoke(Run1, Rum2);//invoke 并行执行
// stopwatch.Stop();
// Console.WriteLine("Parallel run " + stopwatch.ElapsedMilliseconds + " ms.");//并行,花费的时间是执行时间最长的的那个线程,
// stopwatch.Restart();//重置时间
// Run1();
// Rum2();//串行执行
// stopwatch.Stop();
// Console.WriteLine("Normal run " + stopwatch.ElapsedMilliseconds + " ms.");//因为是串行,所以花费的时间应该是各线程执行时间的和
// }
//}
////试一下
///
//public class t
//{
// public const int a = 1000;
// public static void Main()
// {
// // ThreadStart threadStart = Tt;
// ThreadStart threadStart1 = Dowork;
// // Thread thread = new Thread(threadStart);
// Thread thread1 = new Thread(threadStart1);
// //thread.Start();
// //for (int count = 0; count < a; count++)
// //{
// // Console.WriteLine(count);
// //}
// //thread.Join();
// }
// public static void Dowork()
// {
// for (int count = 0; count < a; count++)
// {
// Console.WriteLine(count);
// }
// }
//public static void Tt()
//{
// for (int i = 0; i < 100001; i++)
// {
// int a = i / 1000 % 10 ;
// int b = i / 100 %10 ;
// int c = i / 10 %10;
// int d = i / 1 %10;
// if (a*a*a + b*b*b + c*c*c+d*d*d ==i)
// {
// Console.WriteLine(i);
// }
// }
//}
}
#endregion
parallel.invoke
最新推荐文章于 2024-05-18 21:23:33 发布