1.类 AtomicReferenceArray<E>
public class AtomicReferenceArray<E>extends Objectimplements Serializable
可以用原子方式更新其元素的对象引用数组
2.构造函数详解
AtomicReferenceArray
public AtomicReferenceArray(int length)
-
创建给定长度的新 AtomicReferenceArray。
- 参数:
-
length- 该数组的长度
AtomicReferenceArray
public AtomicReferenceArray(E[] array)
-
创建与给定数组具有相同长度的新 AtomicReferenceArray,并从给定数组复制其所有元素。
- 参数:
-
array- 从中复制元素的数组 - 抛出:
-
NullPointerException- 如果数组为 null
3.方法详解
length
public final int length()
-
返回该数组的长度。
-
- 返回:
- 该数组的长度
get
public final E get(int i)
-
获取位置
i的当前值。 -
- 参数:
-
i- 索引 - 返回:
- 当前值
set
public final void set(int i,
E newValue)
-
将位置
i的元素设置为给定值。 -
- 参数:
-
i- 索引 -
newValue- 新值
lazySet
public final void lazySet(int i,
E newValue)
-
最终将位置
i的元素设置为给定值。 -
- 参数:
-
i- 索引 -
newValue- 新值 - 从以下版本开始:
- 1.6
getAndSet
public final E getAndSet(int i, E newValue)
-
以原子方式将位置
i的元素设置为给定值,并返回旧值。 -
- 参数:
-
i- 索引 -
newValue- 新值 - 返回:
- 以前的值
compareAndSet
public final boolean compareAndSet(int i,
E expect,
E update)
-
如果当前值
==预期值,则以原子方式将位置i的元素设置为给定的更新值。 -
- 参数:
-
i- 索引 -
expect- 预期值 -
update- 新值 - 返回:
- 如果成功,则返回 true。返回 false 表示实际值与预期值不相等。
weakCompareAndSet
public final boolean weakCompareAndSet(int i,
E expect,
E update)
-
如果当前值
==预期值,则以原子方式将位置i的元素设置为给定的更新值。可能意外失败并且不提供排序保证,因此几乎只是
compareAndSet的适当替换方法。 -
- 参数:
-
i- 索引 -
expect- 预期值 -
update- 新值 - 返回:
- 如果成功,则返回 true。
toString
public String toString()
