c#线程实现生产者消费者
2012-05-05 16:03
295 查看
public interface IThreadWorker : IDisposable { void RealWorker(); } public class ThreadController { private Queue<IThreadWorker> WorkQueue = new Queue<IThreadWorker>(); private int mMaxThreadCount = 5; private ManualResetEvent[] mCustomerSyncEvents = null; private ManualResetEvent[] mCustomerFinishEvents = null; public ManualResetEvent ProducterFinishEvent = new ManualResetEvent(false); private string ThreadName = string.Empty; //private bool Started = false; public ThreadController() : this(5, string.Empty) { } public ThreadController(int maxThreadCount) : this(maxThreadCount, string.Empty) { } public ThreadController(int maxThreadCount, string name) { ThreadName = name; mMaxThreadCount = maxThreadCount; mCustomerSyncEvents = new ManualResetEvent[maxThreadCount]; mCustomerFinishEvents = new ManualResetEvent[maxThreadCount]; for (int i = 0; i < maxThreadCount; i++) { mCustomerSyncEvents[i] = new ManualResetEvent(true); mCustomerFinishEvents[i] = new ManualResetEvent(false); } StartRun(); } public void AddJobQueue(IThreadWorker info) { lock (((ICollection)WorkQueue).SyncRoot) { WorkQueue.Enqueue(info); } //if (!Started) //{ // StartRun(); // Started = true; //} } private void StartRun() { for (int i = 0; i < mMaxThreadCount; i++) { CustomerSyncEventController sync = new CustomerSyncEventController(mCustomerSyncEvents[i], mCustomerFinishEvents[i], ProducterFinishEvent); Thread thread = new Thread(new ParameterizedThreadStart(RealWork)); thread.Start(sync); } } public bool Completed() { return WaitHandle.WaitAll(mCustomerFinishEvents); } private void RealWork(object obj) { CustomerSyncEventController sync = obj as CustomerSyncEventController; while (sync.CustomerSyncEvent.WaitOne()) { sync.CustomerSyncEvent.Reset(); try { IThreadWorker instance = null; if (sync.ProducterFinishEvent.WaitOne(10, true)) { lock (((ICollection)WorkQueue).SyncRoot) { if (WorkQueue.Count == 0) { //Console.WriteLine("{0}, set", sync.i); sync.CustomerFinishEvent.Set(); break; } else { instance = WorkQueue.Dequeue(); } } } else { // Console.WriteLine("{0}, false", sync.i); lock (((ICollection)WorkQueue).SyncRoot) { if (WorkQueue.Count == 0) { //break; } else { instance = WorkQueue.Dequeue(); } } } using (instance) { //Console.WriteLine("{0}, {1}", sync.i, info.info); instance.RealWorker(); } } catch (Exception e) { } finally { //Console.WriteLine("{0}, set", sync.i); sync.CustomerSyncEvent.Set(); } } } internal class CustomerSyncEventController { public ManualResetEvent CustomerSyncEvent { get; set; } public ManualResetEvent CustomerFinishEvent { get; set; } public ManualResetEvent ProducterFinishEvent { get; set; } public CustomerSyncEventController() { CustomerSyncEvent = new ManualResetEvent(false); CustomerFinishEvent = new ManualResetEvent(false); ProducterFinishEvent = new ManualResetEvent(true); } public CustomerSyncEventController(ManualResetEvent customerSync, ManualResetEvent customerFinish, ManualResetEvent producterFinish) { CustomerSyncEvent = customerSync; CustomerFinishEvent = customerFinish; ProducterFinishEvent = producterFinish; } } }
public class ImplementClass : IThreadWorker { public string Content { get; set; } public ImplementClass(string content) { Content = content; } #region IThreadWorker Members public void RealWorker() { Console.WriteLine(Content); } #endregion #region IDisposable Members public void Dispose() { } #endregion }
static void Main(string[] args) { List<ImplementClass> list = new List<ImplementClass>(); for (int i = 0; i <= 100; i++) { list.Add(new ImplementClass(i.ToString())); } ThreadController monitor = new ThreadController(32); foreach (ImplementClass ic in list) { monitor.AddJobQueue(ic); } monitor.ProducterFinishEvent.Set(); if (monitor.Completed()) { } }
实现自己的IThreadWorker,
不要忘记调用
monitor.ProducterFinishEvent.Set(),证明队列中不会再增加新的内容
monitor.Completed()保证所有的消费者都完成了工作。
相关文章推荐
- C#中生产者线程和消费者线程同步的实现
- wait和notify实现的生产者消费者线程交互
- 线程通信之生产者消费者间wait()、notify实现
- 生产者消费者问题之Java线程实现
- C#实现“生产者消费者”问题
- linux 线程间使用信号量实现消费者生产者
- 基于线程实现的生产者消费者模型(Object.wait(),Object.notify()方法)
- Ruby:线程实现经典的生产者消费者问题
- C#实现生产者与消费者关系
- 生产者消费者线程在Queue<T>中实现多线程同步
- Ruby中用线程实现经典的生产者消费者问题代码实例
- linux下使用线程实现生产者消费者问题
- java线程:三种方式实现生产者消费者问题_2
- Ruby:线程实现经典的生产者消费者问题
- 11.python并发入门(part8 基于线程队列实现生产者消费者模型)
- 【Linux】线程总结:线程同步 -互斥锁,条件变量,信号量实现多生产者多消费者模型
- java线程:三种方式实现生产者消费者问题_1
- 多线程实现生产者消费者模型,以及线程和进程的回顾
- JAVA synchronized 线程经典 生产者消费者 两个完全不同的实现的方式
- Linux C:利用两个线程实现生产者消费者模型