问题
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.
回答1:
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.
回答2:
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
来源:https://stackoverflow.com/questions/15858681/segmentation-in-concurrenthashmap