Module java.base

Class 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. See VarHandle.AccessMode for supported operations. VarHandle instances are created through the MethodHandles factory API.
    • Method Detail

      • varType

        public final Class<?> varType()
        Get the type of the field this VarHandle refers to.
        Returns:
        The field type
      • coordinateTypes

        public final List<Class<?>> coordinateTypes()
        Different parameters are required in order to access the field referenced by this VarHandle depending on the type of the VarHandle, e.g. instance field, static field, or array element. This method provides a list of the access parameters for this VarHandle instance.
        Returns:
        The parameters required to access the field.
      • 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 this VarHandle. This is a non-volatile operation.
        Parameters:
        args - The arguments for this operation are determined by the field type (see accessModeType()) and the access parameters (see coordinateTypes()).
        Returns:
        The value stored in the field
      • set

        public final void set​(Object... args)
        Sets the value of the field referenced by this VarHandle. This is a non-volatile operation.
        Parameters:
        args - The arguments for this operation are determined by the field type (see accessModeType()) and the access parameters (see coordinateTypes()).
      • getVolatile

        public final Object getVolatile​(Object... args)
        Atomically gets the value of the field referenced by this VarHandle.
        Parameters:
        args - The arguments for this operation are determined by the field type (see accessModeType()) and the access parameters (see coordinateTypes()).
        Returns:
        The value stored in the field
      • setVolatile

        public final void setVolatile​(Object... args)
        Atomically sets the value of the field referenced by this VarHandle.
        Parameters:
        args - The arguments for this operation are determined by the field type (see accessModeType()) and the access parameters (see coordinateTypes()).
      • getOpaque

        public final Object getOpaque​(Object... args)
        Gets the value of the field referenced by this VarHandle. 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 (see accessModeType()) and the access parameters (see coordinateTypes()).
        Returns:
        The value stored in the field
      • setOpaque

        public final void setOpaque​(Object... args)
        Sets the value of the field referenced by this VarHandle. 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 (see accessModeType()) and the access parameters (see coordinateTypes()).
      • getAcquire

        public final Object getAcquire​(Object... args)
        Gets the value of the field referenced by this VarHandle 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 (see accessModeType()) and the access parameters (see coordinateTypes()).
        Returns:
        The value stored in the field
      • setRelease

        public final void setRelease​(Object... args)
        Sets the value of the field referenced by this VarHandle 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 (see accessModeType()) and the access parameters (see coordinateTypes()).
      • compareAndSet

        public final boolean compareAndSet​(Object... args)
        Atomically sets the value of the field referenced by this VarHandle if the field value before this operation equaled the expected value. The get operation has the memory semantics of getVolatile(Object...). The set operation has the memory semantics of setVolatile(Object...).
        Parameters:
        args - The arguments for this operation are determined by the field type (see accessModeType()) and the access parameters (see coordinateTypes()).
        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 this VarHandle if the field value before this operation equaled the expected value. The get operation has the memory semantics of getVolatile(Object...). The set operation has the memory semantics of setVolatile(Object...).
        Parameters:
        args - The arguments for this operation are determined by the field type (see accessModeType()) and the access parameters (see coordinateTypes()).
        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 this VarHandle if the field value before this operation equaled the expected value. The get operation has the memory semantics of getAcquire(Object...). The set operation has the memory semantics of set(Object...).
        Parameters:
        args - The arguments for this operation are determined by the field type (see accessModeType()) and the access parameters (see coordinateTypes()).
        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 this VarHandle if the field value before this operation equaled the expected value. The get operation has the memory semantics of get(Object...). The set operation has the memory semantics of setRelease(Object...).
        Parameters:
        args - The arguments for this operation are determined by the field type (see accessModeType()) and the access parameters (see coordinateTypes()).
        Returns:
        The value of the field before this operation
      • weakCompareAndSet

        public final boolean weakCompareAndSet​(Object... args)
        Sets the value of the field referenced by this VarHandle if the field value before this operation equaled the expected value. The get operation has the memory semantics of get(Object...). The set operation has the memory semantics of set(Object...).
        Parameters:
        args - The arguments for this operation are determined by the field type (see accessModeType()) and the access parameters (see coordinateTypes()).
        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 this VarHandle if the field value before this operation equaled the expected value. The get operation has the memory semantics of getAcquire(Object...). The set operation has the memory semantics of set(Object...).
        Parameters:
        args - The arguments for this operation are determined by the field type (see accessModeType()) and the access parameters (see coordinateTypes()).
        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 this VarHandle if the field value before this operation equaled the expected value. The get operation has the memory semantics of get(Object...). The set operation has the memory semantics of setRelease(Object...).
        Parameters:
        args - The arguments for this operation are determined by the field type (see accessModeType()) and the access parameters (see coordinateTypes()).
        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 this VarHandle if the field value before this operation equaled the expected value. The get operation has the memory semantics of get(Object...). The set operation has the memory semantics of set(Object...).
        Parameters:
        args - The arguments for this operation are determined by the field type (see accessModeType()) and the access parameters (see coordinateTypes()).
        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 this VarHandle and returns the value of the field prior to the update. The get operation has the memory semantics of getVolatile(Object...). The set operation has the memory semantics of setVolatile(Object...).
        Parameters:
        args - The arguments for this operation are determined by the field type (see accessModeType()) and the access parameters (see coordinateTypes()).
        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 this VarHandle and returns the value of the field prior to the update. The get operation has the memory semantics of getAcquire(Object...). The set operation has the memory semantics of set(Object...).
        Parameters:
        args - The arguments for this operation are determined by the field type (see accessModeType()) and the access parameters (see coordinateTypes()).
        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 this VarHandle and returns the value of the field prior to the update. The get operation has the memory semantics of get(Object...). The set operation has the memory semantics of setRelease(Object...).
        Parameters:
        args - The arguments for this operation are determined by the field type (see accessModeType()) and the access parameters (see coordinateTypes()).
        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 this VarHandle and returns the value of the field prior to the update. The get operation has the memory semantics of getVolatile(Object...). The set operation has the memory semantics of setVolatile(Object...).
        Parameters:
        args - The arguments for this operation are determined by the field type (see accessModeType()) and the access parameters (see coordinateTypes()).
        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 this VarHandle and returns the value of the field prior to the update. The get operation has the memory semantics of getAcquire(Object...). The set operation has the memory semantics of set(Object...).
        Parameters:
        args - The arguments for this operation are determined by the field type (see accessModeType()) and the access parameters (see coordinateTypes()).
        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 this VarHandle and returns the value of the field prior to the update. The get operation has the memory semantics of get(Object...). The set operation has the memory semantics of setRelease(Object...).
        Parameters:
        args - The arguments for this operation are determined by the field type (see accessModeType()) and the access parameters (see coordinateTypes()).
        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 this VarHandle and returns the value of the field prior to the update. The get operation has the memory semantics of getVolatile(Object...). The set operation has the memory semantics of setVolatile(Object...).
        Parameters:
        args - The arguments for this operation are determined by the field type (see accessModeType()) and the access parameters (see coordinateTypes()).
        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 this VarHandle and returns the value of the field prior to the update. The get operation has the memory semantics of getAcquire(Object...). The set operation has the memory semantics of set(Object...).
        Parameters:
        args - The arguments for this operation are determined by the field type (see accessModeType()) and the access parameters (see coordinateTypes()).
        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 this VarHandle 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 of setRelease(Object...).
        Parameters:
        args - The arguments for this operation are determined by the field type (see accessModeType()) and the access parameters (see coordinateTypes()).
        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 this VarHandle and returns the value of the field prior to the update. The get operation has the memory semantics of getVolatile(Object...). The set operation has the memory semantics of setVolatile(Object...).
        Parameters:
        args - The arguments for this operation are determined by the field type (see accessModeType()) and the access parameters (see coordinateTypes()).
        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 this VarHandle and returns the value of the field prior to the update. The get operation has the memory semantics of getAcquire(Object...). The set operation has the memory semantics of set(Object...).
        Parameters:
        args - The arguments for this operation are determined by the field type (see accessModeType()) and the access parameters (see coordinateTypes()).
        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 this VarHandle and returns the value of the field prior to the update. The get operation has the memory semantics of get(Object...). The set operation has the memory semantics of setRelease(Object...).
        Parameters:
        args - The arguments for this operation are determined by the field type (see accessModeType()) and the access parameters (see coordinateTypes()).
        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 this VarHandle and returns the value of the field prior to the update. The get operation has the memory semantics of getVolatile(Object...). The set operation has the memory semantics of setVolatile(Object...).
        Parameters:
        args - The arguments for this operation are determined by the field type (see accessModeType()) and the access parameters (see coordinateTypes()).
        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 this VarHandle and returns the value of the field prior to the update. The get operation has the memory semantics of getAcquire(Object...). The set operation has the memory semantics of set(Object...).
        Parameters:
        args - The arguments for this operation are determined by the field type (see accessModeType()) and the access parameters (see coordinateTypes()).
        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 this VarHandle and returns the value of the field prior to the update. The get operation has the memory semantics of get(Object...). The set operation has the memory semantics of setRelease(Object...).
        Parameters:
        args - The arguments for this operation are determined by the field type (see accessModeType()) and the access parameters (see coordinateTypes()).
        Returns:
        The value stored in the field prior to the update