zoukankan      html  css  js  c++  java
  • C# 多线程任务分配辅助类

    1)首先实现一个多线程的辅助类,代码如下:

     1 public class ThreadMulti
     2   {
     3       public delegate void DelegateComplete();
     4       public delegate void DelegateWork(int taskindex,int threadindex);
     5  
     6       public DelegateComplete CompleteEvent;
     7       public DelegateWork WorkMethod;
     8  
     9  
    10       private ManualResetEvent[] _resets;
    11       private int _taskCount = 0;
    12       private int _threadCount = 5;
    13  
    14       public ThreadMulti(int taskcount)
    15       {
    16           _taskCount = taskcount;
    17       }
    18  
    19       public ThreadMulti(int taskcount, int threadCount)
    20       {
    21           _taskCount = taskcount;
    22           _threadCount = threadCount;
    23       }
    24  
    25       public void Start()
    26       {
    27           if (_taskCount < _threadCount)
    28           {
    29               //任务数小于线程数的
    30               _resets = new ManualResetEvent[_taskCount];
    31               for (int j = 0; j < _taskCount; j++)
    32               {
    33                   _resets[j] = new ManualResetEvent(false);
    34                   ThreadPool.QueueUserWorkItem(new WaitCallback(Work), new object[] { j, j });
    35               }
    36           }
    37           else
    38           {
    39               _resets = new ManualResetEvent[_threadCount];
    40               //任务数大于线程数 先把线程数的任务启动
    41               for (int i = 0; i < _threadCount; i++)
    42               {
    43                   _resets[i] = new ManualResetEvent(false);
    44                   ThreadPool.QueueUserWorkItem(new WaitCallback(Work), new object[] { i, i });
    45               }
    46               //完成一个任务后在利用完成的那个线程执行下一个任务
    47               int receivereset = 0;
    48               receivereset = ManualResetEvent.WaitAny(_resets);
    49               for (int l = _threadCount; l < _taskCount; l++)
    50               {
    51                   _resets[receivereset].Reset();
    52                   ThreadPool.QueueUserWorkItem(new WaitCallback(Work), new object[] { l, receivereset });
    53                   receivereset = ManualResetEvent.WaitAny(_resets);
    54               }
    55           }
    56  
    57           ManualResetEvent.WaitAll(_resets);
    58           if (CompleteEvent != null)
    59           {
    60               CompleteEvent();
    61           }
    62       }
    63  
    64       public void Work(object arg)
    65       {
    66           int taskindex = int.Parse(((object[])arg)[0].ToString());
    67           int resetindex = int.Parse(((object[])arg)[1].ToString());
    68           if (WorkMethod != null)
    69           {
    70               WorkMethod(taskindex + 1, resetindex+1);
    71           }
    72           _resets[resetindex].Set();
    73       }
    74   }
    View Code
      ThreadMulti类能够根据根据传入的任务数和线程数,实现多线程的重复利用并执行指定的WorkMethod.
      并且在任务完成后能触发CompleteEvent事件.
      类中关键的是ManualResetEvent类的应用,能够在一个任务完成时通知程序哪个线程执行完毕,然后就安排另一个任务开始执行.
     
     
     
    2)辅助类的应用
               //实例化多线程辅助类并启动
         ThreadMulti thread = new ThreadMulti(workcount);
     
         thread.WorkMethod = new ThreadMulti.DelegateWork(DoWork);//执行任务的函数
         thread.CompleteEvent = new ThreadMulti.DelegateComplete(WorkComplete);//所有任务执行完毕的事件
         thread.Start();

    只需要指定任务数和执行任务的函数和完成任务后的事件即可.调用Start方法后,DoWork会在线程中调用并传递任务的index和执行线程的index.

    public void DoWork(int index,int threadindex)
          {
          }
    
       
  • 相关阅读:
    状态压缩 + 暴力 HDOJ 4770 Lights Against Dudely
    简单几何(推公式) UVA 11646 Athletics Track
    简单几何(四边形形状) UVA 11800 Determine the Shape
    简单几何(求交点) UVA 11437 Triangle Fun
    计算几何模板
    简单几何(相对运动距离最值) UVA 11796 Dog Distance
    简单几何(求划分区域) LA 3263 That Nice Euler Circuit
    覆盖的面积 HDU
    Desert King 最小比率生成树 (好题)
    约会安排 (区间合并)毒瘤题
  • 原文地址:https://www.cnblogs.com/qc-id-01/p/7943666.html
Copyright © 2011-2022 走看看