Issue
I was reading the official Oracle documentation about Concurrency in Java and I was wondering 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?
Edit: I realized that the source code is publicly available: ConcurrentHashMap.java
Solution
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.
Answered By - Peter Lawrey Answer Checked By - Marie Seifert (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.