Preliminary knowledge: The implementation mechanism of Java HASHMAP and HashSet
The storage structure of the HashMap can be known by the preliminary knowledge:
(Image from http://www.ibm.com/developerworks/cn/java/j-lo-hash/)
Also said: A hashmap contains an array of entity class rows, and the elements in this array are entity. In fact, the key and value we put in the map correspond to a Entity object that contains a key, value, Hashcode (key), and a Entity reference, through which Entity can form a list. In the diagram, the blue rectangular box represents the array, and the orange ellipse represents the entity object.
Note that the HashMap class is not thread-safe.
the main subclass of Concurrentmap is Concurrenthashmap.
Principle: A concurrenthashmap consists of multiple segment, each segment containing an array of entity. There is a segment class more than HashMap. This class inherits the Reentrantlock class, so itself is a lock. When multithreading to Concurrenthashmap operation, not completely lock map, but lock the corresponding segment. This increases the concurrency efficiency.
Analysis of Constructors:
/** * Creates a new, empty map with a default initial capacity (), * load factor (0.75) and Concurrencylevel (16). * * Public Concurrenthashmap () {This (default_initial_capacity, Default_load_factor, default_concurrency_level);}
This is the Concurrenthashmap constructor for the default size, which can be seen as 16, load factor 0.75, and concurrency level 16.
Analysis of the PUT function:
/** * Maps The specified key to the specified value of this table. * Neither the key nor the value can be null. * * <p> The value can be retrieved by calling the <tt>get</tt> method * with a key, which equal to the Original key. * * @param key key with which the specified value being associated * @param value value to being associated with the Speci Fied Key * @return The previous value associated with <tt>key</tt>, or * <tt>null</tt> if there WA s no mapping for <tt>key</tt> * @throws nullpointerexception if the specified key or value is NULL */public V Put (K key, v. value) {if (value = = null) throw new NullPointerException (); int hash = hash (Key.hashcode ()); return segmen Tfor (hash). Put (key, hash, value, false); }
Can be seen through the hash () function to get the hash value of the key, in the corresponding segment, in the segment storage entity.
Also to avoid concurrency problems such as "Detect and modify" (conditional thread safety See [2]), Concurrenthashmap provides a putifabsent (K key, V value) method that is added when a key does not exist. (Key's existence depends on two conditions, one is the key Hashcode method, the other is the key equal method)
Advantage: Concurrency is improved by locking the corresponding segment instead of locking the entire map. The ability to directly increase the scalability of inserts, retrievals, and removal operations.
Disadvantage: When traversing the elements in a map, you need to get all the segment locks, which are slow to use. The increase in locks occupies the resources of the system. The implementation of some of the methods that operate on the entire collection, such as size () or IsEmpty (), is more difficult because these methods require a lot of locks to be acquired at a time, and there is a risk of returning incorrect results.
Resources:
1 study the Hash storage mechanism by analyzing the JDK source code
2 Java Theory and Practice: Concurrent collection Classes
3 Java Theory and Practice: building a better HashMap