0
回答
ComcurrentHashmap中计算segement位置的问题?
【寻找人气王】邀新用户免费体验华为云服务,百元话费等你拿!   

public V put(K key, V value) {
        Segment<K,V> s;
        if (value == null)
            throw new NullPointerException();
        int hash = hash(key);
        int j = (hash >>> segmentShift) & segmentMask;
        if ((s = (Segment<K,V>)UNSAFE.getObject          // nonvolatile; recheck
             (segments, (j << SSHIFT) + SBASE)) == null) //  in ensureSegment
            s = ensureSegment(j);
        return s.put(key, hash, value, false);
    }

 

黄色行,hash无符号右移segmentShift后,已经能够保证落在segmentMask内了,为什么还要按位与?

比如segmentShift=28,segmentMask=15,hash=994162679 二进制:00111011010000011011011111110111,

hash >>> segmentShift后二进制为:00000000000000000000000000000011

已经落在segmentMask内了,再进行00000000000000000000000000000011 & 00000000000000000000000000001111 =00000000000000000000000000000011还有什么意义吗?

举报
pinitianjie
发帖于6个月前 0回/176阅
顶部