C # lazyqueue<t> Implementation

Source: Internet
Author: User
Sometimes, when the task is more, a queue needs to be made. When the number of queues reaches a certain number of times, the team will be out and processed, but if it has not reached that number for a long time? Then add a time limit, such as 30 minutes, 1000 elements, whichever condition is first reached will perform the out of line operation.

Implementation of the Lazyqueue<t> class:

Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading;namespace servertoolserver.util{//<summary>///to a specified number of seconds or to a certain amount of execution out of team//</summary>/<typeparam name= " T "></typeparam> public class Lazyqueue<t> {///<summary>//Current queue//&L        t;/summary> private queue<t> _current; <summary>//callback, execute//</summary> private Action when time reaches Maxsec or _curentqueue reaches Maxnum quantity        <T[]> _dequeueaction;        <summary>///maximum number of queues///</summary> private int _maxnum;        <summary>///Queue thread///</summary> private thread _executethread;        <summary>///Current number of seconds//</summary> private int _currentsec = 0;  <summary>////maximum number of seconds to execute this method///</summary> private int _maxsec;      <summary>//constructors///</summary>//<param name= "SEC" > the latest execution seconds </param&        Gt <param name= "num" > Maximum number of queues </param>//<param name= "Dequeueaction" > Out of line function </param> p            Ublic 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 ()); Don't forget to empty the queue and the number of seconds this._current.                    Clear ();    this._currentsec = 0;                ////detects the number of queues and seconds per second thread.sleep (1000);            }            }));        This._executethread.start ();         }///<summary>/////</summary>//<param name= "value" ></param> public void Enqueue (T value) {this._current.        Enqueue (value); }//<summary>///Outbound//</summary>//<returns></returns> P Ublic T Dequeue () {return this._current.        Dequeue (); }///<summary>//Clear Cache///</summary> public void Flush () {thi            s._maxsec = 0;            This._maxnum = 0;            this._currentsec = 0;            This._dequeueaction = null; This._current.            Clear ();        This._executethread.abort (); }    }}

The above is the C # lazyqueue<t> implementation of content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.