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)!