I have a variable:
ConcurrentHashMap<String, List<AnObjectType>> mapOfList;
The list that I understand is not thread safe. But I donβt want to use the Synchronized keyword, because I really need concurrent read access and write synchronization to the list, not a read and write lock.
Therefore, I would usually declare Volatile
for a variable as follows:
volatile List<AnObjectType> varName;
(Although in this case, I believe that these are links to the volatile link in the list, but I want both the link to the list and the contents of the list to be unstable.)
But how can I do this in the ConcurrentHashMap construct, given that I do not declare the list as a variable somewhere, but inside the method?
i.e. The list is created inside the method:
if (!mapOfList.containsKey("ListA")) { List<AnObjectType> listA=new ArrayList<AnObjectType>(); mapOfList.put("ListA", listA); }
and access to the list is carried out by another method within the same class:
List<AnObjectType> listA=mapOfList.get("ListA"); if (listA!=null) {
Sub Question: Will there be something like this work at all?
ConcurrentHashMap<String,List<AtomicReference<AnObjectType>>>>
List operations development:
The list will be available through several threads that will be read almost constantly. Recording will be called up under certain conditions. So what I want is simultaneous access to the contents of the list with rarely performed operations on the contents of the list, which should be reflected by all reads after the write operation.