Package sun.misc

Class Unsafe


  • public final class Unsafe
    extends Object
    The package name notwithstanding, this class is the quasi-standard way for Java code to gain access to and use functionality which, when unsupervised, would allow one to break the pointer/type safety of Java.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Object allocateInstance​(Class<?> c)
      Allocates an instance of the given class without running the constructor.
      int arrayBaseOffset​(Class clazz)
      Gets the offset from the start of an array object's memory to the memory used to store its initial (zeroeth) element.
      int arrayIndexScale​(Class clazz)
      Gets the size of each element of the given array class.
      boolean compareAndSwapInt​(Object obj, long offset, int expectedValue, int newValue)
      Performs a compare-and-set operation on an int field within the given object.
      boolean compareAndSwapLong​(Object obj, long offset, long expectedValue, long newValue)
      Performs a compare-and-set operation on a long field within the given object.
      boolean compareAndSwapObject​(Object obj, long offset, Object expectedValue, Object newValue)
      Performs a compare-and-set operation on an Object field (that is, a reference field) within the given object.
      int getInt​(Object obj, long offset)
      Gets an int field from the given object.
      int getIntVolatile​(Object obj, long offset)
      Gets an int field from the given object, using volatile semantics.
      long getLong​(Object obj, long offset)
      Gets a long field from the given object.
      long getLongVolatile​(Object obj, long offset)
      Gets a long field from the given object, using volatile semantics.
      Object getObject​(Object obj, long offset)
      Gets an Object field from the given object.
      Object getObjectVolatile​(Object obj, long offset)
      Gets an Object field from the given object, using volatile semantics.
      static Unsafe getUnsafe()
      Gets the unique instance of this class.
      long objectFieldOffset​(Field field)
      Gets the raw byte offset from the start of an object's memory to the memory used to store the indicated instance field.
      void park​(boolean absolute, long time)
      Parks the calling thread for the specified amount of time, unless the "permit" for the thread is already available (due to a previous call to unpark(java.lang.Object).
      void putInt​(Object obj, long offset, int newValue)
      Stores an int field into the given object.
      void putIntVolatile​(Object obj, long offset, int newValue)
      Stores an int field into the given object, using volatile semantics.
      void putLong​(Object obj, long offset, long newValue)
      Stores a long field into the given object.
      void putLongVolatile​(Object obj, long offset, long newValue)
      Stores a long field into the given object, using volatile semantics.
      void putObject​(Object obj, long offset, Object newValue)
      Stores an Object field into the given object.
      void putObjectVolatile​(Object obj, long offset, Object newValue)
      Stores an Object field into the given object, using volatile semantics.
      void putOrderedInt​(Object obj, long offset, int newValue)
      Lazy set an int field.
      void putOrderedLong​(Object obj, long offset, long newValue)
      Lazy set a long field.
      void putOrderedObject​(Object obj, long offset, Object newValue)
      Lazy set an object field.
      void unpark​(Object obj)
      Unparks the given object, which must be a Thread.
    • Method Detail

      • getUnsafe

        public static Unsafe getUnsafe()
        Gets the unique instance of this class. This is only allowed in very limited situations.
      • objectFieldOffset

        public long objectFieldOffset​(Field field)
        Gets the raw byte offset from the start of an object's memory to the memory used to store the indicated instance field.
        Parameters:
        field - non-null; the field in question, which must be an instance field
        Returns:
        the offset to the field
      • arrayBaseOffset

        public int arrayBaseOffset​(Class clazz)
        Gets the offset from the start of an array object's memory to the memory used to store its initial (zeroeth) element.
        Parameters:
        clazz - non-null; class in question; must be an array class
        Returns:
        the offset to the initial element
      • arrayIndexScale

        public int arrayIndexScale​(Class clazz)
        Gets the size of each element of the given array class.
        Parameters:
        clazz - non-null; class in question; must be an array class
        Returns:
        > 0; the size of each element of the array
      • compareAndSwapInt

        public boolean compareAndSwapInt​(Object obj,
                                         long offset,
                                         int expectedValue,
                                         int newValue)
        Performs a compare-and-set operation on an int field within the given object.
        Parameters:
        obj - non-null; object containing the field
        offset - offset to the field within obj
        expectedValue - expected value of the field
        newValue - new value to store in the field if the contents are as expected
        Returns:
        true if the new value was in fact stored, and false if not
      • compareAndSwapLong

        public boolean compareAndSwapLong​(Object obj,
                                          long offset,
                                          long expectedValue,
                                          long newValue)
        Performs a compare-and-set operation on a long field within the given object.
        Parameters:
        obj - non-null; object containing the field
        offset - offset to the field within obj
        expectedValue - expected value of the field
        newValue - new value to store in the field if the contents are as expected
        Returns:
        true if the new value was in fact stored, and false if not
      • compareAndSwapObject

        public boolean compareAndSwapObject​(Object obj,
                                            long offset,
                                            Object expectedValue,
                                            Object newValue)
        Performs a compare-and-set operation on an Object field (that is, a reference field) within the given object.
        Parameters:
        obj - non-null; object containing the field
        offset - offset to the field within obj
        expectedValue - expected value of the field
        newValue - new value to store in the field if the contents are as expected
        Returns:
        true if the new value was in fact stored, and false if not
      • getIntVolatile

        public int getIntVolatile​(Object obj,
                                  long offset)
        Gets an int field from the given object, using volatile semantics.
        Parameters:
        obj - non-null; object containing the field
        offset - offset to the field within obj
        Returns:
        the retrieved value
      • putIntVolatile

        public void putIntVolatile​(Object obj,
                                   long offset,
                                   int newValue)
        Stores an int field into the given object, using volatile semantics.
        Parameters:
        obj - non-null; object containing the field
        offset - offset to the field within obj
        newValue - the value to store
      • getLongVolatile

        public long getLongVolatile​(Object obj,
                                    long offset)
        Gets a long field from the given object, using volatile semantics.
        Parameters:
        obj - non-null; object containing the field
        offset - offset to the field within obj
        Returns:
        the retrieved value
      • putLongVolatile

        public void putLongVolatile​(Object obj,
                                    long offset,
                                    long newValue)
        Stores a long field into the given object, using volatile semantics.
        Parameters:
        obj - non-null; object containing the field
        offset - offset to the field within obj
        newValue - the value to store
      • getObjectVolatile

        public Object getObjectVolatile​(Object obj,
                                        long offset)
        Gets an Object field from the given object, using volatile semantics.
        Parameters:
        obj - non-null; object containing the field
        offset - offset to the field within obj
        Returns:
        the retrieved value
      • putObjectVolatile

        public void putObjectVolatile​(Object obj,
                                      long offset,
                                      Object newValue)
        Stores an Object field into the given object, using volatile semantics.
        Parameters:
        obj - non-null; object containing the field
        offset - offset to the field within obj
        newValue - the value to store
      • getInt

        public int getInt​(Object obj,
                          long offset)
        Gets an int field from the given object.
        Parameters:
        obj - non-null; object containing the field
        offset - offset to the field within obj
        Returns:
        the retrieved value
      • putInt

        public void putInt​(Object obj,
                           long offset,
                           int newValue)
        Stores an int field into the given object.
        Parameters:
        obj - non-null; object containing the field
        offset - offset to the field within obj
        newValue - the value to store
      • putOrderedInt

        public void putOrderedInt​(Object obj,
                                  long offset,
                                  int newValue)
        Lazy set an int field.
      • getLong

        public long getLong​(Object obj,
                            long offset)
        Gets a long field from the given object.
        Parameters:
        obj - non-null; object containing the field
        offset - offset to the field within obj
        Returns:
        the retrieved value
      • putLong

        public void putLong​(Object obj,
                            long offset,
                            long newValue)
        Stores a long field into the given object.
        Parameters:
        obj - non-null; object containing the field
        offset - offset to the field within obj
        newValue - the value to store
      • putOrderedLong

        public void putOrderedLong​(Object obj,
                                   long offset,
                                   long newValue)
        Lazy set a long field.
      • getObject

        public Object getObject​(Object obj,
                                long offset)
        Gets an Object field from the given object.
        Parameters:
        obj - non-null; object containing the field
        offset - offset to the field within obj
        Returns:
        the retrieved value
      • putObject

        public void putObject​(Object obj,
                              long offset,
                              Object newValue)
        Stores an Object field into the given object.
        Parameters:
        obj - non-null; object containing the field
        offset - offset to the field within obj
        newValue - the value to store
      • putOrderedObject

        public void putOrderedObject​(Object obj,
                                     long offset,
                                     Object newValue)
        Lazy set an object field.
      • park

        public void park​(boolean absolute,
                         long time)
        Parks the calling thread for the specified amount of time, unless the "permit" for the thread is already available (due to a previous call to unpark(java.lang.Object). This method may also return spuriously (that is, without the thread being told to unpark and without the indicated amount of time elapsing).

        See LockSupport for more in-depth information of the behavior of this method.

        Parameters:
        absolute - whether the given time value is absolute milliseconds-since-the-epoch (true) or relative nanoseconds-from-now (false)
        time - the (absolute millis or relative nanos) time value
      • unpark

        public void unpark​(Object obj)
        Unparks the given object, which must be a Thread.

        See LockSupport for more in-depth information of the behavior of this method.

        Parameters:
        obj - non-null; the object to unpark
      • allocateInstance

        public Object allocateInstance​(Class<?> c)
        Allocates an instance of the given class without running the constructor. The class' will be run, if necessary.