To recap for those .NET gurus who might not know the Java API:
ConcurrentHashMap in Java has atomic methods (i.e. require no external locking) for common Map modification operations such as:
putIfAbsent(K key, V value)
remove(Object key, Object value)
replace(K key, V value)
It also allows iteration over the keyset without locking (it takes a copy at the start of iteration) and get()
operations can generally be interleaved with calls to put()
without blocking (it uses fine grained lock striping IIRC).
Anyway, my question is: does .NET have an equivalent Dictionary implementation?
I guess more generally, I'd be keen to know if .NET has a more general set of thread safe collection libraries. Or concurrency utilities in general - equivalent to Doug Lea's java.util.concurrent
libraries.
HashMap performance is relatively high because it is non-synchronized in nature and any number of threads can perform simultaneously. But ConcurrentHashMap performance is low sometimes because sometimes Threads are required to wait on ConcurrentHashMap.
HashMap allows key and value to be null. ConcurrentHashMap does not allow null key/value. It will throw NullPointerException. HashMap is faster.
In ConcurrentHashMap, at a time any number of threads can perform retrieval operation but for updated in the object, the thread must lock the particular segment in which the thread wants to operate. This type of locking mechanism is known as Segment locking or bucket locking.
ConcurrentHashMap: It allows concurrent access to the map. Part of the map called Segment (internal data structure) is only getting locked while adding or updating the map. So ConcurrentHashMap allows concurrent threads to read the value without locking at all. This data structure was introduced to improve performance.
The incoming .Net 4.0 has a ConcurrentDictionary class, it has a convenient GetOrAdd method.
public TValue GetOrAdd(
TKey key,
Func<TKey, TValue> valueFactory
)
Very useful for global server caches.
EDIT: This was written prior to .NET 4 being released, when obviously there's ConcurrentDictionary
. I'm leaving it here as a reference for those needing .NET 3.5.
I don't know of any equivalent to ConcurrentHashMap
.
In terms of general concurrency utilities - .NET has always provided a bit more than the basics which Java used to provide, in terms of Mutex
, ManualResetEvent
, AutoResetEvent
and ReaderWriterLock
; then more recently (.NET 2.0) Semaphore
and (.NET 3.5) ReaderWriterLockSlim
- as well as the process-wide thread pool, of course.
A bigger shake-up will come in .NET 4.0 when Parallel Extensions arrives - that should make concurrency much simpler. Likewise the Coordination and Concurrency Runtime is finally breaking free of the shackles of the Microsoft Robotics Studio, although I'm not clear on exactly where it's headed (whether it'll be part of .NET itself, or a separate library).
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