在.NET4.0以前,在多线程环境下如果我们使用Dictionary类,只能自己编码实现线程同步来保证线程安全,别无他选。在.NET4.0以后微软替我们开发了ConcurrentDictionary类,如果你使用字典遇到线程安全的问题,那么就使用ConcurrentDictionary类,不用再考虑“Dictionary+Locks”的问题了。
下面我们对比来看看加锁后的Dictionary和ConcurrentDictionary的使用情况,是否解决了问题下,性能如何呢?.
string res = "";
var concurrentDictionary = new ConcurrentDictionary<int, string>();
var dictionary = new Dictionary<int, string>();
dictionary.TryAdd(1, "aa");
concurrentDictionary.TryAdd(1,"");
var sw = new Stopwatch();
sw.Start();
Parallel.For(0, 1000000, i =>//Parallel使用并行循环
{
lock (dictionary)
{
dictionary[i] = new Random().Next(1000, 9999).ToString();
}
});
sw.Stop();
Console.WriteLine("加锁写入dictionary {0}", sw.Elapsed);
//wrinting to dictionary with a lock: 00:00:00.0633939
sw.Restart();
Parallel.For(0, 1000000, i =>
{
concurrentDictionary[i] = new Random().Next(1000, 9999).ToString();
});
sw.Stop();
Console.WriteLine("concurrentdictionary写入时长 : {0}", sw.Elapsed);
sw.Restart();
Parallel.For(0, 1000000, i =>
{
lock (dictionary)
{
res = dictionary[i];
}
});
sw.Stop();
Console.WriteLine("读取dictionary耗时: {0}", sw.Elapsed);
sw.Restart();
Parallel.For(0, 1000000, i =>
{
res = concurrentDictionary[i];
});
sw.Stop();
Console.WriteLine("读取concurrentdictionary耗时: {0}", sw.Elapsed);
Console.ReadLine();
结果如下
ConcurrentDictionary和Dictionary运行结果显示,读取写入的效率都是ConcurrentDictionary胜出,完美,也未出现线程安全的问题。注意环境关系可能略有差距。据说在多核多线程的情况下concurrentDictionary将有更好的性能表现。
面试官也可能问Dictionary怎么解决线程安全问题,如果答出使用ConcurrentDictionary一定是加分项。
注意,有网友指出ConcurrentDictionary使用依然会有线程安全问题,楼主目前没有发现,大家可以自己试一试。
dictionary.TryAdd(1, "aa");
concurrentDictionary.TryAdd(1,"");