Package sun.misc
Class Unsafe
- java.lang.Object
-
- sun.misc.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 ObjectallocateInstance(Class<?> c)Allocates an instance of the given class without running the constructor.intarrayBaseOffset(Class clazz)Gets the offset from the start of an array object's memory to the memory used to store its initial (zeroeth) element.intarrayIndexScale(Class clazz)Gets the size of each element of the given array class.booleancompareAndSwapInt(Object obj, long offset, int expectedValue, int newValue)Performs a compare-and-set operation on anintfield within the given object.booleancompareAndSwapLong(Object obj, long offset, long expectedValue, long newValue)Performs a compare-and-set operation on alongfield within the given object.booleancompareAndSwapObject(Object obj, long offset, Object expectedValue, Object newValue)Performs a compare-and-set operation on anObjectfield (that is, a reference field) within the given object.intgetInt(Object obj, long offset)Gets anintfield from the given object.intgetIntVolatile(Object obj, long offset)Gets anintfield from the given object, usingvolatilesemantics.longgetLong(Object obj, long offset)Gets alongfield from the given object.longgetLongVolatile(Object obj, long offset)Gets alongfield from the given object, usingvolatilesemantics.ObjectgetObject(Object obj, long offset)Gets anObjectfield from the given object.ObjectgetObjectVolatile(Object obj, long offset)Gets anObjectfield from the given object, usingvolatilesemantics.static UnsafegetUnsafe()Gets the unique instance of this class.longobjectFieldOffset(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.voidpark(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 tounpark(java.lang.Object).voidputInt(Object obj, long offset, int newValue)Stores anintfield into the given object.voidputIntVolatile(Object obj, long offset, int newValue)Stores anintfield into the given object, usingvolatilesemantics.voidputLong(Object obj, long offset, long newValue)Stores alongfield into the given object.voidputLongVolatile(Object obj, long offset, long newValue)Stores alongfield into the given object, usingvolatilesemantics.voidputObject(Object obj, long offset, Object newValue)Stores anObjectfield into the given object.voidputObjectVolatile(Object obj, long offset, Object newValue)Stores anObjectfield into the given object, usingvolatilesemantics.voidputOrderedInt(Object obj, long offset, int newValue)Lazy set an int field.voidputOrderedLong(Object obj, long offset, long newValue)Lazy set a long field.voidputOrderedObject(Object obj, long offset, Object newValue)Lazy set an object field.voidunpark(Object obj)Unparks the given object, which must be aThread.
-
-
-
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 anintfield within the given object.- Parameters:
obj- non-null; object containing the fieldoffset- offset to the field withinobjexpectedValue- expected value of the fieldnewValue- new value to store in the field if the contents are as expected- Returns:
trueif the new value was in fact stored, andfalseif not
-
compareAndSwapLong
public boolean compareAndSwapLong(Object obj, long offset, long expectedValue, long newValue)
Performs a compare-and-set operation on alongfield within the given object.- Parameters:
obj- non-null; object containing the fieldoffset- offset to the field withinobjexpectedValue- expected value of the fieldnewValue- new value to store in the field if the contents are as expected- Returns:
trueif the new value was in fact stored, andfalseif not
-
compareAndSwapObject
public boolean compareAndSwapObject(Object obj, long offset, Object expectedValue, Object newValue)
Performs a compare-and-set operation on anObjectfield (that is, a reference field) within the given object.- Parameters:
obj- non-null; object containing the fieldoffset- offset to the field withinobjexpectedValue- expected value of the fieldnewValue- new value to store in the field if the contents are as expected- Returns:
trueif the new value was in fact stored, andfalseif not
-
getIntVolatile
public int getIntVolatile(Object obj, long offset)
Gets anintfield from the given object, usingvolatilesemantics.- Parameters:
obj- non-null; object containing the fieldoffset- offset to the field withinobj- Returns:
- the retrieved value
-
putIntVolatile
public void putIntVolatile(Object obj, long offset, int newValue)
Stores anintfield into the given object, usingvolatilesemantics.- Parameters:
obj- non-null; object containing the fieldoffset- offset to the field withinobjnewValue- the value to store
-
getLongVolatile
public long getLongVolatile(Object obj, long offset)
Gets alongfield from the given object, usingvolatilesemantics.- Parameters:
obj- non-null; object containing the fieldoffset- offset to the field withinobj- Returns:
- the retrieved value
-
putLongVolatile
public void putLongVolatile(Object obj, long offset, long newValue)
Stores alongfield into the given object, usingvolatilesemantics.- Parameters:
obj- non-null; object containing the fieldoffset- offset to the field withinobjnewValue- the value to store
-
getObjectVolatile
public Object getObjectVolatile(Object obj, long offset)
Gets anObjectfield from the given object, usingvolatilesemantics.- Parameters:
obj- non-null; object containing the fieldoffset- offset to the field withinobj- Returns:
- the retrieved value
-
putObjectVolatile
public void putObjectVolatile(Object obj, long offset, Object newValue)
Stores anObjectfield into the given object, usingvolatilesemantics.- Parameters:
obj- non-null; object containing the fieldoffset- offset to the field withinobjnewValue- the value to store
-
getInt
public int getInt(Object obj, long offset)
Gets anintfield from the given object.- Parameters:
obj- non-null; object containing the fieldoffset- offset to the field withinobj- Returns:
- the retrieved value
-
putInt
public void putInt(Object obj, long offset, int newValue)
Stores anintfield into the given object.- Parameters:
obj- non-null; object containing the fieldoffset- offset to the field withinobjnewValue- 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 alongfield from the given object.- Parameters:
obj- non-null; object containing the fieldoffset- offset to the field withinobj- Returns:
- the retrieved value
-
putLong
public void putLong(Object obj, long offset, long newValue)
Stores alongfield into the given object.- Parameters:
obj- non-null; object containing the fieldoffset- offset to the field withinobjnewValue- 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 anObjectfield from the given object.- Parameters:
obj- non-null; object containing the fieldoffset- offset to the field withinobj- Returns:
- the retrieved value
-
putObject
public void putObject(Object obj, long offset, Object newValue)
Stores anObjectfield into the given object.- Parameters:
obj- non-null; object containing the fieldoffset- offset to the field withinobjnewValue- 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 tounpark(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
LockSupportfor 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 aThread.See
LockSupportfor more in-depth information of the behavior of this method.- Parameters:
obj- non-null; the object to unpark
-
-