Is synchronization required when setting the value of an element in an ArrayList


jddxf

On the one hand, according to the following Javadoc:

If multiple threads access an ArrayList instance concurrently, and at least one thread structurally modifies the list, it must be synchronized externally. (A structural modification is any operation that adds or removes one or more elements or explicitly resizes the backing array; merely setting the value of an element is not a structural modification.)

Synchronization is not necessary.

On the other hand, setting the value of an element is an atomic action and therefore cannot be interleaved. However, memory consistency errors are still possible.

Which is right?

EDIT: Maybe I haven't expressed myself clearly. Actually, I just wonder if the Javadoc is wrong or at least misleading. By the way, I said that setting the value of an element is an atomic action, but this is clearly wrong.

Bicelop

I don't think it's a good idea to make assumptions about how the set value is ArrayListimplemented . Importantly, if no consistency guarantees are provided in the Javadoc, then synchronization should always be done externally when multiple threads use the same thread ArrayList.

Or, depending on the use case, choose a completely different structure, such as one designed for concurrency CopyOnWriteArrayList. (Though it's important to make sure you choose the correct option, as they don't behave the same in all situations.)

In this case, the Javadoc doesn't explicitly say that synchronization can be omitted if only used set(), it just implies it. The hint is wrong because, as you say, memory consistency is not guaranteed at all, you could easily end up reading stale values, or multiple calls set()could be reordered.

Related


With ConcurrentHashMap, when is synchronization required?

For each position: I have a ConcurrentHashMap where I do the following: sequences = new ConcurrentHashMap<Class<?>, AtomicLong>(); if(!sequences.containsKey(table)) { synchronized (sequences) { if(!sequences.containsKey(table)) initial

With ConcurrentHashMap, when is synchronization required?

For each position: I have a ConcurrentHashMap where I do the following: sequences = new ConcurrentHashMap<Class<?>, AtomicLong>(); if(!sequences.containsKey(table)) { synchronized (sequences) { if(!sequences.containsKey(table)) initial

With ConcurrentHashMap, when is synchronization required?

For each position: I have a ConcurrentHashMap where I do the following: sequences = new ConcurrentHashMap<Class<?>, AtomicLong>(); if(!sequences.containsKey(table)) { synchronized (sequences) { if(!sequences.containsKey(table)) initial

When using synchronization, atomic references are not required

Maxim Dmitriev The code is from the Java Concurrency Guidelines by Fred Long . I understand that a set of atomic operations is not atomic. Therefore, the following code does not meet the requirements. To find the code, see page 23. public class Adder { pr

When using synchronization, atomic references are not required

Maxim Dmitriev The code is from the Java Concurrency Guidelines by Fred Long . I understand that a set of atomic operations is not atomic. Therefore, the following code does not meet the requirements. To find the code, see page 23. public class Adder { pr

Is synchronization with multiprocessor required in python?

Bobo When using code like this def execute_run(list_out): ... do something pool = ThreadPoolExecutor(6) for i in list1: for j in list2: pool.submit(myfunc, list_out) pool.join() Assuming threads modify list_out, do they operate in a sync

Synchronization is required in getters and setters

snow leopard Probably a very stupid question. Just wanted to confirm my understanding. class Test { private volatile String id; public void setID(String id) { this.id = id; } public String getID() {

Synchronization is required in asynchronous code

Cyrillic I'm trying to create a factory for an angular module that returns a JSON object received through angular $http.get(). In the for's callback function success(), I am trying to assign an object to a variable products. It seems to productsbe returned fro

Synchronization is required in getters and setters

snow leopard Probably a very stupid question. Just wanted to confirm my understanding. class Test { private volatile String id; public void setID(String id) { this.id = id; } public String getID() {

Is synchronization with multiprocessor required in python?

Bobo When using code like this def execute_run(list_out): ... do something pool = ThreadPoolExecutor(6) for i in list1: for j in list2: pool.submit(myfunc, list_out) pool.join() Assuming threads modify list_out, do they operate in a sync

Synchronization is required in getters and setters

snow leopard Probably a very stupid question. Just wanted to confirm my understanding. class Test { private volatile String id; public void setID(String id) { this.id = id; } public String getID() {