public class SimpleThreadPoolThread { PRivate object locker = new object(); private AutoResetEvent are = new AutoResetEvent(false); private WorkItem wi; private Thread t; private bool b = true; private bool isWorking;
public bool IsWorking { get { lock (locker) { return isWorking; } } } public event Action<SimpleThreadPoolThread> WorkComplete;
public SimpleThreadPoolThread() { lock (locker) { // 当前没有实际任务 isWorking = false; } t = new Thread(Work) { IsBackground = true }; t.Start(); }
public void SetWork(WorkItem wi) { this.wi = wi; }
public void StartWork() { // 发出信号 are.Set(); }
public void StopWork() { // 空任务 wi = null; // 停止线程循环 b = false; // 发出信号结束线程 are.Set(); }
public class SimpleThreadPool : IDisposable { private object locker = new object(); private bool b = true; private int minThreads; private int maxThreads; private int currentActiveThreadCount; private List<SimpleThreadPoolThread> simpleThreadPoolThreadList = new List<SimpleThreadPoolThread>(); private Queue<WorkItem> workItemQueue = new Queue<WorkItem>();
public int CurrentActiveThreadCount { get { lock (locker) { return currentActiveThreadCount; } }
}
public int CurrentThreadCount { get { lock (locker) { return simpleThreadPoolThreadList.Count; } } }
public int CurrentQueuedWorkCount { get { lock (locker) { return workItemQueue.Count; } } }