Top Description Inners Fields Constructors Methods
java.util.concurrent.atomic

public Class AtomicStampedReference<V>

extends Object
Class Inheritance
Type Parameters
<V>
The type of object referred to by this reference
Imports
java.lang.invoke.MethodHandles, .VarHandle

An AtomicStampedReference maintains an object reference along with an integer "stamp", that can be updated atomically.

Implementation Note

This implementation maintains stamped references by creating internal objects representing "boxed" [reference, integer] pairs.

Author
Doug Lea
Since
1.5

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private volatile AtomicStampedReference.Pair<V>
private static final VarHandle

Constructor Summary

AccessConstructor and Description
public
AtomicStampedReference(V
the initial reference
initialRef
,
int
the initial stamp
initialStamp
)

Creates a new AtomicStampedReference with the given initial values.

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

true if successful
attemptStamp
(V
the expected value of the reference
expectedReference
,
int
the new value for the stamp
newStamp
)

Atomically sets the value of the stamp to the given update value if the current reference is == to the expected reference.

private boolean
public boolean

Returns:

true if successful
compareAndSet
(V
the expected value of the reference
expectedReference
,
V
the new value for the reference
newReference
,
int
the expected value of the stamp
expectedStamp
,
int
the new value for the stamp
newStamp
)

Atomically sets the value of both the reference and stamp to the given update values if the current reference is == to the expected reference and the current stamp is equal to the expected stamp.

public V

Returns:

the current value of the reference
get
(int[]
an array of size of at least one. On return, stampHolder[0] will hold the value of the stamp.
stampHolder
)

Returns the current values of both the reference and the stamp.

public V

Returns:

the current value of the reference
getReference
()

Returns the current value of the reference.

public int

Returns:

the current value of the stamp
getStamp
()

Returns the current value of the stamp.

public void
set(V
the new value for the reference
newReference
,
int
the new value for the stamp
newStamp
)

Unconditionally sets the value of both the reference and stamp.

public boolean

Returns:

true if successful
weakCompareAndSet
(V
the expected value of the reference
expectedReference
,
V
the new value for the reference
newReference
,
int
the expected value of the stamp
expectedStamp
,
int
the new value for the stamp
newStamp
)

Atomically sets the value of both the reference and stamp to the given update values if the current reference is == to the expected reference and the current stamp is equal to the expected stamp.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

pairback to summary
private volatile AtomicStampedReference.Pair<V> pair
PAIRback to summary
private static final VarHandle PAIR

Constructor Detail

AtomicStampedReferenceback to summary
public AtomicStampedReference(V initialRef, int initialStamp)

Creates a new AtomicStampedReference with the given initial values.

Parameters
initialRef:V

the initial reference

initialStamp:int

the initial stamp

Method Detail

attemptStampback to summary
public boolean attemptStamp(V expectedReference, int newStamp)

Atomically sets the value of the stamp to the given update value if the current reference is == to the expected reference. Any given invocation of this operation may fail (return false) spuriously, but repeated invocation when the current value holds the expected value and no other thread is also attempting to set the value will eventually succeed.

Parameters
expectedReference:V

the expected value of the reference

newStamp:int

the new value for the stamp

Returns:boolean

true if successful

casPairback to summary
private boolean casPair(AtomicStampedReference.Pair<V> cmp, AtomicStampedReference.Pair<V> val)
compareAndSetback to summary
public boolean compareAndSet(V expectedReference, V newReference, int expectedStamp, int newStamp)

Atomically sets the value of both the reference and stamp to the given update values if the current reference is == to the expected reference and the current stamp is equal to the expected stamp.

Parameters
expectedReference:V

the expected value of the reference

newReference:V

the new value for the reference

expectedStamp:int

the expected value of the stamp

newStamp:int

the new value for the stamp

Returns:boolean

true if successful

getback to summary
public V get(int[] stampHolder)

Returns the current values of both the reference and the stamp. Typical usage is int[1] holder; ref = v.get(holder); .

Parameters
stampHolder:int[]

an array of size of at least one. On return, stampHolder[0] will hold the value of the stamp.

Returns:V

the current value of the reference

getReferenceback to summary
public V getReference()

Returns the current value of the reference.

Returns:V

the current value of the reference

getStampback to summary
public int getStamp()

Returns the current value of the stamp.

Returns:int

the current value of the stamp

setback to summary
public void set(V newReference, int newStamp)

Unconditionally sets the value of both the reference and stamp.

Parameters
newReference:V

the new value for the reference

newStamp:int

the new value for the stamp

weakCompareAndSetback to summary
public boolean weakCompareAndSet(V expectedReference, V newReference, int expectedStamp, int newStamp)

Atomically sets the value of both the reference and stamp to the given update values if the current reference is == to the expected reference and the current stamp is equal to the expected stamp. This operation may fail spuriously and does not provide ordering guarantees, so is only rarely an appropriate alternative to compareAndSet.

Parameters
expectedReference:V

the expected value of the reference

newReference:V

the new value for the reference

expectedStamp:int

the expected value of the stamp

newStamp:int

the new value for the stamp

Returns:boolean

true if successful

java.util.concurrent.atomic back to summary

private Class AtomicStampedReference.Pair<T>

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
pack-priv final T
pack-priv final int

Constructor Summary

AccessConstructor and Description
private
Pair(T reference, int stamp)

Method Summary

Modifier and TypeMethod and Description
pack-priv static <T> AtomicStampedReference.Pair<T>
of(T reference, int stamp)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

referenceback to summary
pack-priv final T reference
stampback to summary
pack-priv final int stamp

Constructor Detail

Pairback to summary
private Pair(T reference, int stamp)

Method Detail

ofback to summary
pack-priv static <T> AtomicStampedReference.Pair<T> of(T reference, int stamp)