Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does .NET have a Dictionary implementation that is equivalent to Java's ConcurrentHashMap?

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.

like image 500
serg10 Avatar asked Nov 12 '08 09:11

serg10


People also ask

Which is better HashMap or ConcurrentHashMap?

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.

Which is faster HashMap or ConcurrentHashMap?

HashMap allows key and value to be null. ConcurrentHashMap does not allow null key/value. It will throw NullPointerException. HashMap is faster.

How is ConcurrentHashMap locking implemented?

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.

What is ConcurrentHashMap give me the internal implementation of ConcurrentHashMap?

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.


2 Answers

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.

like image 62
Olmo Avatar answered Oct 30 '22 20:10

Olmo


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).

like image 38
Jon Skeet Avatar answered Oct 30 '22 20:10

Jon Skeet