ConcurrentHashMap集合了hashmap和hashtable 这两张表的优势:
hashtable 每次操作都会锁住整个结构
ConcurrentHashMap 锁的方式是稍微细粒度
把数据分为了16个桶,每次get put remove 等操作,只需要对对应的桶进行操作就可以了;
读取并发,因为在读取的大多数时候都没有用到锁定,写操作锁定的粒度又非常细,所以,其并发性能大大提升了;
ConcurrentHashMap采用重入锁,对segment 进行加锁。
保证每一个segment是线程安全,这样就保证了整个concurrentHashmap是线程安全的。
public ConcurrentHashMap(int initialCapacity,
float loadFactor, int concurrencyLevel) {
if (!(loadFactor > 0) || initialCapacity < 0 || concurrencyLevel <= 0)
throw new IllegalArgumentException();