C# 生产消费模式

    public class ProductionConsumptionHelper
    {
        public static readonly ProductionConsumptionHelper Instance = new ProductionConsumptionHelper();
        private ProductionConsumptionHelper()
        {
            
        }
        // 任务队列
        ConcurrentQueue<WorkItem> _tasksList = new ConcurrentQueue<WorkItem>();
        readonly object _locker = new object();
        // 通过 _wh 给工作线程发信号 
        EventWaitHandle _wh = new AutoResetEvent(false);
        Thread _worker;

        private void Start()
        {
            if (_worker == null|| !_worker.IsAlive)
            {
                lock (_locker)
                {
                    if (_worker == null || !_worker.IsAlive)
                    {
                        // 任务开始,启动工作线程
                          _worker = new Thread(Work);
                        _worker.Start();
                    }
                }
            }

        }

        /// <summary>消费线程的工作</summary>
        private void Work()
        {
            while (true)
            {
                WorkItem witem = null;
                if (_tasksList.Count > 0)
                {
                    _tasksList.TryDequeue(out witem); // 有任务时,出列任务

                    if (witem == null)  // 退出机制:当遇见一个null任务时,代表任务结束
                        return;
                }
                if (witem != null)
                {
                    if (witem.FuncName == "SaveImage")
                    {
                        try
                        {
                            Console.WriteLine(witem.FuncParams);
                            int i = 1;
                        }
                        catch (Exception ex)
                        { }

                    }
                }  // 任务不为null时,处理数据
                else
                    _wh.WaitOne();   // 没有任务了,等待信号
            }
        }

        /// <summary>插入任务</summary>
        public void EnqueueTask(List<WorkItem> tasklist)
        {
            if (_worker == null|| !_worker.IsAlive)
                Start();

            foreach (WorkItem witem in tasklist)
            {
                _tasksList.Enqueue(witem);  // 向队列中插入任务
            }
            _wh.Set();  // 给工作线程发信号
        }

        /// <summary>结束释放</summary>
        public void Dispose()
        {
            EnqueueTask(null);      // 插入一个Null任务,通知工作线程退出
            _worker.Join();         // 等待工作线程完成
            _wh.Close();            // 释放资源
        }

        /// <summary>处理任务</summary>
        public void DoAction(WorkItem data)
        {
            //
            Console.WriteLine(data.Orderid);
            Thread.Sleep(3000); 
        }

    }
    public class WorkItem
    {
        public WorkItem(string _Orderid,string _FuncName,object _FuncParams)
        {
            Orderid = _Orderid;
            FuncName = _FuncName;
            FuncParams = _FuncParams;
        }

        public string Orderid
        {
            get;
            set;
        }
        public string FuncName
        {
            get;
            set;
        }
        public object FuncParams
        {
            get;
            set;
        }
       


    }
  

测试代码:

 List<WorkItem> Orids = new List<WorkItem>() { };
            Orids.Add(new WorkItem("001", "SaveImage", "111"));
            Orids.Add(new WorkItem("002", "SaveImage", "12"));
            Orids.Add(new WorkItem("003", "SaveImage", "123"));
          ProductionConsumptionHelper.Instance.EnqueueTask(Orids);

总结:一个地方把要处理事情插入到一个队列里,后台启一个线程逐条地处理掉。

posted @ 2022-03-25 10:35  上海-天浩  阅读(116)  评论(0编辑  收藏  举报

Living in ShangHai

Copyright © 2008 天浩阁 Corporation, All Rights Reserved