|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- using System.Collections.Generic;
-
- namespace Common.system
- {
- /// <summary>
- /// 队列操作类 默认队列数1000
- /// 创建人:赵耀
- /// 创建时间:2019年8月22日
- /// </summary>
- /// <typeparam name="T"></typeparam>
- public class QueueSync<T>
- {
- int _maxcount;
- private Queue<T> _queue = new Queue<T>();
-
- /// <summary>
- /// 默认1000
- /// </summary>
- public QueueSync() : this(1000) { }
-
- /// <summary>
- /// 定义一个指定容量的队列
- /// </summary>
- /// <param name="maxcount"></param>
- public QueueSync(int maxcount)
- {
- _maxcount = maxcount;
- }
-
- /// <summary>
- /// 入队列
- /// </summary>
- /// <param name="obj"></param>
- /// <returns></returns>
- public bool Enqueue(T obj)
- {
- lock (this)
- {
- if (_queue.Count > _maxcount)
- {
- _queue.Dequeue();
- }
- _queue.Enqueue(obj);
- }
- return true;
- }
- /// <summary>
- /// 出队列
- /// </summary>
- /// <returns></returns>
- public T Dequeue()
- {
- T obj = default(T);
- lock (this)
- {
- try
- {
- obj = _queue.Dequeue();
- }
- catch
- {
- // ignored
- }
- }
- return obj;
- }
- }
- }
|