- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Runtime.CompilerServices;
- namespace Concurrent
- {
- public class ProducerAndConsumer
- {
- BreadContainer container;
- Producer pA;
- Consumer cA;
- Producer pB;
- Consumer cB;
- System.Threading.Thread thPA;
- System.Threading.Thread thCA;
- System.Threading.Thread thPB;
- System.Threading.Thread thCB;
- public void startDemo()
- {
- container = BreadContainer.Instance;
- pA = new Producer("PA", container, 2);
- cA = new Consumer("CA", container, 5);
- pB = new Producer("PB", container, 3);
- cB = new Consumer("CB", container, 10);
- thPA =
- new System.Threading.Thread(new System.Threading.ThreadStart(pA.Produce));
- thCA =
- new System.Threading.Thread(new System.Threading.ThreadStart(cA.Consume));
- thPB =
- new System.Threading.Thread(new System.Threading.ThreadStart(pB.Produce));
- thCB =
- new System.Threading.Thread(new System.Threading.ThreadStart(cB.Consume));
- thPA.Start();
- thCA.Start();
- thPB.Start();
- thCB.Start();
- }
- public string StopDemo()
- {
- thPA.Abort();
- thCA.Abort();
- thPB.Abort();
- thCB.Abort();
- return cA.ConsumedBreads() + "/n" + cB.ConsumedBreads();
- }
- }
- /// <summary>
- /// 面包实体类
- /// </summary>
- public class Bread
- {
- /// <summary>
- /// 面包生产的厂家
- /// </summary>
- private Producer _producer;
- /// <summary>
- /// 面包的标识或批次
- /// </summary>
- private int _id;
- public Producer Producer
- {
- get { return _producer; }
- }
- public int ID
- {
- get { return _id; }
- }
- public Bread(int id, Producer producer)
- {
- _producer = producer;
- _id = id;
- }
- }
- /// <summary>
- /// 装面包的容器类
- /// </summary>
- public class BreadContainer
- {
- /// <summary>
- /// 面包容器
- /// </summary>
- private Bread[] _container;
- private int _index;
- /// <summary>
- /// 容量
- /// </summary>
- private int _capability = 10;
- protected BreadContainer()
- {
- _container = new Bread[_capability];
- _index = -1;
- }
- /// <summary>
- /// note:如果不加MethodImpl,则调用线程的Abort方法时报异常
- /// </summary>
- /// <param name="bread"></param>
- [MethodImpl(MethodImplOptions.Synchronized)]
- public void InBread(Bread bread)
- {
- //lock (_container.SyncRoot)
- //{
- if (_index == _capability)
- {
- System.Threading.Monitor.Wait(this);
- }
- _container[++_index] = bread;
- System.Threading.Monitor.Pulse(this);
- //}
- }
- [MethodImpl(MethodImplOptions.Synchronized)]
- public Bread OutBread()
- {
- //lock (_container.SyncRoot)
- //{
- if (_index < 0)
- {
- System.Threading.Monitor.Wait(this);
- }
- Bread temp = _container[_index--];
- System.Threading.Monitor.Pulse(this);
- return temp;
- //}
- }
- /// <summary>
- /// 容器类的唯一实例
- /// </summary>
- public static BreadContainer Instance
- {
- get
- {
- return Nested.instance;
- }
- }
- class Nested
- {
- static Nested()
- {
- }
- internal static readonly BreadContainer instance = new BreadContainer();
- }
- }
- /// <summary>
- /// 生产厂商
- /// </summary>
- public class Producer
- {
- private string _name;
- private BreadContainer _breadContainer;
- /// <summary>
- /// 生产速度
- /// </summary>
- private int _speed;
- /// <summary>
- /// 生产面包的总数量
- /// </summary>
- private int _breadsCount;
- public string Name
- {
- get { return _name; }
- }
- public Producer(string strName, BreadContainer breadContainer, int speed)
- {
- _name = strName;
- _breadContainer = breadContainer;
- _speed = speed;
- }
- /// <summary>
- /// 开始生产
- /// </summary>
- public void Produce()
- {
- while (true)
- {
- _breadContainer.InBread(new Bread(++_breadsCount, this));
- System.Threading.Thread.Sleep(_speed * 1000);
- }
- }
- }
- /// <summary>
- /// 消费者类
- /// </summary>
- public class Consumer
- {
- private string _name;
- private List<Bread> _consumptionMemo;
- private BreadContainer _breadContainer;
- /// <summary>
- /// 消费速度
- /// </summary>
- private int _speed;
- /// <summary>
- /// 消费者姓名
- /// </summary>
- public string Name
- {
- get { return _name; }
- }
- public Consumer(string strName, BreadContainer breadContainer,int speed)
- {
- _name = strName;
- _consumptionMemo = new List<Bread>();
- _breadContainer = breadContainer;
- _speed = speed;
- }
- public string ConsumedBreads()
- {
- string str = Name+"消费的清单如下:/n";
- foreach (Bread temp in _consumptionMemo)
- {
- str += "厂商:"+temp.Producer.Name + ":面包id:" + temp.ID+"/n";
- }
- return str;
- }
- public void Consume()
- {
- while (true)
- {
- _consumptionMemo.Add(_breadContainer.OutBread());
- System.Threading.Thread.Sleep(_speed * 1000);
- }
- }
- }
- }
- 其它文章:http://blog.csdn.net/program_think/archive/2009/03/25/4022087.aspx
.net实现--多线程之生产者/消费者问题
最新推荐文章于 2022-10-22 20:23:32 发布