.net实现--多线程之生产者/消费者问题

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Runtime.CompilerServices;
  5. namespace  Concurrent
  6. {
  7.     public class ProducerAndConsumer
  8.     {
  9.         BreadContainer container;
  10.         Producer pA;
  11.         Consumer cA;
  12.         Producer pB;
  13.         Consumer cB;
  14.         System.Threading.Thread thPA;
  15.         System.Threading.Thread thCA;
  16.         System.Threading.Thread thPB;
  17.         System.Threading.Thread thCB;
  18.         public void startDemo()
  19.         {
  20.             container = BreadContainer.Instance;
  21.             pA = new Producer("PA", container, 2);
  22.             cA = new Consumer("CA", container, 5);
  23.             pB = new Producer("PB", container, 3);
  24.             cB = new Consumer("CB", container, 10);
  25.             thPA =
  26.                 new System.Threading.Thread(new System.Threading.ThreadStart(pA.Produce));
  27.             thCA =
  28.                 new System.Threading.Thread(new System.Threading.ThreadStart(cA.Consume));
  29.             thPB =
  30.                 new System.Threading.Thread(new System.Threading.ThreadStart(pB.Produce));
  31.             thCB =
  32.                 new System.Threading.Thread(new System.Threading.ThreadStart(cB.Consume));
  33.             thPA.Start();
  34.             thCA.Start();
  35.             thPB.Start();
  36.             thCB.Start();
  37.         }
  38.         public string StopDemo()
  39.         {
  40.             thPA.Abort();
  41.             thCA.Abort();
  42.             thPB.Abort();
  43.             thCB.Abort();
  44.             return cA.ConsumedBreads() + "/n" + cB.ConsumedBreads();
  45.         }
  46.     }
  47.     /// <summary>
  48.     /// 面包实体类
  49.     /// </summary>
  50.     public class Bread
  51.     {
  52.         /// <summary>
  53.         /// 面包生产的厂家
  54.         /// </summary>
  55.         private Producer _producer;
  56.         /// <summary>
  57.         /// 面包的标识或批次
  58.         /// </summary>
  59.         private int _id;
  60.         public Producer Producer
  61.         {
  62.             get { return _producer; }
  63.         }
  64.         public int ID
  65.         {
  66.             get { return _id; }
  67.         }
  68.         public Bread(int id, Producer producer)
  69.         {
  70.             _producer = producer;
  71.             _id = id;
  72.         }
  73.     }
  74.     /// <summary>
  75.     /// 装面包的容器类
  76.     /// </summary>
  77.     public class BreadContainer
  78.     {
  79.         /// <summary>
  80.         /// 面包容器
  81.         /// </summary>
  82.         private Bread[] _container;
  83.         private int _index;
  84.         /// <summary>
  85.         /// 容量
  86.         /// </summary>
  87.         private int _capability = 10;
  88.         protected BreadContainer()
  89.         {
  90.             _container = new Bread[_capability];
  91.             _index = -1;
  92.             
  93.         }
  94.         /// <summary>
  95.         /// note:如果不加MethodImpl,则调用线程的Abort方法时报异常
  96.         /// </summary>
  97.         /// <param name="bread"></param>
  98.         [MethodImpl(MethodImplOptions.Synchronized)]
  99.         public void InBread(Bread bread)
  100.         {
  101.             //lock (_container.SyncRoot)
  102.             //{
  103.                 if (_index == _capability)
  104.                 {
  105.                     System.Threading.Monitor.Wait(this);
  106.                 }
  107.                 _container[++_index] = bread;
  108.                 System.Threading.Monitor.Pulse(this);
  109.             //}
  110.         }
  111.         [MethodImpl(MethodImplOptions.Synchronized)]
  112.         public Bread OutBread()
  113.         {
  114.             //lock (_container.SyncRoot)
  115.             //{
  116.                 if (_index < 0)
  117.                 {
  118.                     System.Threading.Monitor.Wait(this);
  119.                 }
  120.                 Bread temp = _container[_index--];
  121.                 System.Threading.Monitor.Pulse(this);
  122.                 return temp;
  123.             //}
  124.         }
  125.         /// <summary>
  126.         /// 容器类的唯一实例
  127.         /// </summary>
  128.         public static BreadContainer Instance
  129.         {
  130.             get
  131.             {
  132.                 return Nested.instance;
  133.             }
  134.         }
  135.         class Nested
  136.         {
  137.             static Nested()
  138.             {
  139.             }
  140.             internal static readonly BreadContainer instance = new BreadContainer();
  141.         }
  142.     }
  143.     /// <summary>
  144.     /// 生产厂商
  145.     /// </summary>
  146.     public class Producer
  147.     {
  148.         private string _name;
  149.         private BreadContainer _breadContainer;
  150.         /// <summary>
  151.         /// 生产速度
  152.         /// </summary>
  153.         private int _speed;
  154.         /// <summary>
  155.         /// 生产面包的总数量
  156.         /// </summary>
  157.         private int _breadsCount;
  158.         public string Name
  159.         {
  160.             get { return _name; }
  161.         }
  162.         public Producer(string strName, BreadContainer breadContainer, int speed)
  163.         {
  164.             _name = strName;
  165.             _breadContainer = breadContainer;
  166.             _speed = speed;
  167.         }
  168.         /// <summary>
  169.         /// 开始生产
  170.         /// </summary>
  171.         public void Produce()
  172.         {
  173.             while (true)
  174.             {
  175.                 _breadContainer.InBread(new Bread(++_breadsCount, this));
  176.                 System.Threading.Thread.Sleep(_speed * 1000);
  177.             }
  178.         }      
  179.     }
  180.     /// <summary>
  181.     /// 消费者类
  182.     /// </summary>
  183.     public class Consumer
  184.     {
  185.         private string _name;
  186.         private List<Bread> _consumptionMemo;
  187.         private BreadContainer _breadContainer;
  188.         /// <summary>
  189.         /// 消费速度
  190.         /// </summary>
  191.         private int _speed;
  192.         /// <summary>
  193.         /// 消费者姓名
  194.         /// </summary>
  195.         public string Name
  196.         {
  197.             get { return _name; }
  198.         }
  199.         public Consumer(string strName, BreadContainer breadContainer,int speed)
  200.         {
  201.             _name = strName;
  202.             _consumptionMemo = new List<Bread>();
  203.             _breadContainer = breadContainer;
  204.             _speed = speed;
  205.          
  206.         }
  207.         public string ConsumedBreads()
  208.         {
  209.             string str = Name+"消费的清单如下:/n";
  210.             foreach (Bread temp in _consumptionMemo)
  211.             {
  212.                 str += "厂商:"+temp.Producer.Name + ":面包id:" + temp.ID+"/n";
  213.             }
  214.             return str;
  215.         }
  216.         public void Consume()
  217.         {
  218.             while (true)
  219.             {
  220.                 _consumptionMemo.Add(_breadContainer.OutBread());
  221.                 System.Threading.Thread.Sleep(_speed * 1000);
  222.             }
  223.         }
  224.     }
  225. }
  226. 其它文章:http://blog.csdn.net/program_think/archive/2009/03/25/4022087.aspx
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值