When does a dictionary throw an IndexOutOfRangeException on Add or ContainsKey?(字典何时在 Add 或 ContainsKey 上引发 IndexOutOfRangeException?)
问题描述
在一个繁忙的 ASP .NET 网站上,我有一个字典,它充当缓存,基本上存储键/值对以供以后检索.
On a busy ASP .NET website, I have a Dictionary, which acts as a cache, basically storing key/value pairs for later retrieval.
在高负载时,字典有时会进入一种状态,每当我调用 ContainsKey 或 Add 方法时,它总是抛出 IndexOutOfRangeException.异常发生在私有 FindEntry 方法中.
On high load, the Dictionary some times get into a state, where it always throws an IndexOutOfRangeException whenever i call the ContainsKey or Add method. The exception happens inside the private FindEntry method.
我怀疑这可能是由于同步问题,但我不确定.
I am suspecting that this might be due to a synchronization issue, but I am not sure.
谁能告诉我在什么情况下会发生这种情况?我的目标是收集足够的信息,以便我可以在开发环境中重现该问题.
Can anyone tell me under which circumstances this can happen ? My goal is to gather enough information so that I can reproduce the issue in the dev environment.
推荐答案
字典 状态:
字典
A Dictionary<TKey,TValue> can support multiple readers concurrently, as long as the collection is not modified. ... To allow the collection to be accessed by multiple threads for reading and writing, you must implement your own synchronization.
如果您没有同步访问 Dictionary,那么您可能会遇到您所描述的问题(可能是因为内部状态不再有效).如果您想尝试在您的开发环境中重现这一点,请尝试创建一个程序,该程序使用多个线程在不同步的情况下从 Dictionary 连续读取和写入.
If you are not synchronizing access to the Dictionary, then you might get issues like those you describe (presumably because the internal state is no longer valid). If you want to try and reproduce this in your development environment, then try creating a program that uses multiple threads to continuously read and write from a Dictionary without synchronization.
这篇关于字典何时在 Add 或 ContainsKey 上引发 IndexOutOfRangeException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:字典何时在 Add 或 ContainsKey 上引发 IndexOutOfRangeException?
基础教程推荐
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- WPF 模态进度窗口 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
