C# 总结ManualResetEvent与AutoResetEvent【二】

接上篇C# 总结ManualResetEvent与AutoResetEvent

C# 总结ManualResetEvent与AutoResetEvent

   public class TestAutoResetEvent
    {
        Thread thin;
        Thread thout;
        Queue<int> enqueen = new Queue<int>();
        AutoResetEvent ar = new AutoResetEvent(false);

        public TestAutoResetEvent()
        {
            thin = new Thread(AddToenque);
            thin.Name = "thin";
            thin.Start();

            thout = new Thread(OutEnque);
            thout.Name = "thout";
            thout.Start();

        }

        public void AddToenque()
        {
            int k = 0;
            while (true)
            {

                for (int i = 1; i < 2; i++)
                {
                    if (k > 10)
                    {
                        break;
                    }
                    enqueen.Enqueue(i);
                    Console.WriteLine("入队___" + i);
                    Thread.Sleep(500);
                    k++;
                }
                ar.Set();
            }
        }

        public void OutEnque()
        {
            while (true)
            {
                ar.WaitOne();
                if (enqueen.Count() > 0)
                {
                    for (int i = 0; i < enqueen.Count(); i++)
                    {
                        Console.WriteLine("出队:__" + enqueen.Dequeue());
                        Thread.Sleep(500);
                        //ar.Reset();
                    }
                }
            }
        }
    }


    public class ProductAndCostTester
    {
        Thread threadproductglue;
        Thread threadproductassblem;
        Thread threadCost;
        List<int> _goodList = new List<int>();
        private ManualResetEvent _mre = new ManualResetEvent(false);
        public ProductAndCostTester()
        {
            _goodList = new List<int>();

            _mre = new ManualResetEvent(false);//false初始化状态为无信号,将使WaitOne阻塞

            threadproductglue = new Thread(ProductGlue);
            threadproductglue.Name = "ProductGlue";
            threadproductglue.Start();

            threadproductassblem = new Thread(ProductAssblem);
            threadproductassblem.Name = "ProductAssblem";
            threadproductassblem.Start();

            threadCost = new Thread(Cost);
            threadCost.Name = "Cost";
            threadCost.Start();
        }
    
        public void ProductGlue()
        {
            while (true)
            {
                for (int i = 0; i < 5; i++)
                {
                    _goodList.Add(1);
                }
                _mre.Set();
                Thread.Sleep(5000);
            }
        }
        public void ProductAssblem()
        {
            while (true)
            {
                for (int i = 0; i < 8; i++)
                {
                    _goodList.Add(1);
                }
                _mre.Set();
                Thread.Sleep(8000);
            }
        }

        public void Cost()
        {
            while (true)
            {
                if (_goodList.Count() > 0)
                {
                    Console.WriteLine("Cost " + _goodList.Count + " at " + DateTime.Now.ToString("HH:mm:ss"));
                    _goodList.Clear();
                    _mre.Reset();
                }
                else
                {
                    Console.WriteLine("No cost at " + DateTime.Now.ToString("HH:mm:ss"));
                    _mre.WaitOne();//如果没有可消费的产品,即无信号,则会阻塞
                }
            }
        }

    }


    public class TestAuto
    {
        public TestAuto()
        {
           
        }
        AutoResetEvent autoResetEvent = new AutoResetEvent(false); //false代表默认中阻塞状态
        void Do()
        {
            var worker = new Thread(() =>
            {
                Console.WriteLine("Start worker");
                Console.WriteLine("wait");
                autoResetEvent.WaitOne(); //等待信号
                Console.WriteLine("do");
            });
            worker.Start();
        }

        void Signal()
        {
            Console.WriteLine("Sent signal");
            Console.ReadKey();
            autoResetEvent.Set(); //发送信号
            Console.ReadKey();
        }
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值