首页 > 其他 > 详细

ConcurrentDictionary测试

时间:2020-05-11 21:35:54      阅读:39      评论:0      收藏:0      [点我收藏+]

经过测试发现,多线程操作都是安全的。甚至包括在遍历的时候,进行删除操作

 class Program
    {
        private static readonly ConcurrentDictionary<int, int> Dictionary = new ConcurrentDictionary<int, int>();
        private static readonly List<string> AddSuccessfully = new List<string>();
        private static readonly List<string> RemoveSuccessfully = new List<string>();
        private static readonly List<string> IterateString = new List<string>();
        private static int _tryRemoveCount;
        private static readonly Random Random = new Random();
        static void Main(string[] args)
        {
            try
            {
                int j = 0;
                for (int i = 1; i <= 12; i++)
                {
                    j++;
                    Thread thread;
                    if (j == 1)
                    {
                        thread = new Thread(DoTask1);
                    }
                    else if(j == 2)
                    {
                        thread = new Thread(DoTask2);
                    }
                    else if (j == 3)
                    {
                        thread = new Thread(DoTask3);
                        j = 0;
                    }
                    else
                    {
                        throw new Exception();
                    }
                    thread.IsBackground = false;
                    thread.Start(i);
                }

                Console.WriteLine("======");
                int timeout = 0;
                while (AddSuccessfully.Count != 100)
                {
                    timeout++;
                    Thread.Sleep(1000);
                    if (timeout > 5)
                    {
                        break;
                    }
                }
                
                timeout = 0;
                while (_tryRemoveCount != 5)
                {
                    timeout++;
                    Thread.Sleep(1000);
                    if (timeout > 5)
                    {
                        break;
                    }
                }

                Console.WriteLine($"addSuccessfully.Count = {AddSuccessfully.Count}");
                foreach (var item in AddSuccessfully)
                {
                    Console.WriteLine(item);
                }

                Console.WriteLine($"removeSuccessfully.Count = {RemoveSuccessfully.Count}");
                foreach (var item in RemoveSuccessfully)
                {
                    Console.WriteLine(item);
                }

                Console.WriteLine($"IterateString.Count = {IterateString.Count}");
                foreach (var item in IterateString)
                {
                    Console.WriteLine(item);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            finally
            {
                Console.ReadLine();
            }
        }

        public static void DoTask1(object obj)
        {
            Console.WriteLine($"DoTask1 with {obj} Thread id = {Thread.CurrentThread.ManagedThreadId}");
            for (int i = 0; i < 100; i++)
            {
                var flag = Dictionary.TryAdd(i,i);
                if (flag)
                {
                    AddSuccessfully.Add($"DoTask1 with {obj} add {i} successfully {DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}");
                    //Console.WriteLine();
                }
                Thread.Sleep(1);
            }
        }

        public static void DoTask2(object obj)
        {
            _tryRemoveCount++;
            Console.WriteLine(
                $"DoTask2 with {obj} Thread id = {Thread.CurrentThread.ManagedThreadId}, count = {Dictionary.Count}");
            var toRemove = Random.Next(0, 100);
            for (int i = 0; i < 100; i++)
            {
                var flag = Dictionary.TryRemove(toRemove, out _);
                if (flag)
                {
                    RemoveSuccessfully.Add($"DoTask2 with {obj} remove {toRemove} successfully. {DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}");
                }
                Thread.Sleep(1);
            }
        }

        public static void DoTask3(object obj)
        {
            _tryRemoveCount++;
            Console.WriteLine(
                $"DoTask3 with {obj} Thread id = {Thread.CurrentThread.ManagedThreadId}, count = {Dictionary.Count}");
            foreach (var item in Dictionary)
            {
                IterateString.Add($"DoTask3 with {obj} iterate {item.Key} {DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}");
                Thread.Sleep(2);
            }
        }
    }

 

ConcurrentDictionary测试

原文:https://www.cnblogs.com/chucklu/p/12871201.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!