3

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.

Jops
  • 22,535
  • 13
  • 46
  • 63
tuntun fdg
  • 491
  • 2
  • 8
  • 12

2 Answers2

3

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.

jspcal
  • 50,847
  • 7
  • 72
  • 76
  • perfect explanation thanks , could you please advise some url where its diagram or graphically it is shown that how it is divided so that I can grasp more – tuntun fdg Apr 07 '13 at 03:58
  • Here's a diagram: http://javaopensourcecode.blogspot.com/2012/06/concurrenthashmap.html – jspcal Apr 07 '13 at 04:01
  • could you please share some more urls like that so that data structure part will e more clear..!! – tuntun fdg Apr 08 '13 at 05:47
0

Just to add to the other great answers, ConcurrentHashMap divides the map into a number of segments (based on the concurrency level set when creating the map or the default concurrency level of 16).

Concurrent reads within the same segment gets the most recently updated value, reads do not require locks and are not blocked. Writes in different segments can happen concurrently, however, two writes in the same segment may block.

More at: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentHashMap.html

Sripriya V
  • 295
  • 3
  • 8