从AtomicLongArray获取数组 [英] Get the array from an AtomicLongArray

查看:66
本文介绍了从AtomicLongArray获取数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Java 1.6和AtomicLongArray将原始的AtomicLongArray复制到一个新的AtomicLongArray中.有一个构造函数需要一个数组(AtomicLongArray(long [])),所以我想我可以从原始数组中获取该数组并将其提供给构造函数.

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.

遗憾的是,AtomicLongArray中的实际long []是私有的,似乎没有吸气剂.有什么方法可以做到,这意味着将值从一个AtomicLongArray复制到另一个?我无法基于该类创建自己的类,因为sun.misc.Unsafe类对我不可用.

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...

谢谢! 菲利普

推荐答案

我怀疑您必须创建自己的long[]并首先填充它,或者仅对原始对象进行迭代:

I suspect you have to create your own long[] and populate it first, or just iterate over the original:

AtomicLongArray copy = new AtomicLongArray(original.length());
for (int i = 0; i < copy.length(); i++)
{
    copy.set(i, original.get(i));
}

请注意,尽管AtomicLongArray中的每个单独操作都是原子操作,但没有 批量操作-因此无法在时间T上获取整个数组的快照".这种行为,我相信您需要使用同步.

Note that although each individual operation in AtomicLongArray is 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.

这篇关于从AtomicLongArray获取数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆