本文介绍了只读词典 - 多个线程调用.ContainsKey方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个静态的只读字典。 否修改将本字典的。
I have a static readonly dictionary. No modifications will be made to this dictionary.
我有多个线程使用.ContainsKey(关键)这本词典阅读。 。例如
I have multiple threads reading from this dictionary using the .ContainsKey(Key). e.g.
class MyData
{
private static private IDictionary<int, string> _dictionary = new Dictionary<int, string>();
MyData()
{
// Load Dictionary here
}
public string GetValue(int key)
{
if (_dictionary.ContainsKey(key))
{
return _dictionary[key];
}
}
}
是否有任何线程问题这样
Are there any threading problems with doing this?
推荐答案
如果没有人变异它?这是好的。如果有偶尔的编辑,那么也许看看ReaderWriterLockSlim,或(我的偏好)编辑快照/复制和交换的参考。
If nobody is mutating it: this is fine. If there were occasional edits, then perhaps look at ReaderWriterLockSlim, or (my preference) edit a snapshot/copy and swap the reference.
这篇关于只读词典 - 多个线程调用.ContainsKey方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!