首页 新闻 会员 周边

c# ConcurrentDictionary和ConcurrentQueue怎么使用?

0
悬赏园豆:5 [已解决问题] 解决于 2019-12-23 13:16


上图是我的数据结构。我定义了一个线程安全的静态变量(作用:消息队列)用来存储消息,如下:

private static ConcurrentDictionary<string, ConcurrentQueue<SignalModel>> _dic = new ConcurrentDictionary<string, ConcurrentQueue<SignalModel>>();

既用到了ConcurrentDictionary又用到了ConcurrentQueue。用字典是因为要根据IP分组存储。

我就是不知道用了ConcurrentDictionary之后,字典的value是否还有必要使用ConcurrentQueue来定义。

是否应该将ConcurrentQueue改成List?

< >
分享
最佳答案
0

参考 Concurrent collections is it safe to modify from different threads

ConcurrentQueue is only ensuring that calls to methods of that class are observed to be atomic; it is making no other guarantees surrounding thread safety.

收获园豆:5
dudu | 高人七级 |园豆:30979 | 2019-12-21 14:44


根据你发的引用文字和上面的截图,我得出结论,就是还得用ConcurrentDictionary<string, ConcurrentQueue<SignalModel>>类型来定义。是这样的吗?

屌丝大叔的笔记 | 园豆:2 (初学一级) | 2019-12-21 19:53

@屌丝大叔的笔记: 要根据你的应用场景

dudu | 园豆:30979 (高人七级) | 2019-12-21 20:40

@dudu: 是的,很符合,谢谢!

屌丝大叔的笔记 | 园豆:2 (初学一级) | 2019-12-23 10:30
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册