你不了解的Dictionary和ConcurrentDictionary

最近在做项目时,多线程中使用Dictionary的全局变量时,发现数据并没有存入到Dictionary中,但是程序也没有报错,经过自己的一番排查,发现Dictionary为非线程安全类型,因此我感觉数据没有写进去的原因是多线程在争夺全局变量时,导致数据未写入,于是去对Dictionary进行仔细的了解。
经过在网上查阅资料,发现大家讲解最多的是Dictionary(非线程安全)和ConcurrentDictionary(线程安全),于是我也从这两个关键字来仔细的讲解,顺便也更加深入的认识它们。
Dictionary怎样解决线程安全问题?可以使用加锁、线程全局变量、使用ConcurrentDictionary等。下面我们就一起来看看吧,Let’s go。

Dictionary

Dictionary<TKey, TValue>泛型类提供了键值对映射,通过TKey来检索值的速度非常快,时间复杂度接近与O(1),是因为Dictionary通过哈希表实现,是一种变相的HashTable,采用分离链接散列表的数据结构解决哈希冲突问题。
在早期的C#版本中,可以将集合初始值设定项用于序列样式集合,包括在键值对周围添加括号而得到Dictionary<TKey, TValue>,如:

Dictionary<int, string> msgs = new Dictionary<int, string>()
{
    
    
    {
    
     1, "Hello, "},
    {
    
     2 , "World"},
    {
    
     3, "!"}
};

而新的语法支持使用索引分配到集合中,如:

Dictionary<int, string> MsgErrs = new Dictionary<int, string>()
{
    
    
    [1] = "Hello, ",
    [2] = "World",
    [3] = "!",
};

上述两者在初始化赋值时都差不多,但是两者还是有一些区别,前者在初始化时出现重复key值,程序会直接报错。而后者初始化时,key可以有重复值,系统会自动过滤掉重复的key值,程序也不会报错。
实现键/值对集合
每次对字典的添加都包含一个值与其关联的值,通过使用键来检索十分方便;
如果使用集合初始值设定项生成Dictionary集合,可以使用如下方法:

public static Dictionary<string, Element> BuildDic()
        {
    
    
 return new Dictionary<string, Element>
            {
    
    
                {
    
    "L", new Element(){
    
    Symbol = "L", Name = "Postass", AutominNumber = 9}},
                {
    
    "Q", new Element(){
    
     Symbol = "Q", Name = "Calcium", AutominNumber = 99}},
                {
    
    "M", new Element(){
    
     Symbol = "JY", Name = "JYaoiang", AutominNumber=7924}}
            };
        }

Dictionary添加值

public static void IterateDictionary()
        {
    
    
            Dictionary<string, Element> element = BuildDic();
 foreach(KeyValuePair<string, Element> keyValue in element)
            {
    
    
                Element ele = keyValue.Value;
                Console.WriteLine(string.Format("Key={0}; Values={0};{1};{2}", keyValue.Key, ele.Symbol, ele.Name, ele.AutominNumber));
            }
        }
 public static Dictionary<string, Element> BuildDictionary()
        {
    
    
 var elements = new Dictionary<string, Element>();
            AddDictionary(elements, "L", "LLL", 9);
            AddDictionary(elements, "J", "LJLHHH", 19);
            AddDictionary(elements, "A", "ABABABA", 20);
 return elements;
        }
 public static void AddDictionary(Dictionary<string, Element> elements, string symbol, string name, int num)
        {
    
    
            Element ele = new Element()
            {
    
    
                Symbol = symbol,
                Name = name,
                AutominNumber = num
            };
            elements.Add(key: symbol, value: ele);
        }

ContainsKey方法和Item[]属性

public static void FindDictionary(string symbol)
        {
    
    
            Dictionary<string, Element> elements = BuildDictionary();
 if (elements.ContainsKey(symbol))
            {
    
    
                Element ele = elements[symbol];
                Console.WriteLine("Found: " + ele.Name);
            }
 else
            {
    
    
                Console.WriteLine("Not found " + symbol);
            }
        }

TryGetValue方法

public static void FindDictionaryOfTryGetValue(string symbol)
        {
    
    
            Dictionary<string, Element> elements = BuildDictionary();
            Element ele = null;
 if(elements.TryGetValue(symbol, out ele))
            {
    
    
                Console.WriteLine("Found: " + ele.Name);
            }
 else
            {
    
    
                Console.WriteLine("Not found " + symbol);
            }
        }

在这里讲解了Dictionary的常见使用方法,这里在啰嗦一句,不知道大家在使用Dictionary时有没有注意带Add方法和TryAdd方法,这两个方法到底有什么区别?
我们都知道,在往Dictionary中添加键值时,键是不能重复的,如果使用Add方法添加重复的key,会使程序报错。要想避免这个问题,则可以使用TryAdd方法,当添加重复键值使,该方法会返回false,就可以避免此类问题。

ConcurrentDictionary

.NET Framework 4以及更新的版本中,System.Collections.Concurrent命名空间中的集合可提供高效的线程安全操作,以便从多个线程访问集合项。
当有多个线程访问集合项时,应该使用System.Collections.Concurrent命名空间中的类,而不是使用System.Collections.Generic和System.Collections命名空间中的类。
System.Collections.Concurrent命名空间中的类:BlockingCollection、ConcurrentDictionary<TKey, TValue>、ConcurrentQueue<T>、ConcurrentStack<T>
System.Collections命名空间中的类不会将元素作为特别类型化的对象存储,而是作为object类型的对象存储。
ConcurrentDictionary用法与Dictionary类似,这里就不再详细讲解了。但是ConcurrentDictionary只能使用TryAdd方法,而Dictionary可以使用AddTryAdd方法。

Dictionary和ConcurrentDictionary多线程

带大家认识完DictionaryConcurrentDictionary,下面就回归主题,看看两者在多线程方面的使用情况。
代码如下:

ConcurrentDictionary<int, string> keys = new ConcurrentDictionary<int, string>();
keys.TryAdd(1, "LL");
keys.TryAdd(2, "LL");
Dictionary<int, string> dic = new Dictionary<int, string>();
dic.Add(1, "OJ");
dic.TryAdd(2, "R");
Stopwatch stopwatch = new Stopwatch();
#region 写入
stopwatch.Start();
Parallel.For(0, 10000000, i =>
{
    
    
 lock (dic)
    {
    
    
        dic[i] = new Random().Next(100, 99999).ToString();
    }
});
stopwatch.Stop();
Console.WriteLine("Dictionary加锁写入花费时间:{0}", stopwatch.Elapsed);
stopwatch.Restart();
Parallel.For(0, 10000000, i =>
{
    
    
    keys[i] = new Random().Next(100, 99999).ToString();
});
stopwatch.Stop();
Console.WriteLine("ConcurrentDictionary加锁写入花费时间:{0}", stopwatch.Elapsed);
#endregion
#region 读取
string result = string.Empty;
stopwatch.Restart();
Parallel.For(0, 10000000, i =>
{
    
    
 lock (dic)
    {
    
    
        result = dic[i];
    }
});
stopwatch.Stop();
Console.WriteLine("Dictionary加锁读取花费时间:{0}", stopwatch.Elapsed);
stopwatch.Restart();
Parallel.For(0, 10000000, i =>
{
    
    
    result = keys[i];
});
stopwatch.Stop();
Console.WriteLine("ConcurrentDictionary加锁读取花费时间:{0}", stopwatch.Elapsed);
#endregion
Console.ReadLine();

v
​

可以发现,在多线程下,加了lockDictionary写入性能要比ConconcurrentDictionary的写入性能更好,读取数据ConcurrentDictionary性能更好。
当我们将写入的数据增加到20000000时,ConcurrentDictionary写入性能明显就比Dictionary性能差了,但是读取性能ConcurrentDictionary更好。

编辑

当我们将写入的数据增加到2000000时,ConcurrentDictionary写入性能还是比Dictionary性能差,但是读取性能ConcurrentDictionary更好。

编辑

综上,经过对两者的比较,ConcurrentDictionary读取性能更好,Dictionary写入性能更好。
至于,具体是什么原因,到时候我会进行深入讲解,这篇文章大致就讲到这里了,我们下篇文章见。

猜你喜欢

转载自blog.csdn.net/shanniuliqingming/article/details/132218309