-1

ConcurrentHashMap allow conurrent modification of the Map elements from several threads without the need to block them but HashMap block the whole HashMap object and not elements of it as in ConcurrentHashMap right?

But what if one thread get the count of the ConcurrentHashMap as 10 and meanwhile another thread removes an item from that thread. Then that first thread count become false right?

posdef
  • 6,498
  • 11
  • 46
  • 94
FrankD
  • 859
  • 4
  • 19
  • 31
  • 2
    yes. After the second thread modified the HashMap the count got by the first thread becomes false. So you should not cash it. Just use the method the get it each time you need it. – Petr Jan 17 '13 at 16:11
  • `HashMap block` Actually it doesn't as it's not thread safe. This is okay if the Map is read-only, but not if it could be changed. – Peter Lawrey Jan 17 '13 at 16:27

1 Answers1

2

The answer to your question is in the most natural place, the javadocs for ConcurrentHashMap.

See below:

Retrieval operations (including get) generally do not block, so may overlap with update operations (including put and remove). Retrievals reflect the results of the most recently completed update operations holding upon their onset. For aggregate operations such as putAll and clear, concurrent retrievals may reflect insertion or removal of only some entries. Similarly, Iterators and Enumerations return elements reflecting the state of the hash table at some point at or since the creation of the iterator/enumeration. They do not throw ConcurrentModificationException. However, iterators are designed to be used by only one thread at a time.

posdef
  • 6,498
  • 11
  • 46
  • 94