C# 生产者与消费者

线程处理,同步两个线程。

里面用到了Monitor.pulse(this),lock(this),Thread的相关知识。

using System;
using System.Threading;

namespace TestForThread3
{
    public class Cell
    {
        int quantity;
        bool readerFlag;

        public Cell()
        {
            quantity = 0;
            readerFlag = false;
        }

        public int readFormCell()
        {
            lock(this)
            {
                /*如果还轮不到该进程read,那么就让它一直等待*/
                if (!readerFlag)
                {
                    try
                    {
                        Monitor.Wait(this);
                    }
                    catch (ThreadStateException e)
                    {
                        Console.WriteLine(e);
                    }
                    catch (SynchronizationLockException e)
                    {
                        Console.WriteLine(e);
                    }
                }
                /*先把可读标志readerFlag置为false,发出脉冲通知写线程可以去写了*/
                Console.WriteLine("Cusumer:{0}", quantity);
                readerFlag = false;
                Monitor.Pulse(this);
                return quantity;
            }
        }

        public void writeIntoCell(int n)
        {
            lock (this)
            {
                if (readerFlag)
                {
                    try
                    {
                        Monitor.Wait(this);
                    }
                    catch (ThreadStateException e)
                    {
                        Console.WriteLine(e);
                    }
                    catch (SynchronizationLockException e)
                    {
                        Console.WriteLine(e);
                    }
                }
                quantity = n;
                Console.WriteLine("Produce:{0}", quantity);
                /*先把可读标志readerFlag置为true,发出脉冲通知读线程可以去读了*/
                readerFlag = true;
                Monitor.Pulse(this);
            }
        }
    }

    public class CellProd
    {
        Cell cell=new Cell();
        int quantity = 0;

        public CellProd(Cell box, int request)
        {
            cell = box;
            quantity = request;
        }

        public void Produce()
        {
            /*生产者生产20(request)个资源,并且一一写入cell中*/
            for (int looper = 1; looper <= quantity; looper++)
            {
                cell.writeIntoCell(looper);
            }
        }

    }

    public class CellCons
    {
        Cell cell = new Cell();
        int quantity = 0;

        public CellCons(Cell box, int request)
        {
            cell = box;
            quantity = request;
        }

        /*有20(request)个消费者(或1个消费者消费20次)去消费那慢慢生产的20个资源*/
        public void Consume()
        {
            int valRuturned = 0;
            for (int looper = 1; looper <= quantity; looper++)
            {
                valRuturned = cell.readFormCell();
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Cell cell = new Cell();
            CellProd prod = new CellProd(cell, 20);
            CellCons cons = new CellCons(cell, 20);
            try
            {
                Thread oThread1 = new Thread(new ThreadStart(prod.Produce));
                Thread oThread2 = new Thread(new ThreadStart(cons.Consume));

                oThread1.Start();
                oThread2.Start();
                /*join方法是阻止调用个线程,让主线程等待它死亡或超出有效时间*/
                oThread1.Join();
                oThread2.Join();
                /*到这里线程就结束啦·*/
            }
            catch (ThreadStateException e)
            {
                Console.WriteLine(e);
            }
            catch (SynchronizationLockException e)
            {
                Console.WriteLine(e);
            }


        }
    }
}


你可能感兴趣的:(C# 生产者与消费者)