您的位置:首页 > 编程语言 > C#

[C#]一个简易的、轻量级的方法并行执行线程辅助类

2017-04-20 10:41 537 查看
一个简易的、轻量级的方法并行执行线程辅助类   在实际应用中,经常要让多个方法并行执行以节约运行时间,线程就是必不可少的了,而多线程的管理经常又是一件头疼的事情,比如方法并行执行异步的返回问题,方法并行执行的超时问题等等,因此这里分享一个简易的、轻量级的方法并行执行线程辅助类。
  线程管理辅助类的两个目标:
  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 }


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     }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐