java - Are simultaneous reads from an array thread-safe? -


i have array contains integer values declared this:

int data[] = new int[n]; 

each value needs processed , splitting work pieces can processed separate threads. array not modified during processing.

can processing threads read separate parts of array concurrently? or have use lock?

in other words: work order thread-safe?

array created , filled threads created , started thread 0 reads data[0..3] thread 1 reads data[4..7] thread 2 reads data[8..n] 

reading contents of array (or other collection, fields of object, etc.) multiple threads thread-safe provided data not modified in meantime.

if fill array data process , pass different threads reading, data read , no data race possible.

note this work if create threads after have filled array. if pass array processing existing threads without synchronization, contents of array may not read correctly. in such case method in thread obtains reference array should synchronized, because synchronized block forces memory update between threads.

on side note: using immutable collection may idea. way ensure no modification possible. sugges using such wrapper. check java.util.concurrent.atomic package, there should can use.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -