Hashmap/Hashtable/ConcurentHashmap/synchronizedMap

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

HashtableProvides a thread safe and easy to use, the correlation function, map. However, thread safety is with the cost -- HashtableAll methods are synchronized. Synchronized isAccording to the Hash tableThe whole table locks, where each thread exclusively, security is behind the huge waste.

HashMapProvide an unsynchronized base and a synchronized wrapperCollections.synchronizedMap (corresponding to List and Collections.synchronizedList, more safety to use CopyOnWriteArrayList), The thread safety issues. Separating the base functionality from the thread safety, Collections.synchronizedMapThread safety conditions-All individual operations are thread safe, but the operation sequence of a plurality of operating components may lead to data contention, because in the sequence of operation in the control flow depends on the previous operation result. The need to allow for simultaneous users can have synchronization, without the need to synchronize users don't have to pay the price of synchronization.

ConcurrentHashMapAnd the main difference between Hashtable is around the lock granularity and how to lock.

On the left is the realization of the hash - lock table Hashtable; and on the right is the way to realize the lock barrel - ConcurrentHashMap (or segments). The ConcurrentHashMap hash table will be divided into 16 barrels (the default), such as get, put, remove and other commonly used operation only lock the need to use the bucket. The original only one thread to enter, can now also 16 write thread enters, concurrency is obviously improve the. And ConcurrentHashMap read concurrency, because when most read were not used to lock, so read operation is almost completely concurrent operation.


Reference resources:

http://www.blogjava.net/zlsunnan/archive/2006/07/02/56184.html

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Cherry at May 22, 2014 - 9:29 AM