- java.lang.Object
-
- java.lang.invoke.VarHandle
-
public abstract class VarHandle extends Object
Dynamically typed reference to a field, allowing read and write operations, both atomic and with/without memory barriers. SeeVarHandle.AccessMode
for supported operations. VarHandle instances are created through the MethodHandles factory API.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
VarHandle.AccessMode
Access mode identifiers for VarHandle operations.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description MethodType
accessModeType(VarHandle.AccessMode accessMode)
EachVarHandle.AccessMode
, e.g. get and set, requires different parameters in addition to thecoordinateTypes()
.static void
acquireFence()
Inserts an acquire memory fence, ensuring that no loads before this fence are reordered with any loads/stores after the fence.Object
compareAndExchange(Object... args)
Atomically sets the value of the field referenced by thisVarHandle
if the field value before this operation equaled the expected value.Object
compareAndExchangeAcquire(Object... args)
Atomically sets the value of the field referenced by thisVarHandle
if the field value before this operation equaled the expected value.Object
compareAndExchangeRelease(Object... args)
Atomically sets the value of the field referenced by thisVarHandle
if the field value before this operation equaled the expected value.boolean
compareAndSet(Object... args)
Atomically sets the value of the field referenced by thisVarHandle
if the field value before this operation equaled the expected value.List<Class<?>>
coordinateTypes()
static void
fullFence()
Inserts a complete memory fence, ensuring that no loads/stores before this fence are reordered with any loads/stores after the fence.Object
get(Object... args)
Gets the value of the field referenced by thisVarHandle
.Object
getAcquire(Object... args)
Gets the value of the field referenced by thisVarHandle
using acquire semantics.Object
getAndAdd(Object... args)
Atomically adds to the current value of the field referenced by thisVarHandle
and returns the value of the field prior to the update.Object
getAndAddAcquire(Object... args)
Atomically adds to the current value of the field referenced by thisVarHandle
and returns the value of the field prior to the update.Object
getAndAddRelease(Object... args)
Atomically adds to the current value of the field referenced by thisVarHandle
and returns the value of the field prior to the update.Object
getAndBitwiseAnd(Object... args)
Atomically ANDs the provided value and the current value of the field referenced by thisVarHandle
and returns the value of the field prior to the update.Object
getAndBitwiseAndAcquire(Object... args)
Atomically ANDs the provided value and the current value of the field referenced by thisVarHandle
and returns the value of the field prior to the update.Object
getAndBitwiseAndRelease(Object... args)
Atomically ANDs the provided value and the current value of the field referenced by thisVarHandle
and returns the value of the field prior to the update.Object
getAndBitwiseOr(Object... args)
Atomically ORs the provided value and the current value of the field referenced by thisVarHandle
and returns the value of the field prior to the update.Object
getAndBitwiseOrAcquire(Object... args)
Atomically ORs the provided value and the current value of the field referenced by thisVarHandle
and returns the value of the field prior to the update.Object
getAndBitwiseOrRelease(Object... args)
Atomically ORs the provided value and the current value of the field referenced by thisVarHandle
and returns the value of the field prior to the update.Object
getAndBitwiseXor(Object... args)
Atomically XORs the provided value and the current value of the field referenced by thisVarHandle
and returns the value of the field prior to the update.Object
getAndBitwiseXorAcquire(Object... args)
Atomically XORs the provided value and the current value of the field referenced by thisVarHandle
and returns the value of the field prior to the update.Object
getAndBitwiseXorRelease(Object... args)
Atomically XORs the provided value and the current value of the field referenced by thisVarHandle
and returns the value of the field prior to the update.Object
getAndSet(Object... args)
Atomically sets the value of the field referenced by thisVarHandle
and returns the value of the field prior to the update.Object
getAndSetAcquire(Object... args)
Atomically sets the value of the field referenced by thisVarHandle
and returns the value of the field prior to the update.Object
getAndSetRelease(Object... args)
Atomically sets the value of the field referenced by thisVarHandle
and returns the value of the field prior to the update.Object
getOpaque(Object... args)
Gets the value of the field referenced by thisVarHandle
.Object
getVolatile(Object... args)
Atomically gets the value of the field referenced by thisVarHandle
.boolean
isAccessModeSupported(VarHandle.AccessMode accessMode)
Not all AccessMode are supported for allVarHandle
instances, e.g.static void
loadLoadFence()
Inserts a load memory fence, ensuring that no loads before the fence are reordered with any loads after the fence.static void
releaseFence()
Inserts a release memory fence, ensuring that no stores before this fence are reordered with any loads/stores after the fence.void
set(Object... args)
Sets the value of the field referenced by thisVarHandle
.void
setOpaque(Object... args)
Sets the value of the field referenced by thisVarHandle
.void
setRelease(Object... args)
Sets the value of the field referenced by thisVarHandle
using acquire semantics.void
setVolatile(Object... args)
Atomically sets the value of the field referenced by thisVarHandle
.static void
storeStoreFence()
Inserts a store memory fence, ensuring that no stores before the fence are reordered with any stores after the fence.MethodHandle
toMethodHandle(VarHandle.AccessMode accessMode)
This method creates aMethodHandle
for a specificVarHandle.AccessMode
.Class<?>
varType()
Get the type of the field thisVarHandle
refers to.boolean
weakCompareAndSet(Object... args)
Sets the value of the field referenced by thisVarHandle
if the field value before this operation equaled the expected value.boolean
weakCompareAndSetAcquire(Object... args)
Sets the value of the field referenced by thisVarHandle
if the field value before this operation equaled the expected value.boolean
weakCompareAndSetPlain(Object... args)
Sets the value of the field referenced by thisVarHandle
if the field value before this operation equaled the expected value.boolean
weakCompareAndSetRelease(Object... args)
Sets the value of the field referenced by thisVarHandle
if the field value before this operation equaled the expected value.
-
-
-
Method Detail
-
varType
public final Class<?> varType()
Get the type of the field thisVarHandle
refers to.- Returns:
- The field type
-
coordinateTypes
public final List<Class<?>> coordinateTypes()
Different parameters are required in order to access the field referenced by thisVarHandle
depending on the type of theVarHandle
, e.g. instance field, static field, or array element. This method provides a list of the access parameters for thisVarHandle
instance.- Returns:
- The parameters required to access the field.
-
accessModeType
public final MethodType accessModeType(VarHandle.AccessMode accessMode)
EachVarHandle.AccessMode
, e.g. get and set, requires different parameters in addition to thecoordinateTypes()
. This method provides theMethodType
for a specificVarHandle.AccessMode
, including thecoordinateTypes()
.- Parameters:
accessMode
- TheVarHandle.AccessMode
to get theMethodType
for.- Returns:
- The
MethodType
corresponding to the providedVarHandle.AccessMode
.
-
isAccessModeSupported
public final boolean isAccessModeSupported(VarHandle.AccessMode accessMode)
Not all AccessMode are supported for allVarHandle
instances, e.g. because of the field type and/or field modifiers. This method indicates whether a specificVarHandle.AccessMode
is supported by by thisVarHandle
instance.- Parameters:
accessMode
- TheVarHandle.AccessMode
to check support for.- Returns:
- A boolean value indicating whether the
VarHandle.AccessMode
is supported.
-
toMethodHandle
public final MethodHandle toMethodHandle(VarHandle.AccessMode accessMode)
This method creates aMethodHandle
for a specificVarHandle.AccessMode
. TheMethodHandle
is bound to thisVarHandle
instance.- Parameters:
accessMode
- TheVarHandle.AccessMode
to create aMethodHandle
for.- Returns:
- A
MethodHandle
for the specifiedVarHandle.AccessMode
, bound to thisVarHandle
instance.
-
fullFence
public static void fullFence()
Inserts a complete memory fence, ensuring that no loads/stores before this fence are reordered with any loads/stores after the fence.
-
acquireFence
public static void acquireFence()
Inserts an acquire memory fence, ensuring that no loads before this fence are reordered with any loads/stores after the fence.
-
releaseFence
public static void releaseFence()
Inserts a release memory fence, ensuring that no stores before this fence are reordered with any loads/stores after the fence.
-
loadLoadFence
public static void loadLoadFence()
Inserts a load memory fence, ensuring that no loads before the fence are reordered with any loads after the fence.
-
storeStoreFence
public static void storeStoreFence()
Inserts a store memory fence, ensuring that no stores before the fence are reordered with any stores after the fence.
-
get
public final Object get(Object... args)
Gets the value of the field referenced by thisVarHandle
. This is a non-volatile operation.- Parameters:
args
- The arguments for this operation are determined by the field type (seeaccessModeType()
) and the access parameters (seecoordinateTypes()
).- Returns:
- The value stored in the field
-
set
public final void set(Object... args)
Sets the value of the field referenced by thisVarHandle
. This is a non-volatile operation.- Parameters:
args
- The arguments for this operation are determined by the field type (seeaccessModeType()
) and the access parameters (seecoordinateTypes()
).
-
getVolatile
public final Object getVolatile(Object... args)
Atomically gets the value of the field referenced by thisVarHandle
.- Parameters:
args
- The arguments for this operation are determined by the field type (seeaccessModeType()
) and the access parameters (seecoordinateTypes()
).- Returns:
- The value stored in the field
-
setVolatile
public final void setVolatile(Object... args)
Atomically sets the value of the field referenced by thisVarHandle
.- Parameters:
args
- The arguments for this operation are determined by the field type (seeaccessModeType()
) and the access parameters (seecoordinateTypes()
).
-
getOpaque
public final Object getOpaque(Object... args)
Gets the value of the field referenced by thisVarHandle
. The operation is in program order, but does enforce ordering with respect to other threads.- Parameters:
args
- The arguments for this operation are determined by the field type (seeaccessModeType()
) and the access parameters (seecoordinateTypes()
).- Returns:
- The value stored in the field
-
setOpaque
public final void setOpaque(Object... args)
Sets the value of the field referenced by thisVarHandle
. The operation is in program order, but does enforce ordering with respect to other threads.- Parameters:
args
- The arguments for this operation are determined by the field type (seeaccessModeType()
) and the access parameters (seecoordinateTypes()
).
-
getAcquire
public final Object getAcquire(Object... args)
Gets the value of the field referenced by thisVarHandle
using acquire semantics. Preceding loads will not be reordered with subsequent loads/stores.- Parameters:
args
- The arguments for this operation are determined by the field type (seeaccessModeType()
) and the access parameters (seecoordinateTypes()
).- Returns:
- The value stored in the field
-
setRelease
public final void setRelease(Object... args)
Sets the value of the field referenced by thisVarHandle
using acquire semantics. Preceding stores will not be reordered with subsequent loads/stores.- Parameters:
args
- The arguments for this operation are determined by the field type (seeaccessModeType()
) and the access parameters (seecoordinateTypes()
).
-
compareAndSet
public final boolean compareAndSet(Object... args)
Atomically sets the value of the field referenced by thisVarHandle
if the field value before this operation equaled the expected value. The get operation has the memory semantics ofgetVolatile(Object...)
. The set operation has the memory semantics ofsetVolatile(Object...)
.- Parameters:
args
- The arguments for this operation are determined by the field type (seeaccessModeType()
) and the access parameters (seecoordinateTypes()
).- Returns:
- A boolean value indicating whether the field was updated
-
compareAndExchange
public final Object compareAndExchange(Object... args)
Atomically sets the value of the field referenced by thisVarHandle
if the field value before this operation equaled the expected value. The get operation has the memory semantics ofgetVolatile(Object...)
. The set operation has the memory semantics ofsetVolatile(Object...)
.- Parameters:
args
- The arguments for this operation are determined by the field type (seeaccessModeType()
) and the access parameters (seecoordinateTypes()
).- Returns:
- The value of the field before this operation
-
compareAndExchangeAcquire
public final Object compareAndExchangeAcquire(Object... args)
Atomically sets the value of the field referenced by thisVarHandle
if the field value before this operation equaled the expected value. The get operation has the memory semantics ofgetAcquire(Object...)
. The set operation has the memory semantics ofset(Object...)
.- Parameters:
args
- The arguments for this operation are determined by the field type (seeaccessModeType()
) and the access parameters (seecoordinateTypes()
).- Returns:
- The value of the field before this operation
-
compareAndExchangeRelease
public final Object compareAndExchangeRelease(Object... args)
Atomically sets the value of the field referenced by thisVarHandle
if the field value before this operation equaled the expected value. The get operation has the memory semantics ofget(Object...)
. The set operation has the memory semantics ofsetRelease(Object...)
.- Parameters:
args
- The arguments for this operation are determined by the field type (seeaccessModeType()
) and the access parameters (seecoordinateTypes()
).- Returns:
- The value of the field before this operation
-
weakCompareAndSet
public final boolean weakCompareAndSet(Object... args)
Sets the value of the field referenced by thisVarHandle
if the field value before this operation equaled the expected value. The get operation has the memory semantics ofget(Object...)
. The set operation has the memory semantics ofset(Object...)
.- Parameters:
args
- The arguments for this operation are determined by the field type (seeaccessModeType()
) and the access parameters (seecoordinateTypes()
).- Returns:
- A boolean value indicating whether the field was updated
-
weakCompareAndSetAcquire
public final boolean weakCompareAndSetAcquire(Object... args)
Sets the value of the field referenced by thisVarHandle
if the field value before this operation equaled the expected value. The get operation has the memory semantics ofgetAcquire(Object...)
. The set operation has the memory semantics ofset(Object...)
.- Parameters:
args
- The arguments for this operation are determined by the field type (seeaccessModeType()
) and the access parameters (seecoordinateTypes()
).- Returns:
- A boolean value indicating whether the field was updated
-
weakCompareAndSetRelease
public final boolean weakCompareAndSetRelease(Object... args)
Sets the value of the field referenced by thisVarHandle
if the field value before this operation equaled the expected value. The get operation has the memory semantics ofget(Object...)
. The set operation has the memory semantics ofsetRelease(Object...)
.- Parameters:
args
- The arguments for this operation are determined by the field type (seeaccessModeType()
) and the access parameters (seecoordinateTypes()
).- Returns:
- A boolean value indicating whether the field was updated
-
weakCompareAndSetPlain
public final boolean weakCompareAndSetPlain(Object... args)
Sets the value of the field referenced by thisVarHandle
if the field value before this operation equaled the expected value. The get operation has the memory semantics ofget(Object...)
. The set operation has the memory semantics ofset(Object...)
.- Parameters:
args
- The arguments for this operation are determined by the field type (seeaccessModeType()
) and the access parameters (seecoordinateTypes()
).- Returns:
- A boolean value indicating whether the field was updated
-
getAndSet
public final Object getAndSet(Object... args)
Atomically sets the value of the field referenced by thisVarHandle
and returns the value of the field prior to the update. The get operation has the memory semantics ofgetVolatile(Object...)
. The set operation has the memory semantics ofsetVolatile(Object...)
.- Parameters:
args
- The arguments for this operation are determined by the field type (seeaccessModeType()
) and the access parameters (seecoordinateTypes()
).- Returns:
- The value stored in the field prior to the update
-
getAndSetAcquire
public final Object getAndSetAcquire(Object... args)
Atomically sets the value of the field referenced by thisVarHandle
and returns the value of the field prior to the update. The get operation has the memory semantics ofgetAcquire(Object...)
. The set operation has the memory semantics ofset(Object...)
.- Parameters:
args
- The arguments for this operation are determined by the field type (seeaccessModeType()
) and the access parameters (seecoordinateTypes()
).- Returns:
- The value stored in the field prior to the update
-
getAndSetRelease
public final Object getAndSetRelease(Object... args)
Atomically sets the value of the field referenced by thisVarHandle
and returns the value of the field prior to the update. The get operation has the memory semantics ofget(Object...)
. The set operation has the memory semantics ofsetRelease(Object...)
.- Parameters:
args
- The arguments for this operation are determined by the field type (seeaccessModeType()
) and the access parameters (seecoordinateTypes()
).- Returns:
- The value stored in the field prior to the update
-
getAndAdd
public final Object getAndAdd(Object... args)
Atomically adds to the current value of the field referenced by thisVarHandle
and returns the value of the field prior to the update. The get operation has the memory semantics ofgetVolatile(Object...)
. The set operation has the memory semantics ofsetVolatile(Object...)
.- Parameters:
args
- The arguments for this operation are determined by the field type (seeaccessModeType()
) and the access parameters (seecoordinateTypes()
).- Returns:
- The value stored in the field prior to the update
-
getAndAddAcquire
public final Object getAndAddAcquire(Object... args)
Atomically adds to the current value of the field referenced by thisVarHandle
and returns the value of the field prior to the update. The get operation has the memory semantics ofgetAcquire(Object...)
. The set operation has the memory semantics ofset(Object...)
.- Parameters:
args
- The arguments for this operation are determined by the field type (seeaccessModeType()
) and the access parameters (seecoordinateTypes()
).- Returns:
- The value stored in the field prior to the update
-
getAndAddRelease
public final Object getAndAddRelease(Object... args)
Atomically adds to the current value of the field referenced by thisVarHandle
and returns the value of the field prior to the update. The get operation has the memory semantics ofget(Object...)
. The set operation has the memory semantics ofsetRelease(Object...)
.- Parameters:
args
- The arguments for this operation are determined by the field type (seeaccessModeType()
) and the access parameters (seecoordinateTypes()
).- Returns:
- The value stored in the field prior to the update
-
getAndBitwiseAnd
public final Object getAndBitwiseAnd(Object... args)
Atomically ANDs the provided value and the current value of the field referenced by thisVarHandle
and returns the value of the field prior to the update. The get operation has the memory semantics ofgetVolatile(Object...)
. The set operation has the memory semantics ofsetVolatile(Object...)
.- Parameters:
args
- The arguments for this operation are determined by the field type (seeaccessModeType()
) and the access parameters (seecoordinateTypes()
).- Returns:
- The value stored in the field prior to the update
-
getAndBitwiseAndAcquire
public final Object getAndBitwiseAndAcquire(Object... args)
Atomically ANDs the provided value and the current value of the field referenced by thisVarHandle
and returns the value of the field prior to the update. The get operation has the memory semantics ofgetAcquire(Object...)
. The set operation has the memory semantics ofset(Object...)
.- Parameters:
args
- The arguments for this operation are determined by the field type (seeaccessModeType()
) and the access parameters (seecoordinateTypes()
).- Returns:
- The value stored in the field prior to the update
-
getAndBitwiseAndRelease
public final Object getAndBitwiseAndRelease(Object... args)
Atomically ANDs the provided value and the current value of the field referenced by thisVarHandle
and returns the value of the field prior to the update. The get operation has the memory semantics of(Object...)
. The set operation has the memory semantics ofsetRelease(Object...)
.- Parameters:
args
- The arguments for this operation are determined by the field type (seeaccessModeType()
) and the access parameters (seecoordinateTypes()
).- Returns:
- The value stored in the field prior to the update
-
getAndBitwiseOr
public final Object getAndBitwiseOr(Object... args)
Atomically ORs the provided value and the current value of the field referenced by thisVarHandle
and returns the value of the field prior to the update. The get operation has the memory semantics ofgetVolatile(Object...)
. The set operation has the memory semantics ofsetVolatile(Object...)
.- Parameters:
args
- The arguments for this operation are determined by the field type (seeaccessModeType()
) and the access parameters (seecoordinateTypes()
).- Returns:
- The value stored in the field prior to the update
-
getAndBitwiseOrAcquire
public final Object getAndBitwiseOrAcquire(Object... args)
Atomically ORs the provided value and the current value of the field referenced by thisVarHandle
and returns the value of the field prior to the update. The get operation has the memory semantics ofgetAcquire(Object...)
. The set operation has the memory semantics ofset(Object...)
.- Parameters:
args
- The arguments for this operation are determined by the field type (seeaccessModeType()
) and the access parameters (seecoordinateTypes()
).- Returns:
- The value stored in the field prior to the update
-
getAndBitwiseOrRelease
public final Object getAndBitwiseOrRelease(Object... args)
Atomically ORs the provided value and the current value of the field referenced by thisVarHandle
and returns the value of the field prior to the update. The get operation has the memory semantics ofget(Object...)
. The set operation has the memory semantics ofsetRelease(Object...)
.- Parameters:
args
- The arguments for this operation are determined by the field type (seeaccessModeType()
) and the access parameters (seecoordinateTypes()
).- Returns:
- The value stored in the field prior to the update
-
getAndBitwiseXor
public final Object getAndBitwiseXor(Object... args)
Atomically XORs the provided value and the current value of the field referenced by thisVarHandle
and returns the value of the field prior to the update. The get operation has the memory semantics ofgetVolatile(Object...)
. The set operation has the memory semantics ofsetVolatile(Object...)
.- Parameters:
args
- The arguments for this operation are determined by the field type (seeaccessModeType()
) and the access parameters (seecoordinateTypes()
).- Returns:
- The value stored in the field prior to the update
-
getAndBitwiseXorAcquire
public final Object getAndBitwiseXorAcquire(Object... args)
Atomically XORs the provided value and the current value of the field referenced by thisVarHandle
and returns the value of the field prior to the update. The get operation has the memory semantics ofgetAcquire(Object...)
. The set operation has the memory semantics ofset(Object...)
.- Parameters:
args
- The arguments for this operation are determined by the field type (seeaccessModeType()
) and the access parameters (seecoordinateTypes()
).- Returns:
- The value stored in the field prior to the update
-
getAndBitwiseXorRelease
public final Object getAndBitwiseXorRelease(Object... args)
Atomically XORs the provided value and the current value of the field referenced by thisVarHandle
and returns the value of the field prior to the update. The get operation has the memory semantics ofget(Object...)
. The set operation has the memory semantics ofsetRelease(Object...)
.- Parameters:
args
- The arguments for this operation are determined by the field type (seeaccessModeType()
) and the access parameters (seecoordinateTypes()
).- Returns:
- The value stored in the field prior to the update
-
-