Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of AtomicReferenceArray?

When is it a good idea to use AtomicReferenceArray? Please explain with an example.

like image 676
Margus Avatar asked Sep 29 '10 16:09

Margus


2 Answers

looks like it's functionally equivalent to AtomicReference[], occupying a little less memory though.

So it's useful when you need more than a million atomic references - can't think of any use case.

like image 56
irreputable Avatar answered Sep 28 '22 11:09

irreputable


If you had a shared array of object references, then you would use an AtomicReferenceArray to ensure that the array couldn't be updated simultaneously by different threads i.e. only one element can be updated at a time.

However, in an AtomicReference[] (array of AtomicReference) multiple threads can still update different elements simulateously, because the atomicity is on the elements, not on the array as a whole.

More info here.

like image 31
dogbane Avatar answered Sep 28 '22 10:09

dogbane