c# LazyQueue<T>實現

來源:互聯網
上載者:User
有時候,當任務比較多的時候,需要做一個隊列。當隊列數量達到一定數量時候,進行出隊並處理,但是如果很長時間都沒有達到那個數量呢?那就加一個時間限制,例如30分鐘,1000個元素,哪一個條件先達到都會執行出隊操作。

LazyQueue<T>類的實現:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;namespace ServerToolServer.Util{    /// <summary>    /// 到指定的秒數或到達某個數量執行出隊    /// </summary>    /// <typeparam name="T"></typeparam>    public class LazyQueue<T>    {        /// <summary>        /// 當前的隊列        /// </summary>        private Queue<T> _current;        /// <summary>        /// 回調,當時間達到maxSec或_curentQueue達到maxNum數量時,執行        /// </summary>        private Action<T[]> _dequeueAction;        /// <summary>        /// 隊列的最大數量        /// </summary>        private int _maxNum;        /// <summary>        /// 監聽隊列的線程        /// </summary>        private Thread _executeThread;        /// <summary>        /// 當前秒數        /// </summary>        private int _currentSec = 0;        /// <summary>        /// 最大秒數,就執行這個方法        /// </summary>        private int _maxSec;        /// <summary>        /// 構造器        /// </summary>        /// <param name="sec">最遲的執行秒數</param>        /// <param name="num">隊列的最大數量</param>        /// <param name="dequeueAction">出隊函數</param>        public LazyQueue(int sec, int num, Action<T[]> dequeueAction)        {            this._maxSec = sec;            this._maxNum = num;            this._dequeueAction = dequeueAction;            this._current = new Queue<T>();            this._currentSec = 0;            _executeThread = new Thread(new ThreadStart(() =>            {                while (true)                {                    this._currentSec++;                    if (this._current.Count >= _maxNum || this._currentSec >= _maxSec)                    {                        this._dequeueAction.Invoke(this._current.ToArray());                                                ////別忘了清空隊列和秒數                        this._current.Clear();                        this._currentSec = 0;                    }                    ////每秒檢測一下隊列的數量和秒數                    Thread.Sleep(1000);                }            }));            this._executeThread.Start();        }        /// <summary>        /// 入隊        /// </summary>        /// <param name="value"></param>        public void Enqueue(T value)        {            this._current.Enqueue(value);        }        /// <summary>        /// 出隊        /// </summary>        /// <returns></returns>        public T Dequeue()        {            return this._current.Dequeue();        }        /// <summary>        /// 清緩衝        /// </summary>        public void Flush()        {            this._maxSec = 0;            this._maxNum = 0;            this._currentSec = 0;            this._dequeueAction = null;            this._current.Clear();            this._executeThread.Abort();        }    }}

以上就是c# LazyQueue<T>實現的內容,更多相關內容請關注topic.alibabacloud.com(www.php.cn)!

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.