Answer the question
In order to leave comments, you need to log in
Will ArrayList be read in a consistent state into jucConcurrentHashMap?
I have code:
private final Map<Long, SomeStuff> map = new ConcurrentHashMap<>();
//Called by Thread 1
public void write() {
List list = new ArrayList<>();
for (int i = 0; i <100 ; i++) {
list.add(new Stuff(i))
}
map.put(1L,list)
}
// Called by Thread 2
public void read() {
List list = map.get(1L); // Будет ли лист в консистентном состоянии(кол-во элементов) , будут ли все элементы коллекции безопасно опубликованы
}
Answer the question
In order to leave comments, you need to log in
Yes. This is guaranteed by the ConcurrentMap interface .
Memory consistency effects: As with other concurrent collections, actions in a thread prior to placing an object into a ConcurrentMap as a key or value happen-before actions subsequent to the access or removal of that object from the ConcurrentMap in another thread.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question