消息队列

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

示例:

MessageQueue.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NotifyServer
{
    /// <summary>
    /// 线程安全的消息队列
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public sealed class MessageQueue<T>
    {
        private static Queue<T> queue = new Queue<T>();

        public static void Enqueue(T msg)
        {
            lock (queue)
            {
                queue.Enqueue(msg);
            }
        }

        public static T Dequeue()
        {
            lock (queue)
            {
                if (queue.Count <= 0)
                    return default(T);
                return queue.Dequeue();
            }
        }

        public static int Count
        {
            get
            {
                lock (queue)
                {
                    return queue.Count;
                }
            }
        }
    }
}


Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;

namespace NotifyServer
{
    class Program
    {
        static void Main(string[] args)
        {
            CheckEnvironment();

            //向消息队列中增加一组测试数据
            for (int i=0; i<100; i++)
            {
                Message msg = new Message();
                msg.id = i;
                MessageQueue<Message>.Enqueue(msg);
            }

            //启动多条消息处理线程
            int processorCount = Environment.ProcessorCount;
            for (int i=0; i < processorCount; i++)
                ThreadPool.QueueUserWorkItem(ProcessMessage);

            Console.Read();
        }

        // 检查运行环境
        private static void CheckEnvironment()
        {
            Console.WriteLine("OS Version: {0}", Environment.OSVersion.ToString());
            Console.WriteLine("CLR Version: {0}", Environment.Version.ToString());
            Console.WriteLine("逻辑处理器数量: {0}", Environment.ProcessorCount);
            Console.WriteLine("系统已运行时间: {0}秒", Environment.TickCount / 1000);
            Console.WriteLine("操作系统内存页字节数: {0}", Environment.SystemPageSize);
            Console.WriteLine("映射到进程上下文的物理内存量: {0}", Environment.WorkingSet);
            Console.WriteLine("是否为64位操作系统: {0}", Environment.Is64BitOperatingSystem);
            Console.WriteLine("是否为64位进程: {0}", Environment.Is64BitProcess);
            Console.WriteLine("登录到操作系统的用户名: {0}", Environment.UserName);
            Console.WriteLine("计算机名称: {0}", Environment.MachineName);
        }

        // 处理消息
        private static void ProcessMessage(object state)
        {
            while (MessageQueue<Message>.Count > 0)
            {
                Message msg = MessageQueue<Message>.Dequeue();
                Console.WriteLine("线程ID:{0}, 处理消息ID={1}", Environment.CurrentManagedThreadId, msg.id);

                Thread.Sleep(1000); //模拟处理更多任务
            }
        }
    }

    public class Message
    {
        public int id; //消息ID
    }
}

运行测试

111111.png

标签: C#

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号