关键词搜索

源码搜索 ×
×

C#一个简单的队列测试-生产消费

发布2018-01-13浏览1278次

详情内容

对于消费者和生产者而言,资源的有无是可见的,至少生产者有了产品之后就会通知消费者去获取。

队列

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace AVParser
  7. {
  8. /// <summary>
  9. /// 音视频数据队列
  10. /// </summary>
  11. public class AVQueue
  12. {
  13. private System.Collections.Concurrent.ConcurrentQueue<AV> mQueues = new System.Collections.Concurrent.ConcurrentQueue<AV>();
  14. /// <summary>
  15. /// 队列添加音视频数据
  16. /// </summary>
  17. /// <param name="av"></param>
  18. public void Add2Queues(AV av)
  19. {
  20. mQueues.Enqueue(av);
  21. }
  22. /// <summary>
  23. /// 从队列提取一个音视频
  24. /// </summary>
  25. /// <returns></returns>
  26. public AV GetAVFromQueue()
  27. {
  28. AV av;
  29. var flag = mQueues.TryDequeue(out av);
  30. if (!flag)
  31. {
  32. Console.WriteLine("队列取出数据失败");
  33. }
  34. return av;
  35. }
  36. /// <summary>
  37. /// 队列对象个数统计
  38. /// </summary>
  39. /// <returns></returns>
  40. public int CountQueue()
  41. {
  42. return mQueues.Count;
  43. }
  44. }
  45. }

测试

下面用lock(object){}来同步:

  1. using AVParser.Parser;
  2. using FFmpeg.AutoGen;
  3. using JX;
  4. using RTForwardServer;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Media;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13. namespace AVParser
  14. {
  15. static class Program
  16. {
  17. /// <summary>
  18. /// 应用程序的主入口点。
  19. /// </summary>
  20. [STAThread]
  21. static void Main()
  22. {
  23. Application.EnableVisualStyles();
  24. Application.SetCompatibleTextRenderingDefault(false);
  25. //Application.Run(new FrmPlayer());
  26. AVQueue aVQueue = new AVQueue();
  27. int index = 0;
  28. byte[] bytes = new byte[] { 0x0001 , 0x0002, 0x0003, 0x0004, 0x0005 , 0x0006 , 0x0007 , 0x0008 , 0x0009 , 0x0010 };
  29. Thread producer = new Thread(() =>
  30. {
  31. while (true && index < 10)
  32. {
  33. if (aVQueue.CountQueue()==0)
  34. {
  35. lock (aVQueue)
  36. {
  37. AV av = new AV(AV.AVTYPE.MEDIA_AUDIO, new JTRTHead(), new byte[1] { bytes[index] });
  38. aVQueue.Add2Queues(av);
  39. index++;
  40. Console.WriteLine("生产:" + index);
  41. Thread.Sleep(1000);
  42. }
  43. }
  44. }
  45. });
  46. producer.Start();
  47. int count = 0;
  48. Thread customer = new Thread(() =>
  49. {
  50. while (true&& count<10)
  51. {
  52. if (aVQueue.CountQueue() > 0)
  53. {
  54. lock (aVQueue)
  55. {
  56. AV av = aVQueue.GetAVFromQueue();
  57. Console.WriteLine("消费:" + BitConverter.ToString(av.Data));
  58. count++;
  59. }
  60. }
  61. }
  62. });
  63. customer.Start();
  64. Console.ReadLine();
  65. }
  66. }
  67. }

输出

  1. 生产:1
  2. 消费:01
  3. 生产:2
  4. 消费:02
  5. 生产:3
  6. 消费:03
  7. 生产:4
  8. 消费:04
  9. 生产:5
  10. 消费:05
  11. 生产:6
  12. 消费:06
  13. 生产:7
  14. 消费:07
  15. 生产:8
  16. 消费:08
  17. 生产:9
  18. 消费:09
  19. 生产:10
  20. 消费:10

问题

两个线程,一个负责生产一个负责消费,但是生产不阻塞消费,即实现边生产边消费,队列和线程应注意些什么?

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载