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 object be serialized to disk?
Thanks.
To retrieve single item, ConcurrentDictionary provides TryGetValue method. We have to provide Key in the TryGetValue method. It takes the out parameter to return the value of key. TryGetValue returns true if key exists, or returns false if key does not exists in dictionary.
Concurrent. ConcurrentDictionary<TKey,TValue>. This collection class is a thread-safe implementation. We recommend that you use it whenever multiple threads might be attempting to access the elements concurrently.
ConcurrentDictionary<TKey, TValue> Class Represents a thread-safe collection of key-value pairs that can be accessed by multiple threads concurrently.
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.
XmlSerializer
is allergic to the IDictionary<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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With