zoukankan      html  css  js  c++  java
  • 一个简易的、轻量级的方法并行执行线程辅助类

      在实际应用中,经常要让多个方法并行执行以节约运行时间,线程就是必不可少的了,而多线程的管理经常又是一件头疼的事情,比如方法并行执行异步的返回问题,方法并行执行的超时问题等等,因此这里分享一个简易的、轻量级的方法并行执行线程辅助类。

      线程管理辅助类的两个目标:

      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、该线程帮助类只是一个简易的线程管理类,还缺少很多功能,比如异常处理等,不过一般的情况下还是比较使用的。

  • 相关阅读:
    性能测试(硬件性能指标汇总)
    jmeter中通过beanshell访问eclipse中导出jar中的java类的方法
    Linux性能优化参考
    单机到分布式集群简介
    java代码(生成日历时间)
    explian使用介绍
    java代码(处理json串)
    java代码(ascii与字母互转)
    jmeter分布式测试配置
    性能测试整体解决方案技术架构图、模型体系图、LR性能测试流程图
  • 原文地址:https://www.cnblogs.com/loncin/p/4223889.html
Copyright © 2011-2022 走看看