Reading the contents of an array (or any other collection, object fields, etc.) by multiple threads is thread safe, provided that the data does not change at the same time.
If you fill the array with data for processing and pass them to different threads for reading, then the data will be read correctly, and no data race will be possible.
Note that this will only work if you create threads after filling the array . If you pass the array for processing to existing threads without synchronization, the contents of the array may not be read correctly. In this case, the method in which the thread receives a reference to the array must be synchronized, since the synchronized block forcibly updates the memory between the threads.
On the other hand: using an immutable collection may be a good idea. Thus, you guarantee that no modification will be possible. I would rather use such a wrapper. Check out the java.util.concurrent.atomic package, there should be something you can use.
Dariusz
source share