码迷,mamicode.com
首页 > 编程语言 > 详细

C# -- 使用线程池 ThreadPool 执行多线程任务

时间:2018-12-24 16:21:57      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:开始   sedm   void   span   pre   adp   col   []   com   

 

1. 使用线程池

 1 class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             WaitCallback wc1 = s =>{
 6                 Console.WriteLine("线程ID:{0},开始执行", Thread.CurrentThread.ManagedThreadId);
 7                 Stopwatch stw = new Stopwatch();
 8                 stw.Start();
 9                 long result = SumNumbers(10000000);
10                 stw.Stop();
11                 Console.WriteLine("线程ID:{0},执行完成,执行结果:{1},执行用时{2},",Thread.CurrentThread.ManagedThreadId,result,stw.ElapsedMilliseconds);
12             };
13             WaitCallback wc2 = s => {
14                 Console.WriteLine("线程ID:{0},开始执行", Thread.CurrentThread.ManagedThreadId);
15                 Stopwatch stw = new Stopwatch();
16                 stw.Start();
17                 long result = SumNumbers(10000000);
18                 stw.Stop();
19                 Console.WriteLine("线程ID:{0},执行完成,执行结果:{1},执行用时{2},", Thread.CurrentThread.ManagedThreadId, result, stw.ElapsedMilliseconds);
20             };
21             WaitCallback wc3 = s => {
22                 Console.WriteLine("线程ID:{0},开始执行", Thread.CurrentThread.ManagedThreadId);
23                 Stopwatch stw = new Stopwatch();
24                 stw.Start();
25                 long result = SumNumbers(10000000);
26                 stw.Stop();
27                 Console.WriteLine("线程ID:{0},执行完成,执行结果:{1},执行用时{2},", Thread.CurrentThread.ManagedThreadId, result, stw.ElapsedMilliseconds);
28             };
29 
30             
31             ThreadPool.QueueUserWorkItem(wc1);
32             ThreadPool.QueueUserWorkItem(wc2);
33             ThreadPool.QueueUserWorkItem(wc3);
34 
35             Console.ReadKey();
36         }
37 
38         static long SumNumbers(int count)
39         {
40             long sum = 0;
41             for (int i = 0; i < count; i++)
42             {
43                 sum += i;
44             }
45             Thread.Sleep(1000);
46             return sum;
47         }
48     }

执行结果:

技术分享图片

2. 等待线程池的线程执行

 1 class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             using (ManualResetEvent m1 = new ManualResetEvent(false))
 6             using (ManualResetEvent m2 = new ManualResetEvent(false))
 7             using (ManualResetEvent m3 = new ManualResetEvent(false))
 8             {
 9                 ThreadPool.QueueUserWorkItem(
10                     s =>{
11                         Console.WriteLine("线程ID:{0},开始执行", Thread.CurrentThread.ManagedThreadId);
12                         Stopwatch stw = new Stopwatch();
13                         stw.Start();
14                         long result = SumNumbers(10000000);
15                         stw.Stop();
16                         m1.Set();
17                         Console.WriteLine("线程ID:{0},执行完成,执行结果:{1},执行用时{2},", Thread.CurrentThread.ManagedThreadId, result, stw.ElapsedMilliseconds);
18                     });
19 
20                 ThreadPool.QueueUserWorkItem(
21                     s =>{
22                         Console.WriteLine("线程ID:{0},开始执行", Thread.CurrentThread.ManagedThreadId);
23                         Stopwatch stw = new Stopwatch();
24                         stw.Start();
25                         long result = SumNumbers(10000000);
26                         stw.Stop();
27                         m2.Set();
28                         Console.WriteLine("线程ID:{0},执行完成,执行结果:{1},执行用时{2},", Thread.CurrentThread.ManagedThreadId, result, stw.ElapsedMilliseconds);
29                     });
30                 ThreadPool.QueueUserWorkItem(
31                     s =>{
32                         Console.WriteLine("线程ID:{0},开始执行", Thread.CurrentThread.ManagedThreadId);
33                         Stopwatch stw = new Stopwatch();
34                         stw.Start();
35                         long result = SumNumbers(10000000);
36                         stw.Stop();
37                         m3.Set();
38                         Console.WriteLine("线程ID:{0},执行完成,执行结果:{1},执行用时{2},", Thread.CurrentThread.ManagedThreadId, result, stw.ElapsedMilliseconds);
39                     });
40 
41                 //等待线程池的线程执行
42                 m1.WaitOne();
43                 m2.WaitOne();
44                 m3.WaitOne();
45                 Console.WriteLine("所有线程执行完成");
46 
47             }
48 
49             Console.ReadKey();
50         }
51 
52         static long SumNumbers(int count)
53         {
54             long sum = 0;
55             for (int i = 0; i < count; i++)
56             {
57                 sum += i;
58             }
59             Thread.Sleep(3000);
60             return sum;
61         }
62     }

执行结果:

技术分享图片

 

C# -- 使用线程池 ThreadPool 执行多线程任务

标签:开始   sedm   void   span   pre   adp   col   []   com   

原文地址:https://www.cnblogs.com/kliine/p/10168315.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!