C#队列 ConcurrentQueue和Queue之间的区别

ConcurrentQueue<T>//保证线程安全
Queue<T>//不保障线程安全
Queue<T>//添加的同时进行读取或者其他操作就会出现问题

可以去微软文档看一下

微软文档说明

队列帮助类 此处是使用 ConcurrentQueue


    public class deliverQueue
    {
        private static readonly ConcurrentQueue<CancelOrderDeliver_Queue> _CancelDeliverQueue;
        private static Thread _CancelThread;
        private static Boolean autoResetEventFlag = false;
        private static bool flag = true;
        private static AutoResetEvent aEvent = new AutoResetEvent(false);
        static deliverQueue()
        {
            _CancelDeliverQueue = new ConcurrentQueue<CancelOrderDeliver_Queue>();
            _CancelThread = new Thread(CancelProgram);
        }


        public static void AddQueue(CancelOrderDeliver_Queue que)
        {
            try
            {
                Monitor.Enter(_CancelDeliverQueue);
                _CancelDeliverQueue.Enqueue(que);
                Monitor.Exit(_CancelDeliverQueue);
                if (autoResetEventFlag)
                {
                    aEvent.Set();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine (string.Format("[编号:{0}| 平台:{1}]写入队列是失败{2}", que.orderid, que.type, ex.Message), "orderdeliver_log");
            }

        }
        public static int QueueCount()
        {
            return _CancelDeliverQueue.Count;
        }
        public static ConcurrentQueue<CancelOrderDeliver_Queue> returnQue()
        {
            return _CancelDeliverQueue;
        }
        /// 
        /// ExitThread是退出日志记录线程的方法,一旦退出,无法开启,一般在程序关闭时执行
        /// 
        public static void ExitThread()
        {
            flag = false;
            aEvent.Set();//恢复线程执行
        }

        public static void StartThread()
        {
            flag = true;
            _CancelThread.Start();
        }

        private static void CancelProgram()
        {
            while (flag)
            {
                while (_CancelDeliverQueue.Count > 0)
                {
                    autoResetEventFlag = false;
                    Monitor.Enter(_CancelDeliverQueue);
                    _CancelDeliverQueue.TryDequeue(out CancelOrderDeliver_Queue thismodel);
                    Monitor.Exit(_CancelDeliverQueue);

                    Console.WriteLine("删除队列中的数据");

                    //Thread.Sleep(1000);
                }

                autoResetEventFlag = true;
                aEvent.WaitOne();
            }
        }
    }
    public class CancelOrderDeliver_Queue
    {
        /// 
        /// 编号
        /// 
        public string orderid { get; set; }
        /// 
        ///类型
        /// 
        public string type { get; set; }
    }

deliverQueue2类就是把deliverQueue类里的ConcurrentQueue 改为Queue,这里我就不展示了。

这里我们来测试用三个线程去操作队列

  static void Main(string[] args)
        {
            //创建2个线程
            Thread ThreadOne = new Thread(new ThreadStart(ThreadOne1));
            Thread ThreadTwo = new Thread(new ThreadStart(ThreadTwo2));
            Thread ThreadThree = new Thread(new ThreadStart(ThreadThre3));
            ThreadOne.Start();
            ThreadTwo.Start();
            ThreadThree.Start();
          
         //   GetToken();
            Console.ReadKey();

        }
        /// 
        /// 线程1执行逻辑
        /// 
        private static void ThreadOne1()
        {
            deliverQueue.AddQueue(new CancelOrderDeliver_Queue() { orderid = "1234555", type = "ConcurrentQueue" });
            deliverQueue2.AddQueue(new CancelOrderDeliver_Queue() { orderid = "1234", type = "Queue" });
            deliverQueue2.AddQueue(new CancelOrderDeliver_Queue() { orderid = "1234", type = "Queue" });
            Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ff"));
        }
        /// 
        /// 线程2执行逻辑,与线程1相同
        /// 
        private static void ThreadTwo2()
        {
            deliverQueue.AddQueue(new CancelOrderDeliver_Queue() { orderid = "1234555", type = "ConcurrentQueue" });
            deliverQueue2.AddQueue(new CancelOrderDeliver_Queue() { orderid = "1234", type = "Queue" });
            deliverQueue2.AddQueue(new CancelOrderDeliver_Queue() { orderid = "1234", type = "Queue" });
            Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ff"));
        }
        private static void ThreadThre3()
        {
            string count = getqueuecount();
            string count2 = getqueuecount2();
            Conslole();
            Conslole2();
            Console.WriteLine("ConcurrentQueue 的count---->" + count);
            Console.WriteLine("Queue 的Count---->" + count2);
        }
        public static string getqueuecount()
        {
            return deliverQueue.QueueCount().ToString();
        }
        public static string getqueuecount2()
        {
            return deliverQueue2.QueueCount().ToString();
        }
        public static void Conslole()
        {
            foreach (var item in deliverQueue.returnQue())
            {
                Console.WriteLine(item.orderid+"-----"+item.type);
            }
        }
        public static void Conslole2()
        {
            foreach (var item in deliverQueue2.returnQue())
            {
                Console.WriteLine(item.orderid + "-----" + item.type);
            }
        }

C#队列 ConcurrentQueue和Queue之间的区别_第1张图片

由此可看出我们往队列Queue里添加了四个数据单实际他的count只有两个。另外两个并没有命中而ConcurrentQueue只添加了两个两个都命中了

你可能感兴趣的:(C#)