线程池处理中的一个问题

前段时间有任务要用到线程池来解决一个问题,大概描述如下:
要对很多个订单(百万级别)进行处理,处理完毕后需要告诉客户端处理完毕并告之是否全部成功.
其实比较麻烦的就在第二点,试图用WaitHandle.WaitAll()却发现只支持64个Events.
小小地改造了一下这个函数,能同时支持无数个.

ExpandedBlockStart.gif代码
    public static void WaitAll(WaitHandle[] waitHandles)
    {
        
if (waitHandles == null)
            
throw new ArgumentNullException("handles");
        
foreach (WaitHandle wh in waitHandles)
        {
            
if (!wh.WaitOne(_TimeOut, false))
            {
                
throw new TimeoutException();
            }
        }
    }

 

 

此后的任务就很easy了……

以下是Send.cs主要代码

ExpandedBlockStart.gif代码
 1 ThreadPool.SetMaxThreads(_MaxThread, _MaxThread);
 2 _totalCount = sendOrders.Rows.Count;
 3 SendThread[] SendThreadArray = new SendThread[_totalCount];
 4 ManualResetEvent[] doneEvents = new ManualResetEvent[_totalCount];
 5 DateTime begin = DateTime.Now;
 6 for (int i = 0; i < sendOrders.Rows.Count; i++)
 7 {
 8    doneEvents[i] = new ManualResetEvent(false);
 9    SendThread _sendThread = new SendThread(sendOrders.Rows[i], doneEvents[i]);
10    ThreadPool.QueueUserWorkItem(_sendThread.sendThreadCallBack, i);
11    SendThreadArray[i] = _sendThread;
12 }
13 
14 try
15 {
16    //等待所有线程结束
17    WaitAll(doneEvents);
18 }…………

  以下是SendThread.cs主要代码

ExpandedBlockStart.gif代码
public class SendThread
{
    DataRow _orderInfo;
    
private ManualResetEvent _doneEvent;
    
public SendThread(DataRow OrderInfo, ManualResetEvent DoneEvent)
    {
        _orderInfo 
= OrderInfo;
        _doneEvent 
= DoneEvent;
    }
    
public void sendThreadCallBack(object threadContext)
    {
        
// do sth.
        _doneEvent.Set();
    }
}

 

 enjoy~

转载于:https://www.cnblogs.com/uanguei/archive/2009/12/31/1636937.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值