[concurrency-interest] Questions on ConcurrentHashMap

David Holmes dcholmes at optusnet.com.au
Wed Nov 28 01:53:33 EST 2007

Hi Grace,

> 1) Consider these operations on a populated map:
> - map.clear() is called
> - immediately or concurrently afterwards map.get(A) and
> map.get(B) are called.
> We know for sure that if map is a synchronized HashMap, the result of
> A and B would be null.
> However, if map is a concurrentHashMap, the results of get(A) and
> get(B) could be any of the followings [AResult, BResult], [null,
> null], [AResult, null], [null, BResult].  Am I correct?

Yes and no. You are correct on the possible result set, but incorrect that
for a synchronized HashMap the result would always be [null, null].

It all depends on the timing of get relative to the clear:

- clear -> get(A) -> get(B)  => [null, null]
- get(A) -> clear -> get(B)  => [AResult, null]
- get(B) -> clear -> get(A)  => [null, BResult]
- get(A) -> get(B) -> clear  => [AResult, BResult]

With ConcurrentHashMap the timing window is different - clear becomes
clear-reaches-the-point-of-locking-segment-A/B - but the possible outcomes
are the same.

> 2) I have a map cache that behaves as such:

No comment on this part.

David Holmes

More information about the Concurrency-interest mailing list