gpt4 book ai didi

.net - 字典何时在 Add 或 ContainsKey 上抛出 IndexOutOfRangeException?

转载 作者:行者123 更新时间:2023-12-02 14:33:10 24 4
gpt4 key购买 nike

在一个繁忙的 ASP .NET 网站上,我有一个字典,它充当缓存,主要存储键/值对以供以后检索。

在高负载下,字典有时会进入一种状态,每当我调用 ContainsKey 或 Add 方法时,它总是抛出 IndexOutOfRangeException。异常发生在私有(private) FindEntry 方法内部。

我怀疑这可能是由于同步问题造成的,但我不确定。

谁能告诉我在什么情况下会发生这种情况?我的目标是收集足够的信息,以便我可以在开发环境中重现该问题。

最佳答案

Dictionary 的文档状态:

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

关于.net - 字典何时在 Add 或 ContainsKey 上抛出 IndexOutOfRangeException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1920864/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com