Loading

ConCurrentHashMap源码刨析

ConCurrentHashMap的底层是:散列表+红黑树,与HashMap是一样的。

image

image

 

根据上面注释我们可以简单总结:

  • JDK1.8底层是散列表+红黑树
  • ConCurrentHashMap支持高并发的访问和更新,它是线程安全的
  • 检索操作不用加锁,get方法是非阻塞的
  • key和value都不允许为null

JDK1.7底层实现

上面指明的是JDK1.8底层是:散列表+红黑树,也就意味着,JDK1.7的底层跟JDK1.8是不同的~

JDK1.7的底层是:segments+HashEntry数组:

image

 

  • Segment继承了ReentrantLock,每个片段都有了一个锁,叫做“锁分段”

有了Hashtable为啥需要ConCurrentHashMap

  • Hashtable是在每个方法上都加上了Synchronized完成同步,效率低下。
  • ConcurrentHashMap通过在部分加锁和利用CAS算法来实现同步。

CAS算法和volatile简单介绍

在看ConCurrentHashMap源码之前,我们来简单讲讲CAS算法和volatile关键字

CAS(比较与交换,Compare and swap) 是一种有名的无锁算法

CAS有3个操作数

  • 内存值V
  • 旧的预期值A
  • 要修改的新值B

当且仅当预期值A和内存值V相同时,将内存值V修改为B,否则什么都不做

  • 当多个线程尝试使用CAS同时更新同一个变量时,只有其中一个线程能更新变量的值(A和内存值V相同时,将内存值V修改为B),而其它线程都失败,失败的线程并不会被挂起,而是被告知这次竞争中失败,并可以再次尝试(否则什么都不做)

看了上面的描述应该就很容易理解了,先比较是否相等,如果相等则替换(CAS算法)


接下来我们看看volatile关键字,在初学的时候也很少使用到volatile这个关键字。反正我没用到,而又经常在看Java相关面试题的时候看到它,觉得是一个挺神秘又很难的一个关键字。其实不然,还是挺容易理解的~

volatile经典总结:volatile仅仅用来保证该变量对所有线程的可见性,但不保证原子性

我们将其拆开来解释一下:

  • 保证该变量对所有线程的可见性
    • 在多线程的环境下:当这个变量修改时,所有的线程都会知道该变量被修改了,也就是所谓的“可见性”
  • 不保证原子性
    • 修改变量(赋值)实质上是在JVM中分了好几步,而在这几步内(从装载变量到修改),它是不安全的。

 

ConCurrentHashMap域

image

image

ConCurrentHashMap构造方法

ConcurrentHashMap的构造方法有5个:

image.png

具体实现

/**
     * Creates a new, empty map with the default initial table size (16).
     */
    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.
     * @throws IllegalArgumentException if the initial capacity of
     * elements is negative
     */
    public ConcurrentHashMap(int initialCapacity) {
        if (initialCapacity < 0)
            throw new IllegalArgumentException();
        int cap = ((initialCapacity >= (MAXIMUM_CAPACITY >>> 1)) ?
                   MAXIMUM_CAPACITY :
                   tableSizeFor(initialCapacity + (initialCapacity >>> 1) + 1));
        this.sizeCtl = cap;
    }

    /**
     * Creates a new map with the same mappings as the given map.
     *
     * @param m the map
     */
    public ConcurrentHashMap(Map<? extends K, ? extends V> m) {
        this.sizeCtl = DEFAULT_CAPACITY;
        putAll(m);
    }

    /**
     * Creates a new, empty map with an initial table size based on
     * the given number of elements ({@code initialCapacity}) and
     * initial table density ({@code loadFactor}).
     *
     * @param initialCapacity the initial capacity. The implementation
     * performs internal sizing to accommodate this many elements,
     * given the specified load factor.
     * @param loadFactor the load factor (table density) for
     * establishing the initial table size
     * @throws IllegalArgumentException if the initial capacity of
     * elements is negative or the load factor is nonpositive
     *
     * @since 1.6
     */
    public ConcurrentHashMap(int initialCapacity, float loadFactor) {
        this(initialCapacity, loadFactor, 1);
    }

    /**
     * Creates a new, empty map with an initial table size based on
     * the given number of elements ({@code initialCapacity}), table
     * density ({@code loadFactor}), and number of concurrently
     * updating threads ({@code concurrencyLevel}).
     *
     * @param initialCapacity the initial capacity. The implementation
     * performs internal sizing to accommodate this many elements,
     * given the specified load factor.
     * @param loadFactor the load factor (table density) for
     * establishing the initial table size
     * @param concurrencyLevel the estimated number of concurrently
     * updating threads. The implementation may use this value as
     * a sizing hint.
     * @throws IllegalArgumentException if the initial capacity is
     * negative or the load factor or concurrencyLevel are
     * nonpositive
     */
    public ConcurrentHashMap(int initialCapacity,
                             float loadFactor, int concurrencyLevel) {
        if (!(loadFactor > 0.0f) || initialCapacity < 0 || concurrencyLevel <= 0)
            throw new IllegalArgumentException();
        if (initialCapacity < concurrencyLevel)   // Use at least as many bins
            initialCapacity = concurrencyLevel;   // as estimated threads
        long size = (long)(1.0 + (long)initialCapacity / loadFactor);
        int cap = (size >= (long)MAXIMUM_CAPACITY) ?
            MAXIMUM_CAPACITY : tableSizeFor((int)size);
        this.sizeCtl = cap;
    }

通过源码可以发现:

  • 默认初始大小为16
  • 给出指定容量,可以不用过度依赖动态扩容
  • 可以估计并发编程的数量

可以发现,在构造方法中有几处都调用了tableSizeFor(),我们来看一下他是干什么的:

点进去之后发现,啊,原来我看过这个方法,在HashMap的时候…..

image.png

它就是用来获取大于参数且最接近2的整次幂的数

赋值给sizeCtl属性也就说明了:这是下次扩容的大小~

put方法

image

接下来,我们来看看初始化散列表的时候干了什么事:initTable()

image.png

  • 只让一个线程对散列表进行初始化

get方法

从顶部注释我们可以读到,get方法是不用加锁的,是非阻塞的。

我们可以发现,Node节点是重写的,设置了volatile关键字修饰,致使它每次获取的都是最新设置的值

image.png

image.png

总结

上面简单介绍了ConcurrentHashMap的核心知识,还有很多知识点都没有提及到

下面我来简单总结一下ConcurrentHashMap的核心要点:

  • 底层结构是散列表(数组+链表)+红黑树,这一点和HashMap是一样的。
  • Hashtable是将所有的方法进行同步,效率低下。而ConcurrentHashMap作为一个高并发的容器,它是通过部分锁定+CAS算法来进行实现线程安全的。CAS算法也可以认为是乐观锁的一种~
  • 在高并发环境下,统计数据(计算size…等等)其实是无意义的,因为在下一时刻size值就变化了。
  • get方法是非阻塞,无锁的。重写Node类,通过volatile修饰next来实现每次获取都是最新设置的值
  • ConcurrentHashMap的key和Value都不能为null
posted @ 2020-09-14 15:05  _bwang  阅读(254)  评论(0)    收藏  举报