zoukankan      html  css  js  c++  java
  • c#实现生产者消费者模式

    namespace Alex.MultiThread
    {
       
    publicclass Cell
        {
           
    int cellContents;
           
    bool readerFlag =false;
           
    publicint ReadFromCell()//读(消费)
            {
               
    lock (this)
                {
                   
    if (!readerFlag)
                    {
                       
    try
    {
                            Monitor.Wait(
    this);
                        }
                       
    catch (SynchronizationLockException e)
                        {
                            Console.WriteLine(e);
                        }
                       
    catch (ThreadInterruptedException e)
                        {
                            Console.WriteLine(e);
                        }
                    }
                    Console.WriteLine(
    "Consume:{0}", cellContents);
                    readerFlag
    =false;

                    Monitor.Pulse(
    this);
                }
               
    return cellContents;
            }

           
    publicvoid WriteToCell(int n)//写(生产)
            {
               
    lock (this)
                {
                   
    if (readerFlag)
                    {
                       
    try
                        {
                            Monitor.Wait(
    this);
                        }
                       
    catch (SynchronizationLockException e)
                        {
                            Console.WriteLine(e);
                        }
                       
    catch (ThreadInterruptedException e)
                        {
                            Console.WriteLine(e);
                        }
                    }
                    cellContents
    = n;
                    Console.WriteLine(
    "Produce:{0}", cellContents);
                    readerFlag
    =true;
                    Monitor.Pulse(
    this);
                }
              
            }
        }
       
    ///<summary>
       
    /// 生产者
       
    ///</summary>
        publicclass CellProd
        {
            Cell cell;
           
    int quantity =1;//生产者生产次数
            public CellProd(Cell cell, int request)
            {
               
    this.cell = cell;
                quantity
    = request;
            }

           
    publicvoid ThreadRun()
            {
               
    for(int looper=1;looper<=quantity;looper++)
                {
                    cell.WriteToCell(looper);
    //生产
                }
            }
        }

       
    ///<summary>
       
    /// 消费者
       
    ///</summary>
        publicclass CellCons
        {
             Cell cell;
           
    int quantity =1;
           
    public CellCons(Cell cell, int request)
            {
               
    this.cell = cell;
                quantity
    = request;
            }

           
    publicvoid ThreadRun()
            {
               
    for(int looper=1;looper<=quantity;looper++)
                {
                    cell.ReadFromCell();
    //消费
                }
            }
        }

       
    publicclass MonitorSample
        {
           
    publicstaticvoid Main()
            {
               
    int result =0;//标志位,0表示程序没有出错,1表示出错
                Cell cell =new Cell();
                CellProd prod
    =new CellProd(cell, 20);
                CellCons cons
    =new CellCons(cell, 20);
                Thread producer
    =new Thread(new ThreadStart(prod.ThreadRun));
                Thread consumer
    =new Thread(new ThreadStart(cons.ThreadRun));
               
    try
                {
                    producer.Start();
                    consumer.Start();
                    producer.Join();
                    consumer.Join();
                    Console.ReadLine();
                }
               
    catch (ThreadStartException e)
                {
                    Console.WriteLine(e);
                    result
    =1;
                }
               
    catch (ThreadInterruptedException e)
                {
                    Console.WriteLine(e);
                    result
    =1;
                }
                Environment.ExitCode
    = result;
            }
        }
    }

  • 相关阅读:
    CentOS搭建nginx环境
    Gitment评论插件的使用
    GitPages部署自己的网站
    ubuntu防火墙规则之ufw
    鸟哥的Linux私房菜笔记第六章(二)
    一次使用InfluxDB数据库的总结
    网站实现markdown功能
    鸟哥的Linux私房菜笔记第六章(一)
    Flutter学习笔记(21)--TextField文本框组件和Card卡片组件
    Flutter学习笔记(20)--FloatingActionButton、PopupMenuButton、SimpleDialog、AlertDialog、SnackBar
  • 原文地址:https://www.cnblogs.com/mingjian/p/3428393.html
Copyright © 2011-2022 走看看