一個簡易的、輕量級的方法並行執行線程輔助類


  在實際應用中,經常要讓多個方法並行執行以節約運行時間,線程就是必不可少的了,而多線程的管理經常又是一件頭疼的事情,比如方法並行執行異步的返回問題,方法並行執行的超時問題等等,因此這里分享一個簡易的、輕量級的方法並行執行線程輔助類。

  線程管理輔助類的兩個目標:

  1、多個線程方法並行執行,主線程等待,需要知道所有子線程執行完畢;

  2、異步執行方法需要設置超時時間,超時可以跳過該方法,主線程直接返回;

  3、輕量級,雖然微軟提供了線程等待、超時等可用組件,如ManualResetEvent,但那是內核對象,占用系統資源較多。

  設計實現:

  1、該類內部維護兩個變量,異步執行方法總線程數totalThreadCount,當前執行完畢線程數據currThreadCount;

  2、兩個開放方法,WaitAll等待執行,SetOne設置方法執行結束,每一個方法執行完畢調用SetOne,currThreadCount數量加1,同時WaitAll判斷currThreadCount與totalThreadCount是否相等,相等即表示所有方法執行完畢,返回;

  3、為了實現線程安全,currThreadCount的變量處理使用Interlocked。

  代碼實現:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading;
 6 
 7 namespace Loncin.CodeGroup10.Utility
 8 {
 9     public class ThreadHelper
10     {
11         /// <summary>
12         /// 總線程數
13         /// </summary>
14         private int totalThreadCount;
15 
16         /// <summary>
17         /// 當前執行完畢線程數
18         /// </summary>
19         private int currThreadCount;
20 
21         /// <summary>
22         /// 構造函數
23         /// </summary>
24         /// <param name="totalThreadCount">總線程數</param>
25         public ThreadHelper(int totalThreadCount)
26         {
27             Interlocked.Exchange(ref this.totalThreadCount, totalThreadCount);
28             Interlocked.Exchange(ref this.currThreadCount, 0);
29         }
30 
31         /// <summary>
32         /// 等待所有線程執行完畢
33         /// </summary>
34         /// <param name="overMiniseconds">超時時間(毫秒)</param>
35         public void WaitAll(int overMiniseconds = 0)
36         {
37             int checkCount = 0;
38 
39             // 自旋
40             while (Interlocked.CompareExchange(ref this.currThreadCount, 0, this.totalThreadCount) != this.totalThreadCount)
41             {
42                 // 超過超時時間返回
43                 if (overMiniseconds > 0)
44                 {
45                     if (checkCount >= overMiniseconds)
46                     {
47                         break;
48                     }
49                 }
50 
51                 checkCount++;
52 
53                 Thread.Sleep(1);
54             }
55         }
56 
57         /// <summary>
58         /// 設置信號量,表示單線程執行完畢
59         /// </summary>
60         public void SetOne()
61         {
62             Interlocked.Increment(ref this.currThreadCount);
63         }
64     }
65 }
View Code

  使用示例:

 1     public class ThreadHelperTest
 2     {
 3         /// <summary>
 4         /// 線程幫助類
 5         /// </summary>
 6         private ThreadHelper threadHelper;
 7 
 8         public void Test()
 9         {
10             // 開啟方法方法並行執行
11             int funcCount = 5;
12 
13             this.threadHelper = new ThreadHelper(funcCount);
14 
15             for (int i = 0; i < funcCount; i++)
16             {
17                 Action<int> action = new Action<int>(TestFunc);
18                 action.BeginInvoke(i, null, null);
19             }
20 
21             // 等待方法執行,超時時間12ms,12ms后強制結束
22             threadHelper.WaitAll(12);
23 
24             Console.WriteLine("所有方法執行完畢!");
25         }
26 
27         private void TestFunc(int i)
28         {
29             try
30             {
31                 Console.WriteLine("方法{0}執行!");
32                 Thread.Sleep(10);
33             }
34             finally
35             {
36                 // 方法執行結束
37                 this.threadHelper.SetOne();
38             }
39         }
40     }
View Code

  總結:

  1、該線程幫助類只是一個簡易的線程管理類,還缺少很多功能,比如異常處理等,不過一般的情況下還是比較使用的。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM