官网对ConcurrentHashMap的使用场景的介绍
//{Hashtable} is synchronized. If a
//thread-safe implementation is not needed, it is recommended to use
//{HashMap} in place of {@code Hashtable}. If a thread-safe
//highly-concurrent implementation is desired, then it is recommended
//to use {@link java.util.concurrent.ConcurrentHashMap} in place of
//{@code Hashtable}.
//不考虑并发的情况,使用HashMap()
//考虑低并发的情况,使用Hashtable()
//高并发的情况使用ConcurrentHashMap
为什么HashTable是线程安全的
HashTable能够保证线程安全的原因是,put、get、remove以及size、isEmpty等方法全部都用了Synchronized加同步锁,保证每一次只有一个线程能够对其进行改动。保证了线程安全,但是效率很低。
JDK1.7和JDK1.8的不同实现
JDK1.7里面,ConcurrentHashMap是用Segment和HashEntry实现的,每个Segment都是继承于Reentrantlock的,在对该segment进行操作时,获取锁,结束操作释放锁。
JDK1.8里面,没有用segment,而是用Node+CAS+synchronized实现的。
Node+CAS+synchronized实现
public class ConcurrentHashMap<K,V> extends AbstractMap<K,V>
implements ConcurrentMap<K,V>
//重点在于Concurrent接口中,putIfAbsent,remove,replace方法的实现
//与并发有关的默认值
private static final int DEFAULT_CONCURRENCY_LEVEL = 16;
static final int NCPU = Runtime.getRuntime().availablePr