the wait queue
发布时间:2016-01-16 12:00:08
using System;using System.Collections.Concurrent;using System.Threading;namespace Base{ public class WaitQueue<T> : IDisposable where ...
using System;using System.Collections.Concurrent;using System.Threading;namespace Base{ public class WaitQueue<T> : IDisposable where T : class { /// <summary> /// The deal action. /// </summary> public Action<T> DealAction { get; set; } /// <summary> /// The inner queue. /// </summary> private readonly ConcurrentQueue<T> _innerQueue; /// <summary> /// The deal thread. /// </summary> private readonly Thread dealThread; /// <summary> /// The flag for end thread. /// </summary> private bool endThreadFlag = false; /// <summary> /// The auto reset event. /// </summary> private readonly AutoResetEvent autoResetEvent = new AutoResetEvent(true); /// <summary> /// Initializes a new instance of the WaitQueue`1 class. /// </summary> public WaitQueue() { this._innerQueue = new ConcurrentQueue<T>(); this.dealThread = new Thread(this.DealQueue); this.dealThread.Start(); } /// <summary> /// Disposes current instance, end the deal thread and inner queue. /// </summary> public void Dispose() { this.endThreadFlag = true; this._innerQueue.Enqueue(null); this.autoResetEvent.Set(); this.dealThread.Join(); this.autoResetEvent.Close(); } /// <summary> /// Save entity to Queue. /// </summary> /// <param name="entity">The entity what will be deal.</param> public void SaveLog(T entity) { this._innerQueue.Enqueue(entity); this.autoResetEvent.Set(); } /// <summary> /// Out Queue. /// </summary> /// <param name="entity">The init entity.</param> /// <returns>The entity what will be deal.</returns> private bool Dequeue(out T entity) { return this._innerQueue.TryDequeue(out entity); } /// <summary> /// Deal entity in Queue. /// </summary> private void DealQueue() { while (true) { T entity; if (this.Dequeue(out entity)) { if (this.endThreadFlag && entity == null) { return; // Exit the deal thread. } try { if (this.DealAction != null) { this.DealAction(entity); } } catch { } } else { this.autoResetEvent.WaitOne(); } } } }}
原标题:the wait queue
关键词:
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。
阿联酋电商:https://www.goluckyvip.com/tag/11657.html
澳洲邮政:https://www.goluckyvip.com/tag/11658.html
吞吐量:https://www.goluckyvip.com/tag/11659.html
如今无奈破产:https://www.goluckyvip.com/tag/1166.html
提升流量:https://www.goluckyvip.com/tag/11660.html
外贸B2B:https://www.goluckyvip.com/tag/11663.html
南尖岩景区到遂昌汽车站 南尖岩景区到遂昌汽车站多远:https://www.vstour.cn/a/406235.html
北京景点网络购门票网 北京旅游景点门票购买:https://www.vstour.cn/a/406236.html