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