C# 使用ManualResetEvent 实现多线程的生产消费

5 篇文章 0 订阅

C# 使用ManualResetEvent 实现多线程的生产消费
变量命名使用中文。更容易理解。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
using System.Threading;
namespace 使用ManualResetEvent实现生产消费
{
    class Program
    {
        static void Main(string[] args)
        {
            var 青铜生产商 = new 青铜生产商();
            var 白银生产商 = new 白银生产商();
            var 黄金生产商 = new 黄金生产商();
            var 青铜消费者 = new 消费者(5000);
            var 白银消费者 = new 消费者(3000);
            var 黄金消费者 = new 消费者(1000);
            Parallel.Invoke(() => 青铜生产商.生产(), () => 白银生产商.生产(), () => 黄金生产商.生产(), () => 青铜消费者.消费(), () => 白银消费者.消费(), () => 黄金消费者.消费());
            
            WriteLine("END");
            ReadLine();
        }
    }

    public static class 商品
    {
        private static readonly object locker = new object();
        private static Queue<int> list = new Queue<int>();
        private static int No = 0;
        public static int GetCount()
        {
            lock (locker)
            {
                int i;
                i = list.Count;
                return i;
            }
        }
        public static int GetProduct()
        {
            lock (locker)
            {
                int i;
                i = list.Dequeue();
                return i;
            }
        }
        public static int AddProduct()
        {
            lock (locker)
            {
                int i;
                i = No++;
                list.Enqueue(i);
                return i;
            }
        }

    }

    #region 生产者
    public abstract class 生产者
    {
        private static ManualResetEvent 生产控制 = new ManualResetEvent(false);
        public abstract int 生产时间 { get; }
        public void 生产()
        {
            for (int i = 0; i < 10; i++)
            {
                WriteLine($"本次花{生产时间},生产出了【{商品.AddProduct()}】");
                Thread.Sleep(生产时间);
                if (商品.GetCount() == 10)
                {
                    控制中心.开始消费();
                }
            }
        }
    }
    public class 青铜生产商 : 生产者
    {
        public override int 生产时间 { get => 5000; }
    }
    public class 白银生产商 : 生产者
    {
        public override int 生产时间 { get => 3000; }
    }
    public class 黄金生产商 : 生产者
    {
        public override int 生产时间 { get => 1000; }
    }
    #endregion

    #region 消费者
    public class 消费者
    {
        private readonly int 消费时间;

        public 消费者(int 消费时间)
        {
            this.消费时间 = 消费时间;
        }

        public void 消费()
        {
            控制中心.停止消费();
            while (true)
            {

                if (商品.GetCount() > 0)
                {
                    WriteLine($"本次花{消费时间},消费掉了{商品.GetProduct()}");
                    Thread.Sleep(消费时间);
                }
                else 
                {
                    break;   
                }
            }
            Console.WriteLine($"消费者结束{Thread.CurrentThread.ManagedThreadId}");
        }
    }
    #endregion

    #region 控制中心
    public static class 控制中心 
    {
        private static ManualResetEvent 消费控制 = new ManualResetEvent(false);
        public static bool 是否停产 = false;

        public static void 开始消费()
        {
            WriteLine("开始消费");
            消费控制.Set();
        }
        public static void 停止消费() 
        {
            消费控制.WaitOne();
        }
    }
    #endregion
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值