Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do we need to synchronize access to an array when the array variable is volatile?

I have a class containing a volatile reference to an array:

private volatile Object[] objects = new Object[100];

Now, I can guarantee that, only one thread (call it writer) can write to the array. For example,

objects[10] = new Object();

All other threads will only read values written by the writer thread.

Question: Do I need to synchronize such reads and writes in order to ensure memory consistency?

I presume, yes I should. Because it would not be useful from performance standpoint if JVM provides some kind of memory consistency guarantees when writing to an array. But I'm not sure about that. Didn't find anything helpful in documentation.

like image 342
St.Antario Avatar asked Apr 05 '16 10:04

St.Antario


People also ask

Do I need synchronized with volatile?

Effectively, a variable declared volatile must have its data synchronized across all threads, so that whenever you access or update the variable in any thread, all other threads immediately see the same value.

Can we store an array in volatile?

Yes, you can make the array volatile but that will only cover change to the reference variable pointing to an array, it will not cover changes in individual array elements.

How do you synchronize an array?

We can use Collections. synchronizedList(List<T>) method to synchronize collections in java. The synchronizedList(List<T>) method is used to return a synchronized (thread-safe) list backed by the specified list.

What is array synchronization?

Synchronizing on the array only locks on the array (in Java an array is an object). If you synchronize on some array individual element, suppousing they are objects and not primitives, that is another different lock. You can have one without the other, and the inverse is also true. You can´t synchronize on a primitive.


1 Answers

You may use AtomicReferenceArray:

final AtomicReferenceArray<Object> objects = new AtomicReferenceArray<>(100);

// writer
objects.set(10, new Object());

// reader
Object obj = objects.get(10);

This will ensure atomic updates and happens-before consistency of read/write operations, the same as if each item of array was volatile.

like image 171
Alex Salauyou Avatar answered Sep 20 '22 13:09

Alex Salauyou