星火微课系统客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

QueueSync.cs 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using System.Collections.Generic;
  2. namespace Common.system
  3. {
  4. /// <summary>
  5. /// 队列操作类 默认队列数1000
  6. /// 创建人:赵耀
  7. /// 创建时间:2019年8月22日
  8. /// </summary>
  9. /// <typeparam name="T"></typeparam>
  10. public class QueueSync<T>
  11. {
  12. int _maxcount;
  13. private Queue<T> _queue = new Queue<T>();
  14. /// <summary>
  15. /// 默认1000
  16. /// </summary>
  17. public QueueSync() : this(1000) { }
  18. /// <summary>
  19. /// 定义一个指定容量的队列
  20. /// </summary>
  21. /// <param name="maxcount"></param>
  22. public QueueSync(int maxcount)
  23. {
  24. _maxcount = maxcount;
  25. }
  26. /// <summary>
  27. /// 入队列
  28. /// </summary>
  29. /// <param name="obj"></param>
  30. /// <returns></returns>
  31. public bool Enqueue(T obj)
  32. {
  33. lock (this)
  34. {
  35. if (_queue.Count > _maxcount)
  36. {
  37. _queue.Dequeue();
  38. }
  39. _queue.Enqueue(obj);
  40. }
  41. return true;
  42. }
  43. /// <summary>
  44. /// 出队列
  45. /// </summary>
  46. /// <returns></returns>
  47. public T Dequeue()
  48. {
  49. T obj = default(T);
  50. lock (this)
  51. {
  52. try
  53. {
  54. obj = _queue.Dequeue();
  55. }
  56. catch
  57. {
  58. // ignored
  59. }
  60. }
  61. return obj;
  62. }
  63. }
  64. }