I was going through the ConcurrentHashMap
and this related tutorial, and had some questions.
In the article, it was mention that
ConcurrentHashMap
allows multiple readers to read concurrently without any blocking. This is achieved by partitioning the Map into different parts based on concurrency level and locking only a portion of the Map during updates. Default concurrency level is 16, and accordingly the Map is divided into 16 part and each part is governed with a different lock. This means, 16 threads can operate on Map simultaneously, until they are operating on different parts of the Map. This makesConcurrentHashMap
high performant despite keeping thread-safety intact. Though, it comes with a caveat: Since update operations likeput()
,remove()
,putAll()
orclear()
are not synchronized, concurrent retrieval may not reflect the most recent change on the MapAnother point also mentioned in the article: Another important point to remember is iteration over CHM, Iterator returned by
keySet
are weakly consistent and they only reflect state ofConcurrentHashMap
at a certain point and may not reflect any recent change.
I have not understood the points highlighted in bold, could you provide more info or show me in a simple program?