gpt4 book ai didi

c# - 在修改值时循环遍历 ConcurrentDictionary

转载 作者:行者123 更新时间:2023-11-30 17:29:31 33 4
gpt4 key购买 nike

我在 static 类中有一个静态 ConcurrentDictionary。在该类的静态构造函数中,我通过 Task.Run 调用一个私有(private)方法来无限循环遍历字典并删除已过期的项目,即自添加以来已经过去了很长时间,并且因此需要删除(这是通过检查字典中每个项目的每个 CustomItem 值的属性来确定的):

public static class ItemsHolder
{
public static ConcurrentDictionary<Guid, CustomItem> Items = new ConcurrentDictionary<Guid, CustomItem>();

// ...

static ItemsHolder
{
Task.Run(() => DeleteExpired());
}

private static async Task DeleteExpired()
{
while (true)
{
// check if dictionary is empty and if yes,
// await Task.Delay a bit, etc - omitted for brevity

foreach (var item in Items)
{
var guid = item.Key;
var customItem = item.Value;

if (customItem.ConditionToRemoveItemFromDictionaryHere)
{

var removeItem = Items.TryRemove(guid, out _);

// ...
}
}
}
}
}

在代码的其他部分(此类之外),项目正在根据其他条件添加到此字典中并删除。有效地 DeleteExpired() 用于清除无法从字典中删除的项目(在代码的其他部分,出于 XYZ 原因)

我看到 DeleteExpired() 在我的指标中不断弹出,例如 R# 中的 dotTrace 占用 CPU 时间。我知道在其他线程可能正在添加/删除的同时遍历并发字典是无锁的,应该是安全的,但我不确定这是否是最有效的方法。有没有更有效/更高效的方法?

最佳答案

如果字典不为空,您就不会在每次迭代之间等待。

while (true) 使线程疯狂地旋转并且没有休息(字面意思)。您必须在每次检查之间绝对等待。

如果没有延迟或休眠,一个线程将持续消耗你的 CPU 的整个核心(或者至少它会尝试这样做),这就是性能异常的原因。

我会这样写:

while(true) {
Items.Where(pair => pair.Value.ConditionToRemoveItemFromDictionaryHere)
.Select(pair => pair.Key)
.ToList()
.ForEach(guid => Items.TryRemove(guid, out _));

// thread.sleep, task delay or whatever suits your needs
}

关于c# - 在修改值时循环遍历 ConcurrentDictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50964745/

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