线程,线程数控制!

多线程与线程池的介绍看这里
http://edobnet.cnblogs.com/archive/2005/11/29/287094.html

None.gif          // private int threadNum = 0;
None.gif        
// private AutoResetEvent myResetEvent = new AutoResetEvent(false);
ExpandedBlockStart.gifContractedBlock.gif
         /**/ /// <summary>
InBlock.gif        
/// 线程记数器
ExpandedBlockEnd.gif        
/// </summary>

None.gif          private   int  number  =   0 ;
None.gif
ExpandedBlockStart.gifContractedBlock.gif        
/**/ /// <summary>
InBlock.gif        
/// 线程池执行
ExpandedBlockEnd.gif        
/// </summary>

None.gif          public   void  Execute()
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
int maxCount = int.Parse(ConfigurationSettings.AppSettings["Count"]);
InBlock.gif            
int time = int.Parse(ConfigurationSettings.AppSettings["Time"]);
InBlock.gif            
foreach (TG_EvaluteBaseDatasSet service in this)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
InBlock.gif                
InBlock.gif                
//                System.Threading.ThreadStart ts = new System.Threading.ThreadStart(service.GetXMLResult);
InBlock.gif                
//                System.Threading.Thread th = new System.Threading.Thread(ts);
InBlock.gif                
//                th.Start();
InBlock.gif
                WaitCallback async = new WaitCallback(FExeute);
InBlock.gif
InBlock.gif                
while(true)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
InBlock.gif                    Monitor.Enter(number);
InBlock.gif                    
if(number>maxCount)
InBlock.gif                        Thread.Sleep(time);
InBlock.gif                    
else
InBlock.gif                        
break;
InBlock.gif                    Monitor.Exit(number);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
if(ThreadPool.QueueUserWorkItem(async,service))
InBlock.gif                    Interlocked.Increment(
ref number);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
while(true)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//myResetEvent.Set();
InBlock.gif
                Monitor.Enter(number);
InBlock.gif                
if( number <= 0)
InBlock.gif                    
break;
InBlock.gif                
else
InBlock.gif                    Thread.Sleep(time);
InBlock.gif                Monitor.Exit(number);
ExpandedSubBlockEnd.gif            }

InBlock.gif            OnTgFinishReport(
new EventArgs());
InBlock.gif
ExpandedBlockEnd.gif        }

ExpandedBlockStart.gifContractedBlock.gif        
/**/ /// <summary>
InBlock.gif        
/// 线程执行一个计算
InBlock.gif        
/// </summary>
ExpandedBlockEnd.gif        
/// <param name="Param">执行对象<see cref="GleNet.HG.HTResolver.TG_EvaluteBaseDatasSet">TG_EvaluteBaseDatasSet</see></param>

None.gif          public    void  FExeute( object  Param)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
//myResetEvent.WaitOne();
InBlock.gif
            using(TG_EvaluteBaseDatasSet service = (TG_EvaluteBaseDatasSet)Param)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                service.doResult();
InBlock.gif                writeFile(Path,service)    ;
InBlock.gif                OnTgReport(
new ReportEvent(service.TE_PASS_RANGE,service.TE_YEAR,service.TE_MONTH,int.Parse(service.TE_I_E_FLAG),int.Parse(service.TE_ENTRY_TYPE),number));
ExpandedSubBlockEnd.gif            }

InBlock.gif            Interlocked.Decrement(
ref number);
InBlock.gif            Console.WriteLine(
"LEFT:"+number);
InBlock.gif            
ExpandedBlockEnd.gif        }

由程序控制线程数量,并且, 由MONITOR进行保护!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值