public final class Unsafe extends Object
| Modifier and Type | Method and 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 |
getAndAddInt(Object o,
long offset,
int delta)
Atomically adds the given value to the current value of a field
or array element within the given object
o
at the given offset. |
long |
getAndAddLong(Object o,
long offset,
long delta)
Atomically adds the given value to the current value of a field
or array element within the given object
o
at the given offset. |
int |
getAndSetInt(Object o,
long offset,
int newValue)
Atomically exchanges the given value with the current value of
a field or array element within the given object
o
at the given offset. |
long |
getAndSetLong(Object o,
long offset,
long newValue)
Atomically exchanges the given value with the current value of
a field or array element within the given object
o
at the given offset. |
Object |
getAndSetObject(Object o,
long offset,
Object newValue)
Atomically exchanges the given reference value with the current
reference value of a field or array element within the given
object
o at the given offset. |
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. |
public static Unsafe getUnsafe()
public long objectFieldOffset(Field field)
field - non-null; the field in question, which must be an
instance fieldpublic int arrayBaseOffset(Class clazz)
clazz - non-null; class in question; must be an array classpublic int arrayIndexScale(Class clazz)
clazz - non-null; class in question; must be an array classpublic boolean compareAndSwapInt(Object obj, long offset, int expectedValue, int newValue)
int
field within the given object.obj - non-null; object containing the fieldoffset - offset to the field within objexpectedValue - expected value of the fieldnewValue - new value to store in the field if the contents are
as expectedtrue if the new value was in fact stored, and
false if notpublic boolean compareAndSwapLong(Object obj, long offset, long expectedValue, long newValue)
long
field within the given object.obj - non-null; object containing the fieldoffset - offset to the field within objexpectedValue - expected value of the fieldnewValue - new value to store in the field if the contents are
as expectedtrue if the new value was in fact stored, and
false if notpublic boolean compareAndSwapObject(Object obj, long offset, Object expectedValue, Object newValue)
Object
field (that is, a reference field) within the given object.obj - non-null; object containing the fieldoffset - offset to the field within objexpectedValue - expected value of the fieldnewValue - new value to store in the field if the contents are
as expectedtrue if the new value was in fact stored, and
false if notpublic int getIntVolatile(Object obj, long offset)
int field from the given object,
using volatile semantics.obj - non-null; object containing the fieldoffset - offset to the field within objpublic void putIntVolatile(Object obj, long offset, int newValue)
int field into the given object,
using volatile semantics.obj - non-null; object containing the fieldoffset - offset to the field within objnewValue - the value to storepublic long getLongVolatile(Object obj, long offset)
long field from the given object,
using volatile semantics.obj - non-null; object containing the fieldoffset - offset to the field within objpublic void putLongVolatile(Object obj, long offset, long newValue)
long field into the given object,
using volatile semantics.obj - non-null; object containing the fieldoffset - offset to the field within objnewValue - the value to storepublic Object getObjectVolatile(Object obj, long offset)
Object field from the given object,
using volatile semantics.obj - non-null; object containing the fieldoffset - offset to the field within objpublic void putObjectVolatile(Object obj, long offset, Object newValue)
Object field into the given object,
using volatile semantics.obj - non-null; object containing the fieldoffset - offset to the field within objnewValue - the value to storepublic int getInt(Object obj, long offset)
int field from the given object.obj - non-null; object containing the fieldoffset - offset to the field within objpublic void putInt(Object obj, long offset, int newValue)
int field into the given object.obj - non-null; object containing the fieldoffset - offset to the field within objnewValue - the value to storepublic void putOrderedInt(Object obj, long offset, int newValue)
public long getLong(Object obj, long offset)
long field from the given object.obj - non-null; object containing the fieldoffset - offset to the field within objpublic void putLong(Object obj, long offset, long newValue)
long field into the given object.obj - non-null; object containing the fieldoffset - offset to the field within objnewValue - the value to storepublic void putOrderedLong(Object obj, long offset, long newValue)
public Object getObject(Object obj, long offset)
Object field from the given object.obj - non-null; object containing the fieldoffset - offset to the field within objpublic void putObject(Object obj, long offset, Object newValue)
Object field into the given object.obj - non-null; object containing the fieldoffset - offset to the field within objnewValue - the value to storepublic void putOrderedObject(Object obj, long offset, Object newValue)
public void park(boolean absolute,
long time)
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.
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 valuepublic void unpark(Object obj)
Thread.
See LockSupport for more
in-depth information of the behavior of this method.
obj - non-null; the object to unparkpublic Object allocateInstance(Class<?> c)
public final int getAndAddInt(Object o, long offset, int delta)
o
at the given offset.o - object/array to update the field/element inoffset - field/element offsetdelta - the value to addpublic final long getAndAddLong(Object o, long offset, long delta)
o
at the given offset.o - object/array to update the field/element inoffset - field/element offsetdelta - the value to addpublic final int getAndSetInt(Object o, long offset, int newValue)
o
at the given offset.o - object/array to update the field/element inoffset - field/element offsetnewValue - new valuepublic final long getAndSetLong(Object o, long offset, long newValue)
o
at the given offset.o - object/array to update the field/element inoffset - field/element offsetnewValue - new valuepublic final Object getAndSetObject(Object o, long offset, Object newValue)
o at the given offset.o - object/array to update the field/element inoffset - field/element offsetnewValue - new value