zoukankan      html  css  js  c++  java
  • .NET C# 泛型队列

    1.QueueHelper

    using System.Collections.Concurrent;
    using System.Runtime.CompilerServices;

    namespace WindowsFormsServer.Helper
    {
        public static class QueueHelper<T> where T : class
        {
            private static ConcurrentQueue<StrongBox<T>> _queue;

            public static ConcurrentQueue<StrongBox<T>> Queue
            {
                get { return _queue ?? (_queue = new ConcurrentQueue<StrongBox<T>>()); }
            }

            public static void AddQueue(T t)
            {
                if (_queue == null)
                    _queue = new ConcurrentQueue<StrongBox<T>>();
                _queue.Enqueue(new StrongBox<T>(t));
            }

            public static T DealQueue()
            {
                if (_queue == null)
                    _queue = new ConcurrentQueue<StrongBox<T>>();
                if (_queue.Count > 0)
                {
                    StrongBox<T> t;
                    if (_queue.TryDequeue(out t)) return t.Value;   
                }
                return null;
            }

            public static void EmptyQueue()
            {
                if (_queue == null)
                    _queue = new ConcurrentQueue<StrongBox<T>>();
                StrongBox<T> t;
                while(_queue.TryDequeue(out t))
                    t.Value = default(T);
            }

        }
    }
    2.使用方式:

    //①.入队列

    List<T> ts = new List<T>();

    xxx //此处批量为ts集合赋值

    foreach(var t in ts){

    QueueHelper<T>.AddQueue(t);//入队列

    }

    //②.出队列

    while(QueueHelper<T>.Queue.Count>0){

    T tempT = QueueHelper<T>.DealQueue();

    xxx //业务代码处理tempT

    }

  • 相关阅读:
    P3193 [HNOI2008]GT考试(KMP+矩阵乘法加速dp)
    P2606 [ZJOI2010]排列计数
    洛谷P2657 [SCOI2009]windy数
    P2602 [ZJOI2010]数字计数(递推)
    洛谷 P1073 最优贸易
    [一本通学习笔记] 字典树与 0-1 Trie
    [一本通学习笔记] KMP算法
    [一本通学习笔记] 字符串哈希
    [一本通学习笔记] 广度优先搜索和优化
    [一本通学习笔记] 深度优先搜索与剪枝
  • 原文地址:https://www.cnblogs.com/jeff151013/p/11739258.html
Copyright © 2011-2022 走看看