懒得勤快 7 年之前
父节点
当前提交
740dccff1d
共有 1 个文件被更改,包括 35 次插入0 次删除
  1. 35 0
      Masuit.Tools.Core/Systems/ConcurrentLimitedQueue.cs

+ 35 - 0
Masuit.Tools.Core/Systems/ConcurrentLimitedQueue.cs

@@ -0,0 +1,35 @@
+using System.Collections.Concurrent;
+using System.Collections.Generic;
+using System.Linq;
+
+namespace Masuit.Tools.Core.Systems
+{
+    /// <summary>
+    /// 定长队列
+    /// </summary>
+    /// <typeparam name="T"></typeparam>
+    public class ConcurrentLimitedQueue<T> : ConcurrentQueue<T>
+    {
+        public int Limit { get; set; }
+
+        public ConcurrentLimitedQueue(int limit)
+        {
+            Limit = limit;
+        }
+
+        public ConcurrentLimitedQueue(IEnumerable<T> list) : base(list)
+        {
+            Limit = list.Count();
+        }
+
+        public new void Enqueue(T item)
+        {
+            if (Count >= Limit)
+            {
+                TryDequeue(out var _);
+            }
+
+            base.Enqueue(item);
+        }
+    }
+}