ConcurrentHashMap with ArrayList as value

I need to use a HashMap of the form <String, ArrayList<String>> , which will be accessed by several different threads. From what I was able to understand, ConcurrentHashMap is the preferred method. But will there be a problem with the fact that the value of the map is an ArrayList? Should I define the value as a synchronized ArrayList or something like that?

+4
source share
1 answer

Yes, there may be a problem. ConcurrentHashMap will be thread safe for accessing the Map, but in lists to be executed, it should be thread safe if multiple threads can work simultaneously with instance instances.

Therefore, use a list that does not contain threads, if true.

Change - now, when I think about it, the rabbit hole goes further. You have a map, you have your own list, and you have objects in the list. Anything that can be modified by multiple threads must be thread safe. Therefore, if many streams can modify maps, lists, and objects in lists, then all of them must be protected from streams. If only Map and List instances can be changed at the same time, only they need thread safety. If several threads can read everything but not change, then you do not need thread safety (I think someone correct me if this is not so)

+12
source

All Articles