B
B
bakomchik2017-07-12 16:03:13
Java
bakomchik, 2017-07-12 16:03:13

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);  // Будет ли лист в консистентном состоянии(кол-во элементов) , будут ли все элементы коллекции безопасно опубликованы 
}

Will map.get(1L) return a leaf in a consistent state(number of elements) , will all elements of the collection be safely published?
I ask, maybe in the sources jucConcurrentHashMap put is synchronized on the Node to which the value is written
And get goes through Unafe.getObjectVolatile
and I can’t figure out if there is an HB edge between them

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Ruslan Lopatin, 2017-07-12
@lorus

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 question

Ask a Question

731 491 924 answers to any question