zoukankan      html  css  js  c++  java
  • C# 生产者和消费者问题使用Monitor同步

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;

    namespace ConsoleApplication1
    {
        public class Program
        {
            static void Main(string[] args)
            {
                SyncStack ss = new SyncStack();
                Producer p = new Producer(ss);
                Consumer c = new Consumer(ss);
                new Thread(p.run).Start();
                new Thread(c.run).Start();
            }
        }
        public class Cake
        {
            int id;
            public Cake(int id)
            {
                this.id = id;
            }
            public String toString()
            {
                return "Cake" + id;
            }
        }
        public class SyncStack
        {
            int index = 0;
            Cake[] arrWT = new Cake[4];
            public void push(Cake wt)
            {
                try
                {
                    Monitor.Enter(this);
                    while (index == arrWT.Length)
                    {
                        try
                        {

                            Console.WriteLine("已经生产满了!");
                            Monitor.Wait(this);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                    }
                    Monitor.Pulse(this);
                    arrWT[index] = wt;
                    index++;
                }
                finally
                {
                    Monitor.Exit(this);
                }
            }
            public Cake pop()
            {
                try
                {
                    Monitor.Enter(this);
                    while (index == 0)
                    {
                        try
                        {
                            Console.WriteLine("已经消费空了!");
                            Monitor.Wait(this);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                    }
                    Monitor.Pulse(this);
                    index--;
                    return arrWT[index];
                }
                finally
                {
                    Monitor.Exit(this);
                }
            }
        }
        public class Producer
        {

            private bool flag = true;
            private SyncStack m_syncStack = null;
            public Producer(SyncStack ss)
            {
                this.m_syncStack = ss;
            }
            public void run()
            {
                int i = 0;
                while (flag)
                {
                    Cake wt = new Cake(i);
                    i++;
                    m_syncStack.push(wt);
                    Console.WriteLine("生产了:" + wt.toString());
                }
            }
            public void Shutdown()
            {
                flag = false;
            }
        }
        public class Consumer
        {
            private bool flag = true;
            private SyncStack m_syncStack = null;
            public Consumer(SyncStack ss)
            {
                this.m_syncStack = ss;
            }
            public void run()
            {
                while (flag)
                {

                    Cake wt = m_syncStack.pop();
                    Console.WriteLine("消费了:" + wt.toString());
                    try
                    {
                        Thread.Sleep(2000);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            }
            public void Shutdown()
            {
                flag = false;
            }
        }
    }

  • 相关阅读:
    xml方式将dataset导出excel
    linux安装Navicat,界面出现乱码解决方法 (转发)
    ERROR 29 (HY000): File '/var/lib/mysql/txtdata/yz2014_1.txt' not found (Errcode: 13 "Permission denied")
    centos7中yum安装ntfs3g(转载)
    MariaDB中my.cnf文件误删除
    Mysql操作命令出现错误时消除/mysql数据导入txt
    Linux查找yum安装软件在系统中路径
    Centos7安装MariaDB安装数据库yum安装数据库远程登录数据库存储路径更改
    Zookeeper常用命令
    Hbase学习连接-数据导入
  • 原文地址:https://www.cnblogs.com/bjchaofan/p/3466997.html
Copyright © 2011-2022 走看看