C# · 12月 20, 2021

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

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

1. 使用 Task 执行多线程任务

Main(= Task(() =>= result = SumNumbers( Task task2 </span>= <span style=”color: #0000ff”&gt;new</span> Task(() =><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;20000000</span><span style=”color: #000000″&gt;); stw.Stop(); Console.WriteLine(</span><span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;线程ID:{0},stw.ElapsedMilliseconds); }); Task task3 </span>= <span style=”color: #0000ff”&gt;new</span> Task(() =><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;15000000</span><span style=”color: #000000″&gt;); stw.Stop(); Console.WriteLine(</span><span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;线程ID:{0},stw.ElapsedMilliseconds); }); task1.Start(); task2.Start(); task3.Start(); 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>

执行结果:

2. 等待Task多线程任务执行完成

Main(= Task(() => List</span><Task> listTask = <span style=”color: #0000ff”&gt;new</span> List<Task><span style=”color: #000000″&gt;(); listTask.Add(task1); listTask.Add(task2); listTask.Add(task3); task1.Start(); task2.Start(); task3.Start(); Task.WaitAll(listTask.ToArray()); Console.WriteLine(</span><span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;所有线程执行完成。</span><span style=”color: #800000″&gt;”</span><span style=”color: #000000″&gt;); 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>

执行结果: