java - Why can the get() method in ConcurrentHashMap not be locked?
漂亮男人
漂亮男人 2017-05-17 10:00:31
0
1
803
 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;
    }

Why does Vector's get require synchronized locking but ConcurrentHashMap does not? Moreover, CopyOnWriteArrayList also uses copy-on-write to achieve parallel reading and writing. Obviously, ConcrrentHashMap does not implement copy-on-write. How does it ensure that inconsistent intermediate states will not be read during parallel reading and writing?

漂亮男人
漂亮男人

reply all(1)
PHPzhong

This article explains it very well

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!