Using Java 1.6 and the AtomicLongArray, I’d like to “copy” the original AtomicLongArray into a new one. There is a constructor that takes an array (AtomicLongArray(long[])), so I thought I could just get the array from the original one and give it to the constructor.
Sadly, the actual long[] in the AtomicLongArray is private and there seem to be no getters for it. Is there any way to do this, meaning copy the values from one AtomicLongArray to another? I can’t create my own class based on this class, as the sun.misc.Unsafe class is not available to me.
This is needed because I’m going to iterate over the values, and I don’t want them modified by another thread during iteration. So I thought I could make a copy and use that for the iteration…
Thanks!
Phillip
I suspect you have to create your own
long[]and populate it first, or just iterate over the original:Note that although each individual operation in
AtomicLongArrayis atomic, there are no bulk operations – so there’s no way of getting a “snapshot” of the whole array at time T. If you want that sort of behaviour, I believe you’ll need to use synchronization.