0

I got somewhat confused on my understanding, when the below things asked to me in a quiz:

1)ConcurrentHashMap : As per my understanding, there is no lock to get values(corresponding to a key) from this map. Question is : if this is true, suppose t1 is writing(by taking lock on a Segment/bucket) and t2 tries to read same, t2 will not get the correct value, and thus inconsistent value to t2

2)HashMap: As per my understanding,before a element is added to a hashbucket, the hashvalue H is calculated as hashcode%16 (gives values from 0 to 15) for the key (key.hashcode()) and then added to the bucket whose hashvalue is H Note : there are 16 buckets(default implementation), which represents a ArrayList of LinkedList


|0 |1 |2 |3 |4 |5 |6 |7 |8 |9 |10 | 11 |12 |13 |14 |15 |


ArrayList at 2000(memory address)

You can very well say this is duplicate of Segmentation in ConcurrentHashMap, Regarding internal working of concurrent hashmap, HashMap or ConcurrentHashMap at Java Controllers?, etc. But I need to understand on doubts. few links/blogs to a good explanation will work for me. Thanks

Community
  • 1
  • 1
lowLatency
  • 5,534
  • 12
  • 44
  • 70

1 Answers1

0

Use this link for studying ConcurrentHashMap and HashMap

For the 1st part. What you are saying is correct. The get() doesn't takes a lock on the segment if it finds the key in the ConcurrentHashMap. If you have an another thread which is modifying the structure of the Map(eg. put()) and it hasn't finished yet, it's sure that you will get stale values.But it won't throw ConcurrentModificationException. The updated values will be reflected if the modifying operation completes before the retrieval operation.

I hope this helps.