Java Array list synchronization, if it is written in one stream and read in another

I have a controller class that runs on thread A and compiles a list of local variables like this

Theme A

list = new ArrayList<Map<String, Order>>(); list.add(...); list.add(...); 

where Order is a java bean with several primitive properties such as String, int, long, etc.

After creating this list, its link is passed to the user interface thread (thread B) Activity and access to it. Communication between threads is performed using the Handler class + post () method.

So the question is, can I access the list data from stream B without synchronization at all? Note that after creating in stream A, the list will not be accessible / modified at all. It just exists as a local variable and then passed to stream B.

+4
source share
4 answers

From the context you provide, it is not clear where this happens:

 list = new ArrayList<Map<String, Order>>(); list.add(...); list.add(...); 

If there is final in the constructor and list and the this link does not flow from the constructor and you are absolutely sure that the list will not change (for example, using the unmodifiableList decorator method) and references to Order instances are inaccessible from another place than it can be OK, so as not to use synchronization. Otherwise, you have a sword of Damocles above your head.

I mentioned Order links because you cannot receive exceptions if you change them from another place, but this can lead to data inconsistency / corruption.

+1
source

It's safe. Synchronization in the message queue establishes a connection between events. This, of course, assumes that you do not change the Maps later. Also any objects contained in maps, etc. Must not be changed by other threads without proper synchronization.

In short, if the list and none of the data inside it are changed by other threads than B, you do not need further synchronization.

+2
source

If you can guarantee that the list will not be changed, you will not need synchronization, since all threads will always see the same list.

0
source

Yes, there is no need to synchronize if you are only going to read data.

Note that even if thread A eventually changes the list when thread B (or any other number of threads) accesses it, you still do not need to synchronize, because there is only one scenario at any given time.

Sorry, the above statement is not entirely correct. As stated in the JavaDoc:

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

Also note that I do not take into account the modification of the element, but only the modification of the list.

-3
source

Source: https://habr.com/ru/post/1493466/


All Articles