代码改变世界

一个线程管理器

2007-11-21 22:24  buru  阅读(753)  评论(3)    收藏  举报

 

using System;
using System.Text;
using System.Threading;
using System.Collections;

namespace GenThreadPool
{
    
public interface IThreadPool
    
{
        
void AddJob(System.Threading.Thread jobToRun);
        Stats GetStats();
          
        
    }


    
public class GenPool
    
{
        
private Object _lock;
        
private GenThreadPoolImpl _gn;
        
public GenPool(Object lock_,GenThreadPoolImpl gn)
        
{
         
this._lock=lock_;
         
this._gn = gn;
        }

        
public void Run()
        
{
            Thread job 
= null;
            
try
            
{
                
while (true)
                
{
                    
while (true)
                    
{
                        
lock (this._lock)
                        
{
                            
if (_gn.PendingJobs.Count == 0)
                            
{
                                
int index = _gn.FindThread();
                                
if (index == -1return;
                                ((ThreadElement)_gn.AvailableThreads[index]).Idle 
= true;
                                
break;
                            }

                            job 
= (Thread)_gn.PendingJobs[0];
                            _gn.PendingJobs.RemoveAt(
0);
                        }
//end lock
                        job.Start();
                    }

                    
try
                    
{
                        
lock (this)
                        
{
                            
if (_gn.MaxIdleTime == -1)
                                Monitor.Wait(
this);
                            
else Monitor.Wait(this, _gn.MaxIdleTime);
                        }

                    }

                    
catch { }
                    
lock (_lock)
                    
{
                        
if (_gn.PendingJobs.Count == 0)
                        
{
                            
if (_gn.MinThreads != -1 && _gn.AvailableThreads.Count > _gn.MinThreads)
                            
{
                                _gn.RemoveThread();
                                
return;
                            }

                        }

                    }

                }

            }

            
catch { }
        }

    }

   
    
public class GenThreadPoolImpl : IThreadPool
    
{
        
public GenThreadPoolImpl()
        
{
            _maxThreads 
= 1;
            _minThreads 
= 0;
            _maxIdleTime 
= 300;
            
this._pendingJobs = ArrayList.Synchronized(new ArrayList());
            
this._availableThreads = ArrayList.Synchronized(new ArrayList());
            _debug 
= false;
        }

        
public GenThreadPoolImpl(int maxThreads,int minThreads,int maxIdleTime)
        
{
            _maxThreads 
= maxThreads;
            _minThreads 
= minThreads;
            _maxIdleTime 
= maxIdleTime;
            
this._pendingJobs = ArrayList.Synchronized(new ArrayList());
            
this._availableThreads = ArrayList.Synchronized(new ArrayList());
            _debug 
= false;
            InitAvailableThreads();
        
        }


        
private void InitAvailableThreads()
        
{
            
if (this._minThreads > 0)
            
{
                
for (int i = 0; i < this._minThreads; i++)
                

                  Thread t
=new Thread(new ThreadStart(new GenPool(this,this).Run));
                  ThreadElement e 
= new ThreadElement(t);
                  e.Idle 
= true;
                  _availableThreads.Add(e);
                }

                Console.WriteLine(
"Initialized the ThreadPool," + "Number of Available threads:" + this._availableThreads.Count);
            }

        }

        
private int _maxThreads;
        
private int _minThreads;

        
public int MinThreads1
        
{
            
get return _minThreads; }
            
set { _minThreads = value; }
        }


        
public int MinThreads
        
{
            
get return _minThreads; }
            
set { _minThreads = value; }
        }

        
private int _maxIdleTime;

        
public int MaxIdleTime
        
{
            
get return _maxIdleTime; }
            
set { _maxIdleTime = value; }
        }

        
private static bool _debug;

        
public static bool Debug
        
{
            
get return GenThreadPoolImpl._debug; }
            
set { GenThreadPoolImpl._debug = value; }
        }

        
private ArrayList _pendingJobs;

        
public ArrayList PendingJobs
        
{
            
get return _pendingJobs; }
            
set { _pendingJobs = value; }
        }

        
private ArrayList _availableThreads;

        
public ArrayList AvailableThreads
        
{
            
get return _availableThreads; }
            
set { _availableThreads = value; }
        }


        
IThreadPool 成员



         
public  int FindThread()
        
{
            
for (int i = 0; i < _availableThreads.Count; i++)
            
{
                
if (((ThreadElement)_availableThreads[i]).GetMyThread().Equals(Thread.CurrentThread))
                
{
                    
return i;
                }

            }

            
return -1;
        }

         
public void RemoveThread()
         
{
             
for (int i = 0; i < _availableThreads.Count; i++)
             
{
                 
if (((ThreadElement)_availableThreads[i]).GetMyThread().Equals(Thread.CurrentThread))
                 
{
                     _availableThreads.RemoveAt(i);
                     
return;
                 }

             }
   
          }

    }

       
    
public struct Stats
    
{
        
public int maxThreads;
        
public int minThreads;
        
public int maxIdleTime;
        
public int numThreads;
        
public int pendingJobs;
        
public int jobsInProgress;

        
public override string ToString()
        
{
            StringBuilder sb 
= new StringBuilder("MaxThread=");
            sb.Append(maxThreads);
            sb.Append(
"\nMinThreads=");
            sb.Append(minThreads);
            sb.Append(
"\nMaxThreads=");
            sb.Append(maxThreads);
            sb.Append(
"\npendingJobs=");
            sb.Append(pendingJobs);
            sb.Append(
"\nJobs In Progress=");
            sb.Append(jobsInProgress);

            
return sb.ToString();
                        
        }

    }

     
     
public class ThreadElement
     
{
         
private bool _idle;

         
public bool Idle
         
{
             
get return _idle; }
             
set { _idle = value; }
         }


        
         
private Thread _thread;
         
public ThreadElement(Thread th)
         
{
            _thread
=th;
             
this._idle=true;
         }

         
public Thread GetMyThread()
         
{
             
return this._thread;
         }

     }

}