29°

我怎么感觉 ConcurrentDictionary<,> 不是线程安全的喃?

直接上代码

    class Program
    {
        static readonly ConcurrentDictionary<string, Person> Dic = new ConcurrentDictionary<string, Person>();
        static void Main(string[] args)
        {
            for (int i = 0; i < 500; i++)
            {
                Task.Run(() => { GetPerson("wjire"); });
            }
            Console.ReadKey();
        }
        private static Person GetPerson(string key)
        {
            return Dic.GetOrAdd(key, k => new Person());
        }
    }
</span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Person
{
    </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> Person()
    {
        Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">new Person()</span><span style="color: #800000;">"</span><span style="color: #000000;">);
    }
}</span></pre> 

 

输出:

 

WHAT FUCK!

哪位大佬能出来解释下么?有点懵逼啊!

本文转载自博客园,原文链接:https://www.cnblogs.com/refuge/p/12207927.html

全部评论: 0

    我有话说: