- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
如果字典不包含给定键的值,它会只返回 false 还是会由于线程竞争条件而返回 false,比如另一个线程添加/更新某些东西?
代码中的问题:
ConcurrentDictionary<int, string> cd = new ConcurrentDictionary<int, string>();
// This might fail if another thread is adding with key value of 1.
cd.TryAdd(1, "one");
// Will this ever fail if no other thread ever removes with the key value of 1?
cd.TryRemove(1);
编辑:我认为如果它不包含给定键的值,它只会返回 false,但要绝对确定。
最佳答案
同时 Mitch is right ConcurrentDictionary
不易受竞争条件的影响,我认为您所问问题的答案是肯定的,如果 key 存在,TryRemove
将起作用并返回真
。
在您发布的代码中,TryRemove
不可能返回 false
,因为 cd
是无法在其他任何地方访问的局部变量。但是,如果其他地方的某些代码被赋予了对此 ConcurrentDictionary
对象的引用,并在单独的线程上删除键,则 TryRemove
可能会返回 false
,即使在这里——但只是因为 key 已被删除,而不是因为正在对字典执行某些其他操作并且 key 以某种方式“卡”在那里。
关于c# - ConcurrentDictionary TryRemove 什么时候返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3519557/
这个问题在这里已经有了答案: When will ConcurrentDictionary TryRemove return false (3 个答案) 关闭 5 年前。 我一直在寻找一种从并发字典
这个问题在这里已经有了答案: How to achieve remove_if functionality in .NET ConcurrentDictionary (5 个答案) 关闭 2 年前。
我只想删除一个值。之后我不需要使用该变量。为什么不包括不需要第二个参数的重载? 我真的只需要将它存储在一个临时局部变量中而不使用它,并让垃圾收集器在方法结束时收集它吗?看起来很傻.. 函数:http:
如果字典不包含给定键的值,它会只返回 false 还是会由于线程竞争条件而返回 false,比如另一个线程添加/更新某些东西? 代码中的问题: ConcurrentDictionary cd = ne
我正在将我的F#代码从dotnet3.1迁移到5,并在以下代码中苦苦挣扎: let tryRemove key (dict: Concurrent.ConcurrentDictiona
本文整理了Java中org.apache.flink.runtime.checkpoint.ZooKeeperCompletedCheckpointStore.tryRemove()方法的一些代码示例
我有一个由元素组成的缓存,每个元素都包含两个并发字典,我认为它们可能是我应用程序中内存泄漏的原因。我经常从这本词典中添加和删除内容。有人可以向我描述并发字典的内存分配如何工作以及我的最佳实践是什么吗?
我相信这是可行的,我已经用多个并发线程对其进行了测试(尽管对于竞争条件和死锁没有详尽无遗): public static System.Collections.Concurrent.Concurren
我是一名优秀的程序员,十分优秀!