private readonly object _lockObj; //保证_count的多线程同步 /// <summary> /// 创建一个延时的阻塞队列 /// </summary> /// <param name="capacity"></param> /// <param name="delayTime">队列中每个元素的延时时间</param> public MessageQueueDelay(int capacity, int delayTime) { _count = 0; _delayTime = delayTime; /** * 先将元素保存于延时队列中 * 若过期,则从延时队列抛出 * 将延时队列抛出的元素保存至缓冲队列中 * 若缓冲队列中存在元素,则元素可被取出;否则进入阻塞状态 */ _synQueue = new SimpleSynQueue <T>(); _delayDispatcherQueue = new DelayDispatcherQueue <T>(capacity); _delayDispatcherQueue.GetEvent += delegate(T t) { _synQueue.Enqueue(t); }; _lockObj = new object(); }
public void Enqueue(T t) { _synQueue.Enqueue(t); }