Reputation: 539
Supposing I have a Synchronized HashMap, which has a String as a key and as a List as a value.
Map<String, List<Object>> map = Collections.synchronizedMap(new HashMap<String, List<Object>>());
Will that list be thread-safe? For example, if I do this:
List<Object> list = map.get("whatever"); // Supposing I get a List
list.add(new Object()); // Would this be thread-safe?
In case not, would it be with a ConcurrentHashMap, instead of a synchronized one? Or the only way to avoid race conditions is converting it into a CopyOnWriteArrayList?
Upvotes: 2
Views: 1206
Reputation: 1562
Collections.synchronizedMap would only provide thread safeness for the resulting map. If you want to make the map values thread safe why not decorate your lists like:
Collections.synchronizedList(yourList)
and then add them to the map?
Upvotes: 4