生产者消费者模式,BlockingCollection

生产者消费者模式可以让生产者和消费者互不干扰,且有个缓存区解。

生产者把数据放入缓冲区,而消费者从缓冲区取出数据,不必生产一个才能消费一个,消费完一个才能生产下一个。 

 class Program
    {

        static void Main(string[] args)
        {
            Task.Factory.StartNew(() => {
                  new Productor().Product();

            });//生产者线程
            Task.Factory.StartNew(() =>
            {
                new Customer().Consume();

            });//消费者线程
            Console.ReadKey();
        }
    }

    static class Container
    {  
        public static Queue queue = new Queue();
        static readonly int totalCount = 10;
        static  int currentCount = 0;
        static object obj = new object();
        public static void Push(string s)
        {
            lock (obj)
            {
                if (currentCount == totalCount)
                {
                    Monitor.Wait(obj);//仓库满了,生产者等待
                }
                currentCount++;
                queue.Enqueue(s);
                Console.WriteLine("入库,入库后库中有" + currentCount + "个产品");
                Monitor.Pulse(obj);
            }
        }

        public static string Pop()
        {
            lock (obj)
            {
                if (currentCount == 0)
                {
                    Monitor.Wait(obj);//仓库为空,消费者等待
                }
                currentCount--;
                string str= queue.Dequeue();
                Console.WriteLine("出库,出库后库中有" + currentCount + "个产品");
                Monitor.Pulse(obj);
                return str;

            }
        }
    }

    class Productor
    {
        public void Product()
        {
            for (int i = 1; i <= 50; i++)
            {
                Thread.Sleep(500);
                Console.WriteLine("生产了第" + i + "个物品");
                Container.Push("第" + i + "个物品");

            }
        }
    }

    class Customer
    {
        Random random = new Random();
        public void Consume()
        {
            while (true)
            {
                Console.WriteLine("消费了" + Container.Pop());
                Thread.Sleep(random.Next(500, 2000));
            }
        }
    }

用BlockingCollection简单实现就不需要手写仓库了

  class Program
    {
        static void Main(string[] args)
        {
            BlockingCollection bc = new BlockingCollection(10);
            Task.Factory.StartNew(() =>
            {
                new Productor().Product(bc);

            });
            Task.Factory.StartNew(() =>
            {
                new Customer().Consume(bc);

            });
            Console.ReadKey();
        }
    }
    class Productor
    {   
        public void Product(BlockingCollection bc)
        {
            for (int i = 1; i <= 50; i++)
            {
                Thread.Sleep(500);
                bc.Add("第" + i + "个物品");
                Console.WriteLine("生产了第" + i + "个物品");           
            }
        }
    }
    class Customer
    {     
        public void Consume(BlockingCollection bc)
        {
            Random random = new Random();
            foreach (var item in bc.GetConsumingEnumerable())
            {
                Console.WriteLine("消费了" + item);
                Thread.Sleep(random.Next(500, 2000));
            }
        }
    }

 

你可能感兴趣的:(c#,设计模式)