我什么时候应该使用ConcurrentDictionary和Dictionary? [英] When should I use ConcurrentDictionary and Dictionary?

查看:222
本文介绍了我什么时候应该使用ConcurrentDictionary和Dictionary?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我总是很困惑选择哪一个。如我所见,如果我想将两种数据类型用作 Key,则在 List 上使用 Dictionary Value ,所以我可以通过其轻松找到一个值,但我总是很困惑如果我应该使用 ConcurrentDictionary Dictionary

I'm always confused on which one of these to pick. As I see it I use Dictionary over List if I want two data types as a Key and Value so I can easily find a value by its key but I am always confused if I should use a ConcurrentDictionary or Dictionary?

在您对我没有做太多研究之前,我尝试过,但是看来Google在 Dictionary 上并没有真正的用处ConcurrentDictionary ,但每一个都有单独的内容。

Before you go off at me for not putting much research in to this I have tried, but it seems google hasn't really got anything on Dictionary vs ConcurrentDictionary but has something on each one individually.

我之前曾问过一个朋友,但他们所说的只是:如果使用字典,请使用 ConcurrentDictionary 大量的代码,而我真的不想让他们费心去详细解释它。有人可以对此进行扩展吗?

I have asked a friend this before but all they said is: "use ConcurrentDictionary if you use your dictionary a lot in code" and I didn't really want to pester them in to explaining it in larger detail. Could anyone expand on this?

推荐答案

如果您在代码中大量使用字典,请使用ConcurrentDictionary。是一种模糊的建议。

"Use ConcurrentDictionary if you use your dictionary a lot in code" is kind of vague advice. I don't blame you for the confusion.

ConcurrentDictionary 主要用于您要从中更新字典的环境中。多线程(或异步任务)。您可以使用尽可能多的代码中的标准 Dictionary (如果它来自单个线程;)

ConcurrentDictionary is primarily for use in an environment where you're updating the dictionary from multiple threads (or async tasks). You can use a standard Dictionary from as much code as you like if it's from a single thread ;)

ConcurrentDictionary上的方法,您会发现一些有趣的方法,例如 TryAdd TryGetValue TryUpdate TryRemove

If you look at the methods on a ConcurrentDictionary, you'll spot some interesting methods like TryAdd, TryGetValue, TryUpdate, and TryRemove.

例如,考虑使用正常模式时可能会看到的典型模式 Dictionary 类。

For example, consider a typical pattern you might see for working with a normal Dictionary class.

// There are better ways to do this... but we need an example ;)
if (!dictionary.ContainsKey(id))
    dictionary.Add(id, value);

在检查是否包含键与调用 Add 另一个线程可以使用相同的 id 调用 Add 。当该线程调用 Add 时,它将引发异常。方法 TryAdd 为您处理该问题,并返回true / false,告诉您是否添加了它(或该键是否已在字典中)。

This has an issue in that between the check for whether it contains a key and calling Add a different thread could call Add with that same id. When this thread calls Add, it'll throw an exception. The method TryAdd handles that for you and will return a true/false telling you whether it added it (or whether that key was already in the dictionary).

因此,除非您在代码的多线程部分中进行工作,否则您可能只可以使用标准的 Dictionary 类。话虽这么说,理论上您可以使用锁来防止对字典的并发访问。该问题已经在字典锁定与ConcurrentDictionary中得到解决。

So unless you're working in a multi-threaded section of code, you probably can just use the standard Dictionary class. That being said, you could theoretically have locks to prevent concurrent access to a dictionary; that question is already addressed in "Dictionary locking vs. ConcurrentDictionary".

这篇关于我什么时候应该使用ConcurrentDictionary和Dictionary?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆