array(2) { ["docs"]=> array(0) { } ["count"]=> int(0) } 111string(0) "" int(1) int(10) int(70) int(8640000) string(13) "likecs_art_db" array(1) { ["query"]=> array(1) { ["match_all"]=> object(stdClass)#29 (0) { } } } array(1) { ["createtime.keyword"]=> array(1) { ["order"]=> string(4) "desc" } } int(10) int(0) int(8640000) array(2) { ["docs"]=> array(0) { } ["count"]=> int(0) } C# -- 使用Parallel并行执行任务 - 爱码网

C#:使用Parallel并行执行任务

1. 代码实现

 1     class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             TestParallel();
 6             Console.ReadKey();
 7         }
 8 
 9         static void TestParallel()
10         {
11             List<Action> listTask = new List<Action>();
12             for (int i = 0; i < 5; i++)
13             {
14                 listTask.Add(new Action(TestAction));
15             }
16             Parallel.For(0, listTask.Count, new Action<int>(i => listTask[i].Invoke()));
17 
18         }
19 
20         static void TestAction()
21         {
22             string gid = Guid.NewGuid().ToString();
23             Console.WriteLine("当前线程ID:{0},线程启动....GID:{1}", Thread.CurrentThread.ManagedThreadId, gid);
24             Thread.Sleep(1000);
25             Console.WriteLine("当前线程ID:{0},线程结束<<<<....GID:{1}", Thread.CurrentThread.ManagedThreadId, gid);
26         }
27 
28     }

2. 运行结果:
C# -- 使用Parallel并行执行任务

 

相关文章: