Skip to content
Advertisement

Segment class in ConcurrentHashMap usage

Looking into implementation of ConcurrentHashMap, I see that when trying to manipulate it(put/remove), value is found and this value is then used as lock for synchronized code which performs actual manipulation. This way only that bucket is locked where value actually exists.

I also see a class Segment declared and in many resources available online its mentioned that this is used to implement concurrency. But I couldn’t find any code in implementation using Segment for synchronization implementation.

So what actually I miss here?

I am looking into java 11.

Advertisement

Answer

Looking at the sources of OpenJDK 8, the class Segment bears a comment:

    /**
     * Stripped-down version of helper class used in previous version,
     * declared for the sake of serialization compatibility
     */
    static class Segment<K,V> extends ReentrantLock implements Serializable {
        ...
    }

So it seems to be present only for backwards-compatibility.

Advertisement