我怎么感觉 ConcurrentDictionary<,> 不是线程安全的喃?
热敷哥 人气:0直接上代码
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()); } } class Person { public Person() { Console.WriteLine("new Person()"); } }
输出:
WHAT FUCK!
哪位大佬能出来解释下么?有点懵逼啊!
加载全部内容