Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are simultaneous reads from an array thread-safe?

I have an array which contains integer values declared like this:

int data[] = new int[n];

Each value needs to be processed and I am splitting the work into pieces so that it can be processed by separate threads. The array will not be modified during processing.

Can all the processing threads read separate parts of the array concurrently? Or do I have to use a lock?

In other words: is this work order thread-safe?

Array is created and filled
Threads are created and started
Thread 0 reads data[0..3]
Thread 1 reads data[4..7]
Thread 2 reads data[8..n]
like image 588
user2342875 Avatar asked Aug 15 '13 16:08

user2342875


People also ask

Can multiple threads read the same array?

Would multiple threads each modifying a different array element be writing to the same location in memory? The answer is no. Each array element has a region of memory reserved for it alone within the region attributed the overall array.

Can two threads read at the same time?

This means two threads can read the same value and get different results creating a race condition. This can be prevented though memory barriers, correct use of volatile or a few other mechanisms.

Are synchronized methods thread-safe?

synchronized keyword is one of the way to achieve 'thread safe'. But Remember:Actually while multiple threads tries to access synchronized method they follow the order so becomes safe to access.

What will happen if multiple threads accessing the same resource?

Multiple threads accessing shared data simultaneously may lead to a timing dependent error known as data race condition. Data races may be hidden in the code without interfering or harming the program execution until the moment when threads are scheduled in a scenario (the condition) that break the program execution.


2 Answers

Reading contents of an array (or any other collection, fields of an object, etc.) by multiple threads is thread-safe provided that the data is not modified in the meantime.

If you fill the array with data to process and pass it to different threads for reading, then the data will be properly read and no data race will be possible.

Note that this will only work if you create the threads after you have filled the array. If you pass the array for processing to some already existing threads without synchronization, the contents of the array may not be read correctly. In such case the method in which the thread obtains the reference to the array should be synchronized, because a synchronized block forces memory update between threads.

On a side note: using an immutable collection may be a good idea. That way you ensure no modification is even possible. I would sugges using such wrapper. Check the java.util.concurrent.atomic package, there should be something you can use.

like image 75
Dariusz Avatar answered Oct 06 '22 13:10

Dariusz


As long as the threads don't modify the contents in the array, it is fine to read the array from multiple threads.

like image 32
Vikdor Avatar answered Oct 06 '22 13:10

Vikdor