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
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.booleangetBoolean(Object obj, long offset)Gets abooleanfield from the given object.bytegetByte(Object obj, long offset)Gets abytefield from the given object.floatgetFloat(Object obj, long offset)Gets anfloatfield from 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).voidputBoolean(Object obj, long offset, boolean newValue)Stores abooleanfield into the given object.voidputByte(Object obj, long offset, byte newValue)Stores abytefield into the given object.voidputFloat(Object obj, long offset, float newValue)Stores anfloatfield into the given 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 Details
-
getUnsafe
Gets the unique instance of this class. This is only allowed in very limited situations. -
objectFieldOffset
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
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
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
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
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
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
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
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
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
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
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
Gets anintfield from the given object.- Parameters:
obj- non-null; object containing the fieldoffset- offset to the field withinobj- Returns:
- the retrieved value
-
getBoolean
Gets abooleanfield from the given object.- Parameters:
obj- non-null; object containing the fieldoffset- offset to the field withinobj- Returns:
- the retrieved value
-
getByte
Gets abytefield from the given object.- Parameters:
obj- non-null; object containing the fieldoffset- offset to the field withinobj- Returns:
- the retrieved value
-
putInt
Stores anintfield into the given object.- Parameters:
obj- non-null; object containing the fieldoffset- offset to the field withinobjnewValue- the value to store
-
putBoolean
Stores abooleanfield into the given object.- Parameters:
obj- non-null; object containing the fieldoffset- offset to the field withinobjnewValue- the value to store
-
putByte
Stores abytefield into the given object.- Parameters:
obj- non-null; object containing the fieldoffset- offset to the field withinobjnewValue- the value to store
-
getFloat
Gets anfloatfield from the given object.- Parameters:
obj- non-null; object containing the fieldoffset- offset to the field withinobj- Returns:
- the retrieved value
-
putFloat
Stores anfloatfield into the given object.- Parameters:
obj- non-null; object containing the fieldoffset- offset to the field withinobjnewValue- the value to store
-
putOrderedInt
Lazy set an int field. -
getLong
Gets alongfield from the given object.- Parameters:
obj- non-null; object containing the fieldoffset- offset to the field withinobj- Returns:
- the retrieved value
-
putLong
Stores alongfield into the given object.- Parameters:
obj- non-null; object containing the fieldoffset- offset to the field withinobjnewValue- the value to store
-
putOrderedLong
Lazy set a long field. -
getObject
Gets anObjectfield from the given object.- Parameters:
obj- non-null; object containing the fieldoffset- offset to the field withinobj- Returns:
- the retrieved value
-
putObject
Stores anObjectfield into the given object.- Parameters:
obj- non-null; object containing the fieldoffset- offset to the field withinobjnewValue- the value to store
-
putOrderedObject
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
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
-
allocateInstance
Allocates an instance of the given class without running the constructor. The class'will be run, if necessary.
-