concurrentHashMap源码中的readValueUnderLock(e)存在的意义?

发布于 2022-09-03 19:37:09 字数 918 浏览 27 评论 0

concurrentHashMap源码(JDK1.6)get方法中为什么要readValueUnderLock(e)vnull究竟是怎么产生的?
put方法中有这么一段:
tab[index] = new HashEntry<K,V>(key, hash, first, value);难道在执行构造方法中会存在中间状态?value还没有赋值就能读到?

V get(Object key, int hash) {
    if (count != 0) { // read-volatile
        HashEntry<K,V> e = getFirst(hash);
        while (e != null) {
            if (e.hash == hash && key.equals(e.key)) {
                V v = e.value;
                if (v != null)
                    return v;
                return readValueUnderLock(e); // recheck
            }
            e = e.next;
        }
    }
    return null;
}
V readValueUnderLock(HashEntry<K,V> e) {
    lock();
    try {
        return e.value;
    } finally {
        unlock();
    }
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

热情消退 2022-09-10 19:37:09
e.hash == hash && key.equals(e.key)

前面这一句说明了表里有这个key的,你看看put方法,当valuenull的时候是会跑出异常的:

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);
    }

我这是高版本的,可能和你的不一样,但是也是值也是不能为空的。
所以:

if (v != null)
    return v;
return readValueUnderLock(e); // recheck

不为空可以直接返回,如果为空则说明有其他线程在操作它。所以就加了一句。

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;
    }

现在版本的get方法把HashEntry<K,V> e弄成UNSAFE.getObjectVolatile()获取,像是volatile的了

陈年往事 2022-09-10 19:37:09

楼主可以把JDK升级一下,我用的1.8找了一下,没发现这段代码在ConcurrentHashMap中。

心房的律动 2022-09-10 19:37:09

对象初始的时候就是null,这个null并不是在程序中特意赋值的。

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文