2017  Kodetalk | Feedback | Privacy Policy | Terms | About
userimage

How does ConcurrentHashMap work internally?

what could be the difference between a Collection returned by

public static <T> Collection<T> synchronizedCollection(Collection<T> c);

and using for example a ConcurrentHashMap. I"m assuming that I use synchronizedCollection(Collection<T> c) on a HashMap. I know that in general a synchronized collection is essentially just a decorator for my HashMap so it is obvious that a ConcurrentHashMap has something different in its internals. Do you have some information about those implementation details?

userimage

I would read the source of ConcurrentHashMap as it is rather complicated in the detail. In short it has

  • Multiple partitions which can be locked independently. (16 by default)
  • Using concurrent Locks operations for thread safety instead of synchronized.
  • Has thread safe Iterators. synchronizedCollection's iterators are not thread safe.
  • Does not expose the internal locks. synchronizedCollection does.
Answer is