Thinker
Thinker

Reputation: 6892

Explain the below lines of put(key,value) method of concurrentHashmap?

I was reading a implementation of inserting a key value pair in ConcurrentHashMap here.

I am unable to understand the below lines.

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

Somebody please explain.

Upvotes: 1

Views: 158

Answers (1)

Joni
Joni

Reputation: 111339

The method call:

UNSAFE.getObject(segments, (j << SSHIFT) + SBASE)

is equivalent to a normal array access segments[j], but is likely faster because it requires no bounds checks. SBASE is the offset to where the array data begins in the segments array. SSHIFT is log2 of the array index scale, so j << SSHIFT calculates the raw byte offset of index j in the array.

Upvotes: 1

Related Questions