FixSizeQueue.cs 676 B

1234567891011121314151617181920212223242526272829303132
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Collections.Concurrent;
  6. namespace Aitex.DataAnalysis.Core
  7. {
  8. public class FixSizeQueue<T> : ConcurrentQueue<T>
  9. {
  10. public int FixedSize { get; private set; }
  11. public FixSizeQueue(int size)
  12. {
  13. FixedSize = size;
  14. }
  15. public new void Enqueue(T obj)
  16. {
  17. base.Enqueue(obj);
  18. lock (this)
  19. {
  20. while (base.Count > FixedSize)
  21. {
  22. T outObj;
  23. base.TryDequeue(out outObj);
  24. }
  25. }
  26. }
  27. }
  28. }