top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Can we make array volatile in Java?

+1 vote
745 views
Can we make array volatile in Java?
posted Oct 21, 2016 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Declaring an array volatile does NOT give volatile access to it's fields. you're declaring the reference itself volatile, not it's elements.

In other words you're declaring a volatile set of elements, not a set of volatile elements. The solution here is to use AtomicIntegerArray in case you want to use integers. Another way (but kinda ugly) is to rewrite the reference to the array every time you edit a field

you do that by

arr = arr;

answer Nov 1, 2016 by Shyam
...