views:

253

answers:

2

what is the use of concurrent hash map in java? where we have to use this? what are the comforts in it? help please. how it works? sample codes are easy to understandable..

+5  A: 

The point is to provide an implementation of HashMap that is threadsafe. Multiple threads can read from and write to it without the chance of receiving out-of-date or corrupted data. ConcurrentHashMap provides its own synchronization, so you do not have to synchronize accesses to it explicitly.

Another feature of ConcurrentHashMap is that it provides the putIfAbsent method, which will atomically add a mapping if the specified key does not exist. Consider the following code:

ConcurrentHashMap<String, Integer> myMap = new ConcurrentHashMap<String, Integer>();

// some stuff

if (!myMap.contains("key")) {
  myMap.put("key", 3);
}

This code is not threadsafe, because another thread could add a mapping for "key" between the call to contains and the call to put. The correct implementation would be:

myMap.putIfAbsent("key", 3);
danben
I would describe it more as "safely unsynchronized." It allows two threads to be inside mucking about at the same time and promises to end up in a consistent state afterward.
Affe
Synchronization is not an (external) ordering guarantee.
danben
It is not a guarantee that threads requesting the monitor will receive it in the order requested, but it is a happens-before guarantee that once one thread acquires the monitor and starts modifying the map, no one else will see it until the modification is done. In the Concurrent Map one thread could start a put, then not get scheduled again for a long time and other threads can execute gets that won't see the in-progress put.
Affe
+1  A: 

Really the big functional difference is it doesn't throw an exception and/or end up corrupt when someone else changes it while you're using it.

With regular collections, if another thread adds or removes an element while you're access it (via the iterator) it will throw an exception. ConcurrentHashMap lets them make the change and doesn't stop your thread.

Mind you it does not make any kind of synchronization guarantees or promises about the point-in-time visibility of the change from one thread to the other. (It's sort of like a read-committed database isolation, rather than a synchronized map which behaves more like a serializable database isolation. (old school row-locking SQL serializable, not Oracle-ish multiversion serializable :) )

The most common use I know of is in caching immutable derived information in App Server environments where many threads may be accessing the same thing, and it doesn't really matter if two happen to calculate the same cache value and put it twice because they interleave, etc. (e.g., it's used extensively inside the Spring WebMVC framework for holding runtime-derived config like mappings from URLs to Handler Methods.)

Affe
This is not true at all. The javadocs specify explicitly that all operations are threadsafe.
danben
All operations are threadsafe, but there is no happens before promise like there would be with a synchronized map. What you see when you look in a ConcurrentHashMap is the results of the most recently completed operations. Some of which may have begun significantly later in time than when you started trying to look. It works rather like a read-committed database isolation, whereas a synchronized map works more like a serializable database isolation.
Affe