Search code examples
javahashmapconcurrenthashmap

Segmentation in ConcurrentHashMap


I am a newbie to the world of Java and I was exploring the ConcurrentHashMap API in which I discovered this:

static final int DEFAULT_INITIAL_CAPACITY = 16;
  static final float DEFAULT_LOAD_FACTOR = 0.75F;
  static final int DEFAULT_CONCURRENCY_LEVEL = 16;
  static final int MAXIMUM_CAPACITY = 1073741824;
  static final int MAX_SEGMENTS = 65536;
  static final int RETRIES_BEFORE_LOCK = 2;
  final Segment<K, V>[] segments;
final Segment<K, V> segmentFor(int paramInt)
  {
    return this.segments[(paramInt >>> this.segmentShift & this.segmentMask)];
  }

What are the fundamentals of segmentation in ConcurrentHashMap and why it is used? Please advise more on the segmentation concept.


Solution

  • The concurrent hash map divides its contents into segments, to reduce writer lock contention.

    The concurrencyLevel parameter defines the number of segments. It's 16 by default.