How ConcurrentDictionary is accessed and how to serialize it?
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 written to, no other thread can access it?
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 theIDictionary<TKey, TValue>
interface)
- 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.
- 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.
精彩评论