Java does not have direct access to the underlying operating system, but is accessed through local methods. The unsafe class provides hardware-level atomic operations that provide the following features:
1, through the unsafe class can allocate memory, can free memory;
The 3 local methods provided in the class Allocatememory, Reallocatememory, and freememory are used to allocate memory, expand memory, and free memory, corresponding to the 3 methods in the C language.
Public native long allocatememory (long L);p ublic native Long reallocatememory (long L, long L1);p ublic native void Freememo Ry (long L);
2, you can locate the memory location of a field of an object, you can also modify the field value of the object, even if it is private;
Positioning of the field:
The positioning of the fields of an object in Java may be implemented by the Staticfieldoffset method, which returns the memory address offset for a given field, which is unique and invariant for a given filed.
The Getintvolatile method gets the value of the integer field in the object that offsets the offset address, and supports the volatile load semantics.
The Getlong method gets the value of the long field in the object that corresponds to offset shift address
Array element positioning:
There are many constants in the unsafe class that end with Base_offset, such as Array_int_base_offset,array_byte_base_offset, which are obtained through the Arraybaseoffset method. The Arraybaseoffset method is a local method that can get the offset address of the first element of the array. There are many constants in the unsafe class that end with Index_scale, such as Array_int_index_scale, Array_byte_index_scale, and so on, and these constant values are obtained through the Arrayindexscale method. The Arrayindexscale method is also a local method that can get the conversion factor of an array, which is the incremental address of the elements in the array. Using Arraybaseoffset with Arrayindexscale, you can position each element in the array in memory.
Arrays, like Java objects, have an object header, which is stored in front of the actual data. The length of the head can be obtained by means of the Unsafe.arraybaseoffset (T[].class) method, where T is the type of the array element. The size of the array element can be obtained through the Unsafe.arrayindexscale (T[].class) method. This means that if you want to access the nth element of the type T, your offset offsets should be arrayoffset+n*arrayscale.
Public final class Unsafe {public static final int array_int_base_offset; public static final int array_int_index_scale; Public native long Staticfieldoffset (field field); public native int Getintvolatile (Object obj, long l); Public native long Getlong (Object obj, long l); public native int Arraybaseoffset (Class class1); public native int Arrayindexscale (Class class1); Static { Array_int_base_offset = Theunsafe.arraybaseoffset ([I); Array_int_index_scale = Theunsafe.arrayindexscale ([I); }}
3. Suspend and resume
Suspending a thread is implemented through the park method, and after calling park, the thread will block until a condition such as a timeout or interruption occurs. Unpark can terminate a suspended thread and return it to normal. The suspend operation of threads in the entire concurrency framework is encapsulated in the Locksupport class, with various versions of the Pack method in the Locksupport class, but eventually the Unsafe.park () method is called.
public class Locksupport {public static void Unpark (thread thread) {if (thread! = null) UNSAFE.UNPA RK (thread); public static void Park (Object blocker) {Thread t = thread.currentthread (); Setblocker (t, blocker); Unsafe.park (False, 0L); Setblocker (t, NULL); } public static void Parknanos (Object blocker, long Nanos) {if (Nanos > 0) {Thread t = thread.cu Rrentthread (); Setblocker (t, blocker); Unsafe.park (False, Nanos); Setblocker (t, NULL); }} public static void Parkuntil (Object blocker, long deadline) {Thread t = thread.currentthread (); Setblocker (t, blocker); Unsafe.park (true, Deadline); Setblocker (t, NULL); public static void Park () {Unsafe.park (false, 0L); } public static void Parknanos (Long Nanos) {if (Nanos > 0) Unsafe.park (false, Nanos); } public static void Parkuntil (longDeadline) {Unsafe.park (true, Deadline); }}
4. CAS operation
is achieved through the Compareandswapxxx method.
The/*** compares the values in the memory location of the offset at obj and the expected value, if the same is updated. This update is non-disruptive. * * @param obj object to update * @param offset of integer field in offset obj * @param expect you want the value present in field * @param update if the expected value expect is the same as field's current values, set the The value of the filed is this new value * @return If the value of field is changed returns True*/public native Boolean compareandswapint (Object obj, long offset, int expect, I NT update);
The CAS operation has 3 operands, the memory value m, the expected value E, the new value U, and if m==e, modifies the memory value to B, otherwise nothing is done.
"Java in-depth study" 8, Java in the description of unsafe class