How do I make an array volatile? Because as I’ve come to understand, it’s unsafe to make an array volatile?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Declaring an array volatile does not give volatile access to its fields. You’re declaring the reference itself volatile, not its elements.
In other words, you’re declaring a volatile set of elements, not a set of volatile elements.
The solution here is to use
AtomicIntegerArrayin 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:
(as I said… ugly)