1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace Common.system
- {
- /// <summary>
- /// 队列操作类 默认队列数1000
- /// 创建人:赵耀
- /// 创建时间:2019年8月22日
- /// </summary>
- /// <typeparam name="T"></typeparam>
- public class QueueSync<T>
- {
- int _maxcount = 10000;
- Queue<T> _queue = new Queue<T>();
- /// <summary>
- /// 获取该队列包含的元素数
- /// </summary>
- public int Count
- {
- get
- {
- if (_queue == null)
- {
- return 0;
- }
- else
- {
- return _queue.Count;
- }
- }
- }
-
- /// <summary>
- /// 默认1000
- /// </summary>
- public QueueSync() : this(1000) { }
-
- /// <summary>
- /// 定义一个指定容量的队列
- /// </summary>
- /// <param name="maxcount"></param>
- public QueueSync(int maxcount)
- {
- //_maxcount = Math.Max(maxcount, _maxcount);
- _maxcount = maxcount;
- }
-
- /// <summary>
- /// 入队列
- /// </summary>
- /// <param name="obj"></param>
- /// <returns></returns>
- public bool Enqueue(T obj)
- {
- bool result = false;
- lock (this)
- {
- if (_queue.Count > _maxcount)
- {
- _queue.Dequeue();
- }
- _queue.Enqueue(obj);
- result = true;
- }
- return result;
- }
- /// <summary>
- /// 出队列
- /// </summary>
- /// <returns></returns>
- public T Dequeue()
- {
- T obj = default(T);
- lock (this)
- {
- try
- {
- obj = _queue.Dequeue();
- }
- catch
- {
-
- }
- }
- return obj;
- }
- }
- }
|