0

Suppose, there are three threads, who want to access same object of ConcurrentHashMap of Java (Java 7). These three threads have operation as put, remove and get respectively. Could you please help me to understand, how ConcurrentHashMap will manage to put, remove and get Map Entry (Key, Value) for same segment on same time for multiple threads in Java (Java 7)? Please specify, if there is any update in Java 8 for same.

Edit:

What is mean by segment in ConcurrentHashMap as per Java 7?

The concurrency level, which is allowed during update operations can be set by the optional concurrencyLevel constructor argument (default 16) [ ConcurrentHashMap(int initialCapacity, float loadFactor, int concurrencyLevel) ], which is used as a hint for internal sizing, to partition ConcrrentHashMap internally, such partition is commonly known as Segment. Each segment manages it's own HashTable. The ConcurrentHashMap maintains a list of 16 segments by default.

Thanks and Regards.

Manjur
  • 63
  • 6
  • If you really want to know **how** those methods work, look at the source code. [Where can I locate and browse JDK 7 source files?](https://stackoverflow.com/q/4827002/5221149) Or look at `source.zip` in the JDK folder. --- At an abstract level, the effect will be as-if the 3 threads executed their operations at separate times, in arbitrary order, not all 3 at the exact same time, akin to SQL's *Serializable* isolation level. – Andreas Feb 09 '20 at 08:51
  • Please [edit] your question to include a description of what you mean by "segment". – Progman Feb 09 '20 at 08:57
  • 1
    If you mean the same key, the answer is it doesn't; ConcurrentHashMap will acquire a lock for the individual key when doing any operation on that key. (The lock is internal; it doesn't synchronize on the actual key object itself.) – kaya3 Feb 09 '20 at 09:46
  • I suggest comparing the source files of java 7 and java 8 with a diff to see if there's been any change. – Scratte Feb 09 '20 at 10:25

0 Answers0