- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我仅将此代码用作示例。假设我有以下 Person 类。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace dictionaryDisplay
{
class Person
{
public string FirstName { get; private set;}
public string LastName { get; private set; }
public Person(string firstName, string lastName)
{
this.FirstName = firstName;
this.LastName = lastName;
}
public override string ToString()
{
return this.FirstName + " " + this.LastName;
}
}
主程序
static void Main(string[] args)
{
ConcurrentDictionary<int, Person> personColl = new ConcurrentDictionary<int, Person>();
personColl.TryAdd(0, new Person("Dave","Howells"));
personColl.TryAdd(1, new Person("Jastinder","Toor"));
Person outPerson = null;
personColl.TryRemove(0, out outPerson);
//Is this safe to do?
foreach (var display in personColl)
{
Console.WriteLine(display.Value);
}
}
这是遍历并发字典的安全方法吗?如果不是,安全的做法是什么?
假设我想从字典中删除一个 Person 对象。我使用 tryRemove 方法,但我如何处理 outPerson 对象?从字典中删除的 Person 存储在其中。我要如何处理 outPerson 对象才能完全清除它?
最佳答案
is this the safe way of iterating over a concurrent dictionary? If not, what is the safe way for doing it?
是的,它是安全的,因为它不会抛出异常。如果在开始迭代后添加或删除元素,则它们可能会或可能不会包含在迭代中。来自 GetEnumerator
documentation:
The enumerator returned from the dictionary is safe to use concurrently with reads and writes to the dictionary, however it does not represent a moment-in-time snapshot of the dictionary. The contents exposed through the enumerator may contain modifications made to the dictionary after GetEnumerator was called.
下一步:
I use the tryRemove method, but what do I do with the outPerson object?
随心所欲,包括什么都不行。您可以将字典转换为 IDictionary<TKey, TValue>
并调用Remove
, 或者只使用 TryRemove
然后忽略变量:
Person ignored;
dictionary.TryRemove(key, out ignored);
或者您可以使用 C# 7.0 功能 Discards
dictionary.TryRemove(key, out _);
没有“完全清除 [对象]” 的概念 - 如果您没有对它的任何引用,它将被垃圾收集。但无论哪种方式,它都不再存在于字典中(至少通过那个键)。如果您不在代码的其他任何地方使用变量(上面的 ignored
),它不会阻止对象被垃圾回收。
关于c# - 这是在 C# 中迭代 ConcurrentDictionary 的正确方法吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17776422/
我在我的网络应用程序中创建了一个 ConcurrrentDictionary 作为应用程序对象。它在 session 之间共享。 (基本上用作存储库。) 有时,任何可用的 session 都会将新项目
ConcurrentDictionary.TryUpdate 方法需要与具有指定键的元素的值进行比较的 comparisonValue。但是如果我想做这样的事情: if (!_store.TryGet
如果我有一个 ConcurrentDictionary例如,我是否使用 Count 有关系吗?属性(property)或LINQ的Any() ?我宁愿写 dict.Any()而不是 dict.Coun
我正在使用并发字典来存储大约200万条记录,并且想知道如何初始化字典的并发级别。 MSDN页面的示例代码中具有以下注释: concurrencyLevel越高,可以在ConcurrentDiction
我正在使用 ConcurrentDictionary 来存储日志行,当我需要向用户显示它们时,我调用 ToList()生成一个列表。但奇怪的是,一些用户在列表中最先收到最近的行,而从逻辑上讲,他们应该
我发现这个 C# 扩展将 GetOrAdd 转换为 Lazy,我想对 AddOrUpdate 做同样的事情。 有人可以帮我将其转换为 AddOrUpdate 吗? public static cla
我对并发字典如何锁定他们的资源感到困惑。 例如,如果我运行一个方法来迭代字典中的每个项目并在一个线程中编辑它的值,然后我尝试从另一个线程读取一个键的值: 第二个线程会访问字典的快照吗? 如果没有,如果
我找不到有关 ConcurrentDictionary 类型的足够信息,因此我想在这里询问一下。 目前,我使用字典来保存由多个线程(来自线程池,因此没有确切数量的线程)不断访问的所有用户,并且它具有同
我在 Azure 应用服务上使用 MVC 5、ASP.NET 4.7 我正在使用 ConcurrentDictionary 对象来保存数据,以保存对数据源的多次调用。 关于其行为的几个问题: 1) 填
我想使用 ConcurrentDictionary 来检查之前是否添加了这个数据键,但看起来我仍然可以添加之前添加的键。 代码: public class pKeys {
我正在按照教程构建聊天客户端和服务器,现在我遇到了以下错误: Inconsistent accessibility: field type 'System.Collections.Concurrent
我正在考虑将类(单例)与使用 ConcurrentDictionary 实现的集合一起使用。此类将用作缓存实现 (asp.net/wcf)。 您如何看待从此类中显式公开这些集合与仅公开例如3 种方法(
有一个并发字典,它从不同的来源收集信息,每分钟刷新一次并将收集的数据传递给另一个处理程序。 var currentDictionarySnapshot = _currentDictionary; _c
我在 static 类中有一个静态 ConcurrentDictionary。在该类的静态构造函数中,我通过 Task.Run 调用一个私有(private)方法来无限循环遍历字典并删除已过期的项目,
我在 StackOverflow 上阅读了以下文章:ConcurrentBag - Add Multiple Items?和 Concurrent Dictionary Correct Usage但答
我存储这个类 public class Customer { public string Firstname { get; set; } public string Lastname
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 关于您编写的代码问题的问题必须在问题本身中描述具体问题 — 并且包括有效代码 以重现它。参见 SSC
我正在编写一些使用反射的代码。所以我试图将昂贵的反射处理缓存到 ConcurrentDictionary 中。但是,我想对并发字典应用限制,以防止存储旧的和未使用的缓存值。 我做了一些研究以了解如何限
好吧,所以我遇到了一个奇怪的小问题,坦率地说,我没有想法。我想把它扔出去看看我是否遗漏了我做错的东西,或者 ConcurrentDictionary 是否工作不正常。这是代码: (缓存是一个包含静态
我正在尝试使用 ConcurrentDictionary 来帮助完成过滤任务。 如果一个数字出现在列表中,那么我想将一个条目从一个字典复制到另一个。 但这部分的 AddOrUpdate 是不对的 -
我是一名优秀的程序员,十分优秀!