Reading an array in a parallel system

I have a simple ArrayList and I am writing this ArrayList from several Thread through Java concurrency. Each Thread will read only the same instance of this ArrayList . Is there a chance of an error during a read operation?

+4
source share
2 answers

If there are no more entries, make it immutable with Collections.unmodifiableList , and then forget about reading problems.

+5
source

If the list is completely populated and always read-only by all threads, you will not have a problem. If there is a write operation, you need to synchronize all calls to the list or use a parallel list (for example, CopyOnWriteArrayList ).

+5
source

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


All Articles