I read all and its contrary about Java ConcurrentHashMap usage. I hope my question will help clarify something that looks simple (with up to date answers).
I have a map like this:
ConcurrentHashMap<Integer, ClassA> map = new ConcurrentHashMap<Integer, ClassA>()
I am using a ConcurrentHashMap to keep put and get operation thread safe. My ClassA has some Integer/String attributes and a Collection of Strings.
Now I would like to know how to update my mapped object safely. If I want to create a method to update an object from my map by adding a new String, I have something like:
synchronized(map)
{
Collection<String> strings = map.get(id).getStrings();
if(!strings.contains(newString)) //strings can't be null
{
strings.add(newString);
}
}
Is this code safe against concurrent reads/write? Can it be done differently by using the Java API?