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

public Class AtomicMarkableReference<V>

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

An AtomicMarkableReference maintains an object reference along with a mark bit, that can be updated atomically.

Implementation Note

This implementation maintains markable references by creating internal objects representing "boxed" [reference, boolean] 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 AtomicMarkableReference.Pair<V>
private static final VarHandle

Constructor Summary

AccessConstructor and Description
public
AtomicMarkableReference(V
the initial reference
initialRef
,
boolean
the initial mark
initialMark
)

Creates a new AtomicMarkableReference with the given initial values.

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

true if successful
attemptMark
(V
the expected value of the reference
expectedReference
,
boolean
the new value for the mark
newMark
)

Atomically sets the value of the mark 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
,
boolean
the expected value of the mark
expectedMark
,
boolean
the new value for the mark
newMark
)

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

public V

Returns:

the current value of the reference
get
(boolean[]
an array of size of at least one. On return, markHolder[0] will hold the value of the mark.
markHolder
)

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

public V

Returns:

the current value of the reference
getReference
()

Returns the current value of the reference.

public boolean

Returns:

the current value of the mark
isMarked
()

Returns the current value of the mark.

public void
set(V
the new value for the reference
newReference
,
boolean
the new value for the mark
newMark
)

Unconditionally sets the value of both the reference and mark.

public boolean

Returns:

true if successful
weakCompareAndSet
(V
the expected value of the reference
expectedReference
,
V
the new value for the reference
newReference
,
boolean
the expected value of the mark
expectedMark
,
boolean
the new value for the mark
newMark
)

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

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

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

Constructor Detail

AtomicMarkableReferenceback to summary
public AtomicMarkableReference(V initialRef, boolean initialMark)

Creates a new AtomicMarkableReference with the given initial values.

Parameters
initialRef:V

the initial reference

initialMark:boolean

the initial mark

Method Detail

attemptMarkback to summary
public boolean attemptMark(V expectedReference, boolean newMark)

Atomically sets the value of the mark 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

newMark:boolean

the new value for the mark

Returns:boolean

true if successful

casPairback to summary
private boolean casPair(AtomicMarkableReference.Pair<V> cmp, AtomicMarkableReference.Pair<V> val)
compareAndSetback to summary
public boolean compareAndSet(V expectedReference, V newReference, boolean expectedMark, boolean newMark)

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

Parameters
expectedReference:V

the expected value of the reference

newReference:V

the new value for the reference

expectedMark:boolean

the expected value of the mark

newMark:boolean

the new value for the mark

Returns:boolean

true if successful

getback to summary
public V get(boolean[] markHolder)

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

Parameters
markHolder:boolean[]

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

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

isMarkedback to summary
public boolean isMarked()

Returns the current value of the mark.

Returns:boolean

the current value of the mark

setback to summary
public void set(V newReference, boolean newMark)

Unconditionally sets the value of both the reference and mark.

Parameters
newReference:V

the new value for the reference

newMark:boolean

the new value for the mark

weakCompareAndSetback to summary
public boolean weakCompareAndSet(V expectedReference, V newReference, boolean expectedMark, boolean newMark)

Atomically sets the value of both the reference and mark to the given update values if the current reference is == to the expected reference and the current mark is equal to the expected mark. 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

expectedMark:boolean

the expected value of the mark

newMark:boolean

the new value for the mark

Returns:boolean

true if successful

java.util.concurrent.atomic back to summary

private Class AtomicMarkableReference.Pair<T>

extends Object
Class Inheritance

Field Summary

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

Constructor Summary

AccessConstructor and Description
private
Pair(T reference, boolean mark)

Method Summary

Modifier and TypeMethod and Description
pack-priv static <T> AtomicMarkableReference.Pair<T>
of(T reference, boolean mark)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

markback to summary
pack-priv final boolean mark
referenceback to summary
pack-priv final T reference

Constructor Detail

Pairback to summary
private Pair(T reference, boolean mark)

Method Detail

ofback to summary
pack-priv static <T> AtomicMarkableReference.Pair<T> of(T reference, boolean mark)