C# · 12月 20, 2021

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

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

1. 使用线程池

Main(= s =>= result = SumNumbers(= s =>= s => ThreadPool.QueueUserWorkItem(wc1); ThreadPool.QueueUserWorkItem(wc2); ThreadPool.QueueUserWorkItem(wc3); Console.ReadKey(); } </span><span style=”color: #0000ff”&gt;static</span> <span style=”color: #0000ff”&gt;long</span> SumNumbers(<span style=”color: #0000ff”&gt;int</span><span style=”color: #000000″&gt; count) { </span><span style=”color: #0000ff”&gt;long</span> sum = <span style=”color: #800080″&gt;0</span><span style=”color: #000000″&gt;; </span><span style=”color: #0000ff”&gt;for</span> (<span style=”color: #0000ff”&gt;int</span> i = <span style=”color: #800080″&gt;0</span>; i < count; i++<span style=”color: #000000″&gt;) { sum </span>+=<span style=”color: #000000″&gt; i; } Thread.Sleep(</span><span style=”color: #800080″&gt;1000</span><span style=”color: #000000″&gt;); </span><span style=”color: #0000ff”&gt;return</span><span style=”color: #000000″&gt; sum; }}</span></pre>

执行结果:

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

Main( (ManualResetEvent m1 = ManualResetEvent( (ManualResetEvent m2 = ManualResetEvent( (ManualResetEvent m3 = ManualResetEvent(=>= result = SumNumbers( ThreadPool.QueueUserWorkItem( s </span>=><span style=”color: #000000″&gt;{ Console.WriteLine(</span><span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;线程ID:{0},Thread.CurrentThread.ManagedThreadId); Stopwatch stw </span>= <span style=”color: #0000ff”&gt;new</span><span style=”color: #000000″&gt; Stopwatch(); stw.Start(); </span><span style=”color: #0000ff”&gt;long</span> result = SumNumbers(<span style=”color: #800080″&gt;10000000</span><span style=”color: #000000″&gt;); stw.Stop(); m2.Set(); Console.WriteLine(</span><span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;线程ID:{0},stw.ElapsedMilliseconds); }); ThreadPool.QueueUserWorkItem( s </span>=><span style=”color: #000000″&gt;{ Console.WriteLine(</span><span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;线程ID:{0},Thread.CurrentThread.ManagedThreadId); Stopwatch stw </span>= <span style=”color: #0000ff”&gt;new</span><span style=”color: #000000″&gt; Stopwatch(); stw.Start(); </span><span style=”color: #0000ff”&gt;long</span> result = SumNumbers(<span style=”color: #800080″&gt;10000000</span><span style=”color: #000000″&gt;); stw.Stop(); m3.Set(); Console.WriteLine(</span><span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;线程ID:{0},stw.ElapsedMilliseconds); }); </span><span style=”color: #008000″&gt;//</span><span style=”color: #008000″&gt;等待线程池的线程执行</span>

<span style=”color: #000000″> m1.WaitOne();
m2.WaitOne();
m3.WaitOne();
Console.WriteLine(<span style=”color: #800000″>”<span style=”color: #800000″>所有线程执行完成<span style=”color: #800000″>”<span style=”color: #000000″>);

} Console.ReadKey(); } </span><span style=”color: #0000ff”&gt;static</span> <span style=”color: #0000ff”&gt;long</span> SumNumbers(<span style=”color: #0000ff”&gt;int</span><span style=”color: #000000″&gt; count) { </span><span style=”color: #0000ff”&gt;long</span> sum = <span style=”color: #800080″&gt;0</span><span style=”color: #000000″&gt;; </span><span style=”color: #0000ff”&gt;for</span> (<span style=”color: #0000ff”&gt;int</span> i = <span style=”color: #800080″&gt;0</span>; i < count; i++<span style=”color: #000000″&gt;) { sum </span>+=<span style=”color: #000000″&gt; i; } Thread.Sleep(</span><span style=”color: #800080″&gt;3000</span><span style=”color: #000000″&gt;); </span><span style=”color: #0000ff”&gt;return</span><span style=”color: #000000″&gt; sum; }}</span></pre>

执行结果: