zoukankan      html  css  js  c++  java
  • Redis 消息队列 初体验

    队列之生产者、消费者模式

    using System;
    using System.Threading;
    using NServiceKit.Redis;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                //开启一个线程添加生产者
                Thread thread = new Thread(Run);
                thread.Start();
    
                //开启10个线程来进行消费
                Thread[] threads = new Thread[10];
                for (int i = 0; i < threads.Length; i++)
                {
                    threads[i] = new Thread(Pull);
                    threads[i].Start();
                }
                Console.Read();
            }
    
            //移除队列  消费者
            private static void Pull()
            {
                IRedisClientFactory factory = RedisClientFactory.Instance;
                using (IRedisClient client = factory.CreateRedisClient("127.0.0.1", 6379))
                {
                    while (true)
                    {
                        if (client.GetListCount("Myqueue") > 0)
                        {
                            string result = client.DequeueItemFromList("Myqueue");
                            if (string.IsNullOrEmpty(result))
                            {
                                Thread.SpinWait(1000);
                            }
                            else
                            {
                                Console.WriteLine("Threadid:" + Thread.CurrentThread.ManagedThreadId.ToString() + "	" +
                                                  result);
                            }
                        }
                        else
                        {
                            Thread.SpinWait(1000);
                        }
                    }
                }
            }
    
            //加入队列 生产者
            private static void Run()
            {
                
                IRedisClientFactory factory = RedisClientFactory.Instance;
                using (IRedisClient client = factory.CreateRedisClient("127.0.0.1", 6379))
                {
                    while (true)
                    {
                        Thread.Sleep(3000);
                        client.EnqueueItemOnList("Myqueue", DateTime.Now.ToString());
                    }
                }
            }
        }
    }
    

      

  • 相关阅读:
    记坑
    常用模板
    ACM-东北赛划水记
    jzoj 4178游戏
    JZOI 4163
    jzoj 4146踩气球
    jzoj 5589. 缩点
    jzoj 5588 %%%
    jzoj 5571 ffs
    BJOI 2017 Kakuro
  • 原文地址:https://www.cnblogs.com/ideacore/p/6867289.html
Copyright © 2011-2022 走看看