ConcurrentHashMap 锁分段 源码分析

看ConcurrentHashMap下几个属性:

/**
     * The default concurrency level for this table, used when not
     * otherwise specified in a constructor.
     * 默认的分段锁个数
     */
    static final int DEFAULT_CONCURRENCY_LEVEL = 16;

     /**
     * The minimum capacity for per-segment tables.  Must be a power
     * of two, at least two to avoid immediate resizing on next use
     * after lazy construction. 每个分段锁,最小容量
     */
    static final int MIN_SEGMENT_TABLE_CAPACITY = 2;


     /**
     * 尝试获取锁的次数
     * Number of unsynchronized retries in size and containsValue
     * methods before resorting to locking. This is used to avoid
     * unbounded retries if tables undergo continuous modification
     * which would make it impossible to obtain an accurate result.
     */
    static final int RETRIES_BEFORE_LOCK = 2;

    /**
     * The segments, each of which is a specialized hash table.
     * 分段锁数组 
     */
    final Segment<K,V>[] segments;
/**
     * Segments are specialized versions of hash tables.  This
     * Segments 也是一个定制的hashtable
     * subclasses from ReentrantLock opportunistically, just to
     * 同时他也是 ReentrantLock的子类
     * simplify some locking and avoid separate construction.
     */
    static final class Segment<K,V> extends ReentrantLock implements Serializable {
    .....
    }
  //以put为例分析

 /**
     * Maps the specified key to the specified value in this table.
     * Neither the key nor the value can be null.
     * key 和 value 都不能为null 否则抛异常
     * <p> The value can be retrieved by calling the <tt>get</tt> method
     * with a key that is equal to the original key.
     *
     * @param key key with which the specified value is to be associated
     * @param value value to be associated with the specified key
     * @return the previous value associated with <tt>key</tt>, or
     *         <tt>null</tt> if there was no mapping for <tt>key</tt>
     * @throws NullPointerException if the specified key or value is null
     */
    @SuppressWarnings("unchecked")
    public V put(K key, V value) {
        Segment<K,V> s;
        if (value == null)
            throw new NullPointerException();
        int hash = hash(key);//先通过key求hash,再获取当前hash在哪个分段锁内,这些全是位操作,比较烦,也能分析透,还有UNSAFE类的使用比较多。UNSAFE是高危操作类,但是高效,功能强大。
        int j = (hash >>> segmentShift) & segmentMask;
        if ((s = (Segment<K,V>)UNSAFE.getObject          // nonvolatile; recheck
             (segments, (j << SSHIFT) + SBASE)) == null) //  in ensureSegment
            s = ensureSegment(j);//没找到合适分段,调用ensureSegment() 看下面,
        return s.put(key, hash, value, false);//调用的分段锁Segment的put方法
    }


    /**
     * Returns the segment for the given index, creating it and
     * recording in segment table (via CAS) if not already present.
     *  返回给定的索引的分段,不存在就创建一个。
     * @param k the index
     * @return the segment
     */
    @SuppressWarnings("unchecked")
    private Segment<K,V> ensureSegment(int k) {
        final Segment<K,V>[] ss = this.segments;
        long u = (k << SSHIFT) + SBASE; // raw offset
        Segment<K,V> seg;
        if ((seg = (Segment<K,V>)UNSAFE.getObjectVolatile(ss, u)) == null) {//通过索引没取到分段,创建分段
            Segment<K,V> proto = ss[0]; // use segment 0 as prototype  //总是以ss[0]为例 这点写死了
            int cap = proto.table.length;
            float lf = proto.loadFactor;
            int threshold = (int)(cap * lf);
            HashEntry<K,V>[] tab = (HashEntry<K,V>[])new HashEntry[cap];
            if ((seg = (Segment<K,V>)UNSAFE.getObjectVolatile(ss, u))
                == null) { // recheck//再检查一次,有没有合适的分段
                Segment<K,V> s = new Segment<K,V>(lf, threshold, tab);//创建分段
                while ((seg = (Segment<K,V>)UNSAFE.getObjectVolatile(ss, u))
                       == null) { //再检查一次,有没有合适的分段  第三次检查,
                    if (UNSAFE.compareAndSwapObject(ss, u, null, seg = s))//最后用cas方法,把新建的分段放到分段数组中
                        break;
                }
            }
        }
        return seg;
    }

    //  关键看下Segment类 
      /**
     * Segments are specialized versions of hash tables.  This 
     * subclasses from ReentrantLock opportunistically, just to
     * simplify some locking and avoid separate construction.
     * 它是ReentrantLock的子类
     */
      static final class Segment<K,V> extends ReentrantLock implements Serializable {}
	
     //看下Segment的put方法
      final V put(K key, int hash, V value, boolean onlyIfAbsent) {
            HashEntry<K,V> node = tryLock() ? null :
                scanAndLockForPut(key, hash, value);//先用tryLock()获取锁,如果成功node=null ,否则进入scanAndLockForPut(key, hash, value)方法,看下面scanAndLockForPut方法
            V oldValue;
            try {
                HashEntry<K,V>[] tab = table;
                int index = (tab.length - 1) & hash;
                HashEntry<K,V> first = entryAt(tab, index);
                for (HashEntry<K,V> e = first;;) {
                    if (e != null) {//key 的hash位置有值了
                        K k;
                        if ((k = e.key) == key ||
                            (e.hash == hash && key.equals(k))) {
                            oldValue = e.value;
                            if (!onlyIfAbsent) {
                                e.value = value;
                                ++modCount;
                            }
                            break;
                        }
                        e = e.next;
                    }
                    else {
                        if (node != null)
                            node.setNext(first);
                        else
                            node = new HashEntry<K,V>(hash, key, value, first);
                        int c = count + 1;
                        if (c > threshold && tab.length < MAXIMUM_CAPACITY)
                            rehash(node);//扩容 *2的大小
                        else
                            setEntryAt(tab, index, node);。
                        ++modCount;
                        count = c;
                        oldValue = null;
                        break;
                    }
                }
            } finally {
                unlock();//释放锁
            }
            return oldValue;
        }


	  /**
         * Scans for a node containing given key while trying to
         * acquire lock, creating and returning one if not found. Upon
         * return, guarantees that lock is held. UNlike in most
         * methods, calls to method equals are not screened: Since
         * traversal speed doesn't matter, we might as well help warm
         * up the associated code and accesses as well.
         *  通过key找对应node.没有就创建一个。
         * @return a new node if key not found, else null
         */
        private HashEntry<K,V> scanAndLockForPut(K key, int hash, V value) {
            HashEntry<K,V> first = entryForHash(this, hash);
            HashEntry<K,V> e = first;
            HashEntry<K,V> node = null;
            int retries = -1; // negative while locating node
            while (!tryLock()) {//没有获取锁,重试
                HashEntry<K,V> f; // to recheck first below
                if (retries < 0) {
                    if (e == null) {
                        if (node == null) // speculatively create node
                            node = new HashEntry<K,V>(hash, key, value, null);
                        retries = 0;
                    }
                    else if (key.equals(e.key))
                        retries = 0;
                    else
                        e = e.next;
                }
                else if (++retries > MAX_SCAN_RETRIES) {//大于最大尝试次数
                    lock();//等待,阻塞锁
                    break;
                }
                else if ((retries & 1) == 0 &&
                         (f = entryForHash(this, hash)) != first) {
                    e = first = f; // re-traverse if entry changed
                    retries = -1;
                }
            }
            return node;
        }

     /**  get 方法不加锁
     * Returns the value to which the specified key is mapped,
     * or {@code null} if this map contains no mapping for the key.
     *
     * <p>More formally, if this map contains a mapping from a key
     * {@code k} to a value {@code v} such that {@code key.equals(k)},
     * then this method returns {@code v}; otherwise it returns
     * {@code null}.  (There can be at most one such mapping.)
     *
     * @throws NullPointerException if the specified key is null
     */
    public V get(Object key) {
        Segment<K,V> s; // manually integrate access methods to reduce overhead
        HashEntry<K,V>[] tab;
        int h = hash(key);
        long u = (((h >>> segmentShift) & segmentMask) << SSHIFT) + SBASE;
        if ((s = (Segment<K,V>)UNSAFE.getObjectVolatile(segments, u)) != null &&
            (tab = s.table) != null) {
            for (HashEntry<K,V> e = (HashEntry<K,V>) UNSAFE.getObjectVolatile
                     (tab, ((long)(((tab.length - 1) & h)) << TSHIFT) + TBASE);
                 e != null; e = e.next) {
                K k;
                if ((k = e.key) == key || (e.hash == h && key.equals(k)))
                    return e.value;
            }
        }
        return null;
    }

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏c#开发者

Easyui DataGrid DateRange Filter 漂亮实用的日期区间段筛选功能

自定义扩展Jquery easyui datagrid filter组件实现对日期类型区间段的筛选功能。显示效果如一下 ? 是不是非常实用 引用的jquery ...

4087
来自专栏牛客网

网易一面面经

自我介绍 TCP三次握手和四次挥手的过程 为什么是三次握手 JDK动态代理 如果没有实现接口使用什么?(CGLIB) NIO和BIO区别 HashMap Con...

4319
来自专栏向治洪

Android 应用安装过程分析

在之前的文章中,我们对PakageManagerService启动流程分析 做了简单的介绍,并对PMS系统的启动流程做了详细的解析。上面只是说到了Android...

7559
来自专栏狂码一生

用MFC写一个聊天室程序 - 学习笔记

下面的服务器端与客户端的程序与步骤是我在学习MFC网络编程写一个聊天室程序所写的程序,在这里作一个笔记,也希望能帮到一部分刚刚学习的朋友,一起共勉,一起努力历进...

1.1K15
来自专栏跟着阿笨一起玩NET

C#如何快速高效地导出大量数据?

本文转载:http://www.cnblogs.com/herbert/archive/2010/07/28/1787095.html

4541
来自专栏我是攻城师

在Lucene或Solr中实现高亮的策略

3825
来自专栏linux驱动个人学习

高通Audio中ASOC的codec驱动(二)

继上一篇文章:高通Audio中ASOC的machine驱动(一) ASOC的出现是为了让codec独立于CPU,减少和CPU之间的耦合,这样同一个codec驱动...

1K6
来自专栏数据之美

关于 xargs 参数被截断,tar 文件被覆盖的问题

问题: 目录下共 2W+ 个小文件: $ find . -type f | wc -l   20083   如果我们这样打包,会爆出 "Arg...

2326
来自专栏生信技能树

下载TCGA所有癌症的maf文件做signature分析

才sanger研究所已经做好了这个分析,但是值得我们重复一下,效果如下: ? TCGA所有癌症的mutation signature 首先TCGA所有癌症的ma...

76212
来自专栏Java后端技术

Spring集成shiro做登陆认证

  其实很早的时候,就在项目中有使用到shiro做登陆认证,直到今天才又想起来这茬,自己抽空搭了一个spring+springmvc+mybatis和shiro...

1135

扫码关注云+社区

领取腾讯云代金券