Synchronization is required when manipulating various arrays (array of objects)

In context, if Java, I have such code,

MyObject[] array; 

and in different threads I have this code

 array[i] = new MyObject(val); 

If I guarantee that each of my threads uses different " i " values, then will I need to synchronize the above statement to take care of the race condition?

+2
java arrays synchronization
Jun 02 '09 at 15:14
source share
4 answers

Race conditions are only a problem if two threads can simultaneously read and change the same variable.

As long as you are sure that each thread uses a different range of indices and the underlying array is not redefined, then it should be safe to think of each cell as a different variable. Therefore, each thread works with a separate set of variables, and you will not get the conditions of the race.

At the same time, make sure that you really do not overlap when using indexes - this is often more complicated than it seems.

In addition, you must make sure that two cells are not displayed in the object - if you change the same object (and not just a link to it) from two threads, you can get a race condition.

+3
Jun 02 '09 at 15:17
source share

May be. If each task is written to another location in the array, they will not overwrite each other. So that works.

You may have encountered a problem with the final processing of the array, as Java does not guarantee when values ​​are written to memory. The optimizer may decide to write the values ​​very late (or too late for you). Therefore, you must make the array volatile , which means that when accessing the values ​​in it, caching should not be performed.

+3
Jun 02 '09 at 15:20
source share

If you are absolutely sure that each thread will always refer to a different index, you do not need to synchronize these calls.

BUT you need to make sure that the reference to the array is correctly published for different threads - in practice, this usually means that the link must be final or volatile .

+2
Jun 02 '09 at 15:35
source share

There are three low-key points with synchronization.

Array variable itself: Declaring an array variable as volatile or final (or synchronizing to array ) is important to ensure that both threads will have access to the same instances of the array. The following are possible:

  • Themes A and B are launched
  • Thread A instances of array as a new Array object
  • Thread B tries to access thread A array , but gets NullPointerExceptoin because it still does not see Thread A assignment to the new array .

Not good.

The array refers to MyObject instances (and what seems to be the actual Venkatraman question): with the array variable correctly synchronized, yes, two threads can access the different elements in the array safely. After the child thread completes what it does, then the "master" thread will be synchronized with the array to ensure its final state for the children of the elements before using them.

MyObject objects themselves , but this is beyond the scope of the question

0
Jun 04 '09 at 9:34
source share



All Articles