ConcurrentHashMap 是 Map 的派生类,所以 api 基本和 Hashmap 是类似,主要就是 put、get 这些方法 public static void main(String[] args) { ConcurrentHashMap<String, Object> map = new ConcurrentHashMap<>(); map.put("1", "1"); map.put("2", "2"); Object value = map.get("1"); Syst...
ConcurrentHashMap类中包含两个静态内部类 HashEntry 和 Segment,其中 HashEntry 用来封装具体的K/V对,是个典型的四元组;Segment 用来充当锁的角色,每个 Segment 对象守护整个ConcurrentHashMap的若干个桶 (可以把Segment看作是一个小型的哈希表),其中每个桶是由若干个 HashEntry 对象链接起来的链表。总的来说,一个...
ConcurrentHashMap类中包含两个静态内部类 HashEntry 和 Segment,其中 HashEntry 用来封装具体的K/V对,是个典型的四元组;Segment 用来充当锁的角色,每个 Segment 对象守护整个ConcurrentHashMap的若干个桶 (可以把Segment看作是一个小型的哈希表),其中每个桶是由若干个 HashEntry 对象链接起来的链表。总的来说,一个...
new一个默认table容量为16的ConcurrentHashMap*/publicConcurrentHashMap(){}/*** Creates a new,...
*/ public ConcurrentHashMap() { }/** * Creates a new, empty map with an initial table size * accommodating the specified number of elements without the need * to dynamically resize. * * @param initialCapacity The implementation performs internal * sizing to accommodate...
*@paraminitialCapacity The implementation performs internal * sizing to accommodate this many elements. *@throwsIllegalArgumentException if the initial capacity of * elements is negative*/publicConcurrentHashMap(intinitialCapacity) {this(initialCapacity, LOAD_FACTOR, 1); ...
前两个参数比较容易理解;并发级别表示分片(shard)的数量,用于在ConcurrentHashMap内部分为相应的分区,同时相同数量的线程被创建,用于在分片级别保证线程安全。 concurrencyLevel的默认值为16。这意味着我们只要使用默认构造函数创建一个ConcurrentHashMap时,就会创建16个分片——在我们向map中加入任何键值对之前。它同时意味...
*/ public ConcurrentHashMap() { } /** * Creates a new, empty map with an initial table size * accommodating the specified number of elements without the need * to dynamically resize. * * @param initialCapacity The implementation performs internal * sizing to accommodate this many elements. ...
public ConcurrentHashMap(int initialCapacity) Creates a new, empty map with an initial table size accommodating the specified number of elements without the need to dynamically resize. Parameters: initialCapacity - The implementation performs internal sizing to accommodate this many elements. Throws: Illeg...
使用ConcurrentHashMap最长用的也应该是put和get方法了吧,我们先来看看put方法是怎样实现的。调用put方法时实际具体实现是putVal方法,源码如下:/** Implementation for put and putIfAbsent */final V putVal(K key, V value, boolean onlyIfAbsent) {if (key == null || value == null) thrownew Null...