关闭

MessageQueue

220人阅读 评论(0) 收藏 举报

using System;
//using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace MQ

{
    public class MessageQueue
    {
        private static LinkedList<string> lls;
        static int msgCount = 10;
        static object obj;

        public static void InitialMessageQueue(int count)
        {
            lls = new LinkedList<string>();
            msgCount = count;
            obj = new object();
        }

        public static bool CheckMsgRemove(string key)
        {
            try
            {
                //return false;
                lock (obj)
                {
                    Console.WriteLine("- Begin Msg Link Count is " + lls.Count.ToString());

                    if (lls.Contains(key))
                    {
                        Console.WriteLine(" * Remove duplicate Key_" + key);
                        lls.Remove(key);

                        Console.WriteLine("- End Msg Link Count is " + lls.Count.ToString());

                        return true;
                    }
                    else
                    {
                        if (lls.Count > msgCount)
                        {
                            Console.WriteLine(" * Remove last Key_" + lls.Last.Value);
                            lls.RemoveLast();
                        }
                        lls.AddFirst(key);
                        Console.WriteLine(" * Add first Key_" + key);
                    }

                    Console.WriteLine("- End Msg Link Count is " + lls.Count.ToString());
                }
                return false;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:42870次
    • 积分:726
    • 等级:
    • 排名:千里之外
    • 原创:26篇
    • 转载:22篇
    • 译文:0篇
    • 评论:5条
    文章分类
    最新评论