星火批注
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace Common.system
  7. {
  8. /// <summary>
  9. /// 队列操作类 默认队列数1000
  10. /// 创建人:赵耀
  11. /// 创建时间:2019年8月22日
  12. /// </summary>
  13. /// <typeparam name="T"></typeparam>
  14. public class QueueSync<T>
  15. {
  16. int _maxcount = 10000;
  17. Queue<T> _queue = new Queue<T>();
  18. /// <summary>
  19. /// 获取该队列包含的元素数
  20. /// </summary>
  21. public int Count
  22. {
  23. get
  24. {
  25. if (_queue == null)
  26. {
  27. return 0;
  28. }
  29. else
  30. {
  31. return _queue.Count;
  32. }
  33. }
  34. }
  35. /// <summary>
  36. /// 默认1000
  37. /// </summary>
  38. public QueueSync() : this(1000) { }
  39. /// <summary>
  40. /// 定义一个指定容量的队列
  41. /// </summary>
  42. /// <param name="maxcount"></param>
  43. public QueueSync(int maxcount)
  44. {
  45. //_maxcount = Math.Max(maxcount, _maxcount);
  46. _maxcount = maxcount;
  47. }
  48. /// <summary>
  49. /// 入队列
  50. /// </summary>
  51. /// <param name="obj"></param>
  52. /// <returns></returns>
  53. public bool Enqueue(T obj)
  54. {
  55. bool result = false;
  56. lock (this)
  57. {
  58. if (_queue.Count > _maxcount)
  59. {
  60. _queue.Dequeue();
  61. }
  62. _queue.Enqueue(obj);
  63. result = true;
  64. }
  65. return result;
  66. }
  67. /// <summary>
  68. /// 出队列
  69. /// </summary>
  70. /// <returns></returns>
  71. public T Dequeue()
  72. {
  73. T obj = default(T);
  74. lock (this)
  75. {
  76. try
  77. {
  78. obj = _queue.Dequeue();
  79. }
  80. catch
  81. {
  82. }
  83. }
  84. return obj;
  85. }
  86. }
  87. }