Hello. Such a problem - as a container I use MultiValueMap from commons-coolection. The key is String, Value is ArrayList. Several threads simultaneously access my Map, which read the value (ArrayList), take an elimination from it, after which this element is removed from this ArrayList. The problem is that I don't know how to synchronize it correctly. If I synchronize operations to remove an item from an ArrayList in the same way:

synchronize(myMap) { Collection c = myMap.getCollection(key);// Возвращается value - ArrayList c.get(randomIndex); c.remove(randomIndex); // Удаление элемента из листа } 

Does this mean that read-delete operations for my ArrayList are thread-safe? After all, the sheet itself is not synchronized, and as you can see from the example, for synchronization, I use the Map object and not the List object.

    1 answer 1

    In order to make everything thread-safe, then all operations with sheets received from a map must be inside a synchronized section for the same map (in the simple case). And moreover, it is necessary that the receipt of a sheet from a map was in the same section as the operations on it.

    • Well, I have done so. I wanted to know whether it is necessary to synchronize operations on this sheet after receiving the sheet from the mapa? - Vladimir
    • one
      No no need. Apparently, you do not understand the essence of synchronized, but think that it is some kind of magic that allows you to synchronize objects. I advise you to ask how the Hoare monitor works and how it is used. The synchronized keyword does almost nothing with the map itself. In theory, you can do synchronized on some other object and get the same result. - cy6erGn0m