开发者

How ConcurrentDictionary is accessed and how to serialize it?

开发者 https://www.devze.com 2023-04-06 18:30 出处:网络
I\'ve never used the ConcurrentDictionary object before and have a couple questions about it: Am I correct that multiple threads can read from the dictionary at the same time, but if it\'s being wri

I've never used the ConcurrentDictionary object before and have a couple questions about it:

  1. Am I correct that multiple threads can read from the dictionary at the same time, but if it's being written to, no other thread can access it?

  2. Can this o开发者_如何学运维bject be serialized to disk?

Thanks.


Am I correct that multiple threads can read from the dictionary at the same time, but if it's being written to, no other thread can access it?

No, you can safely read and write from multiple threads. Of course internally I suppose that there is some synchronization happening but the performance penalty should be small and you shouldn't be worried about it and do any additional synchronization.

Can this object be serialized to disk?

Depends on what serializer you use.

  • JavaScriptSerializer: yes
  • Json.NET: yes
  • DataContractSerializer: yes
  • BinaryFormatter: yes
  • XmlSerializer: no (well, you could do some hacking to make it work but it will be a PITA => XmlSerializer is allergic to the IDictionary<TKey, TValue> interface)


  1. Am I correct that multiple threads can read from the dictionary at the same time, but if it's being written to, no other thread can access it?

This is not observable, you can read and write from multiple threads and let the class worry about the synchronization.

  1. Can this object be serialized to disk?

Yes, it is marked with [Serializable]. And you can always extract the <K,V> pairs and use any Serializer you like.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号