我正在将对象添加/更新到并发字典中并定期(每分钟)刷新字典,因此我的代码如下所示:
private static ConcurrentDictionary<string, Metric> _metrics = new ConcurrentDictionary<string, Metric>();
public static void IncrementCountMetricBy(string name, int count)
{
_metrics.AddOrUpdate(....
}
public static Metric[] Flush()
{
var flushedMetrics = _metrics;
_metrics = new ConcurrentDictionary<string, Metric>();
return flushedMetrics.Values.ToArray();
}
现在我不确定这段代码是否有可能丢失一些对象/更新