- java.lang.Object
-
- java.util.concurrent.atomic.AtomicStampedReference<V>
-
- 参数类型
-
V- 此引用引用的对象类型
public class AtomicStampedReference<V> extends Object
AtomicStampedReference维护一个对象引用以及一个整数“标记”,可以原子方式更新。实现说明:此实现通过创建表示“盒装”[引用,整数]对的内部对象来维护标记引用。
- 从以下版本开始:
- 1.5
-
-
构造方法摘要
构造方法 构造器 描述 AtomicStampedReference(V initialRef, int initialStamp)使用给定的初始值创建新的AtomicStampedReference。
-
方法摘要
所有方法 实例方法 具体的方法 变量和类型 方法 描述 booleanattemptStamp(V expectedReference, int newStamp)如果当前引用为预期引用的当前引用为==,则以原子方式将戳记的值设置为给定的更新值。booleancompareAndSet(V expectedReference, V newReference, int expectedStamp, int newStamp)如果当前参考值为==并且当前标记等于预期标记,则以原子方式将参考值和标记值设置为给定的更新值。Vget(int[] stampHolder)返回引用和戳记的当前值。VgetReference()返回引用的当前值。intgetStamp()返回戳记的当前值。voidset(V newReference, int newStamp)无条件地设置引用和戳记的值。booleanweakCompareAndSet(V expectedReference, V newReference, int expectedStamp, int newStamp)如果当前引用为预期引用的当前引用为==且当前戳记等于预期戳记,则以原子方式将引用和戳记的值设置为给定的更新值。
-
-
-
构造方法详细信息
-
AtomicStampedReference
public AtomicStampedReference(V initialRef, int initialStamp)
使用给定的初始值创建新的AtomicStampedReference。- 参数
-
initialRef- 初始参考 -
initialStamp- 最初的印章
-
-
方法详细信息
-
getReference
public V getReference()
返回引用的当前值。- 结果
- 参考的当前值
-
getStamp
public int getStamp()
返回戳记的当前值。- 结果
- 邮票的当前价值
-
get
public V get(int[] stampHolder)
返回引用和戳记的当前值。 典型用法是int[1] holder; ref = v.get(holder);。- 参数
-
stampHolder- 大小至少为一的数组。 返回时,stampHolder[0]将保留邮票的值。 - 结果
- 参考的当前值
-
weakCompareAndSet
public boolean weakCompareAndSet(V expectedReference, V newReference, int expectedStamp, int newStamp)
如果当前引用为预期引用的当前引用为==且当前戳记等于预期戳记,则以原子方式将引用和戳记的值设置为给定的更新值。May fail spuriously and does not provide ordering guarantees ,所以很少是
compareAndSet的合适替代compareAndSet。- 参数
-
expectedReference- 参考的预期值 -
newReference- 引用的新值 -
expectedStamp- 邮票的预期价值 -
newStamp- 邮票的新值 - 结果
-
true如果成功
-
compareAndSet
public boolean compareAndSet(V expectedReference, V newReference, int expectedStamp, int newStamp)
如果当前引用为预期引用的当前引用为==且当前标记等于预期标记,则以原子方式将引用和标记的值设置为给定的更新值。- 参数
-
expectedReference- 参考的预期值 -
newReference- 引用的新值 -
expectedStamp- 邮票的预期价值 -
newStamp- 邮票的新值 - 结果
-
true如果成功
-
set
public void set(V newReference, int newStamp)
无条件地设置引用和戳记的值。- 参数
-
newReference- 引用的新值 -
newStamp- 邮票的新值
-
attemptStamp
public boolean attemptStamp(V expectedReference, int newStamp)
如果当前引用为==到==引用,==原子方式将戳记的值设置为给定的更新值。 任何给定的此操作调用都可能会失败(返回false),但是当当前值保持期望值并且没有其他线程也尝试设置该值时,重复调用将最终成功。- 参数
-
expectedReference- 引用的预期值 -
newStamp- 邮票的新值 - 结果
-
true如果成功
-
-