消息队列

作者:追风剑情 发布于:2020-1-14 11:10 分类:C#

示例:

MessageQueue.cs


  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace NotifyServer
  8. {
  9. /// <summary>
  10. /// 线程安全的消息队列
  11. /// </summary>
  12. /// <typeparam name="T"></typeparam>
  13. public sealed class MessageQueue<T>
  14. {
  15. private static Queue<T> queue = new Queue<T>();
  16.  
  17. public static void Enqueue(T msg)
  18. {
  19. lock (queue)
  20. {
  21. queue.Enqueue(msg);
  22. }
  23. }
  24.  
  25. public static T Dequeue()
  26. {
  27. lock (queue)
  28. {
  29. if (queue.Count <= 0)
  30. return default(T);
  31. return queue.Dequeue();
  32. }
  33. }
  34.  
  35. public static int Count
  36. {
  37. get
  38. {
  39. lock (queue)
  40. {
  41. return queue.Count;
  42. }
  43. }
  44. }
  45. }
  46. }


Program.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. using System.Threading;
  8. using System.Net;
  9. using System.Net.Sockets;
  10. using System.Runtime.InteropServices;
  11.  
  12. namespace NotifyServer
  13. {
  14. class Program
  15. {
  16. static void Main(string[] args)
  17. {
  18. CheckEnvironment();
  19.  
  20. //向消息队列中增加一组测试数据
  21. for (int i=0; i<100; i++)
  22. {
  23. Message msg = new Message();
  24. msg.id = i;
  25. MessageQueue<Message>.Enqueue(msg);
  26. }
  27.  
  28. //启动多条消息处理线程
  29. int processorCount = Environment.ProcessorCount;
  30. for (int i=0; i < processorCount; i++)
  31. ThreadPool.QueueUserWorkItem(ProcessMessage);
  32.  
  33. Console.Read();
  34. }
  35.  
  36. // 检查运行环境
  37. private static void CheckEnvironment()
  38. {
  39. Console.WriteLine("OS Version: {0}", Environment.OSVersion.ToString());
  40. Console.WriteLine("CLR Version: {0}", Environment.Version.ToString());
  41. Console.WriteLine("逻辑处理器数量: {0}", Environment.ProcessorCount);
  42. Console.WriteLine("系统已运行时间: {0}秒", Environment.TickCount / 1000);
  43. Console.WriteLine("操作系统内存页字节数: {0}", Environment.SystemPageSize);
  44. Console.WriteLine("映射到进程上下文的物理内存量: {0}", Environment.WorkingSet);
  45. Console.WriteLine("是否为64位操作系统: {0}", Environment.Is64BitOperatingSystem);
  46. Console.WriteLine("是否为64位进程: {0}", Environment.Is64BitProcess);
  47. Console.WriteLine("登录到操作系统的用户名: {0}", Environment.UserName);
  48. Console.WriteLine("计算机名称: {0}", Environment.MachineName);
  49. }
  50.  
  51. // 处理消息
  52. private static void ProcessMessage(object state)
  53. {
  54. while (MessageQueue<Message>.Count > 0)
  55. {
  56. Message msg = MessageQueue<Message>.Dequeue();
  57. Console.WriteLine("线程ID:{0}, 处理消息ID={1}", Environment.CurrentManagedThreadId, msg.id);
  58.  
  59. Thread.Sleep(1000); //模拟处理更多任务
  60. }
  61. }
  62. }
  63.  
  64. public class Message
  65. {
  66. public int id; //消息ID
  67. }
  68. }

运行测试

111111.png

标签: C#

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号