c#实现生产者消费者

做操作系统课设的时候使用的基本代码。

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Collections;
  5. using System.Threading;
  6. namespace ConsoleApplication4
  7. {
  8.     class Program
  9.     {
  10.         ArrayList container new ArrayList();
  11.         Producer producer null;
  12.         Consumer consumer null;
  13.         static void Main(string[] args)
  14.         {
  15.             Program new Program();
  16.             Thread t1 new Thread(new ThreadStart(p.ThreadProduct));
  17.             Thread t2 new Thread(new ThreadStart(p.ThreadConsumption));
  18.             t1.Start();
  19.             t2.Start();
  20.             Console.Read();
  21.         }
  22.         public void ThreadProduct()
  23.         {
  24.             producer new Producer(this.container);
  25.             lock (this)
  26.             {
  27.                 for (int 1; <= 8; i++)
  28.                 {
  29.                     if (this.container.Count == 0)
  30.                     {
  31.                         producer.Product(i "");
  32.                         Monitor.Pulse(this);
  33.                     }
  34.                     Monitor.Wait(this);
  35.                 }
  36.             }
  37.         }
  38.         public void ThreadConsumption()
  39.         {
  40.             consumer new Consumer(this.container);
  41.             lock (this)
  42.             {
  43.                 while (true)
  44.                 {
  45.                     if (this.container.Count != 0)
  46.                     {
  47.                         consumer.Consumption();
  48.                         Monitor.Pulse(this);
  49.                     }
  50.                     Monitor.Wait(this);
  51.                 }
  52.             }
  53.         }
  54.     }
  55.     public class Consumer
  56.     {
  57.         ArrayList container null;
  58.         public Consumer(ArrayList container)
  59.         {
  60.             this.container container;
  61.         }
  62.         public void Consumption()
  63.         {
  64.             Goods goods (Goods)this.container[0];
  65.             Console.WriteLine("消费了物品:" goods.ToString());
  66.             this.container.RemoveAt(0);
  67.         }
  68.     }
  69.     public class Producer
  70.     {
  71.         ArrayList container null;
  72.         public Producer(ArrayList container)
  73.         {
  74.             this.container container;
  75.         }
  76.         public void Product(string name)
  77.         {
  78.             Goods goods new Goods();
  79.             goods.Name name;
  80.             this.container.Add(goods);
  81.             Console.WriteLine("生产了物品:" goods.ToString());
  82.         }
  83.     }
  84.     public class Goods
  85.     {
  86.         private string name;
  87.         public string Name
  88.         {
  89.             get return name; }
  90.             set name value}
  91.         }
  92.         public override string ToString()
  93.         {
  94.             return "物品名称:" name;
  95.         }
  96.     }
  97. }

 


版权声明:本文为wk360833257原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。