admin 管理员组

文章数量: 887021


2023年12月23日发(作者:源码转换器在线转换)

readedBytes = teArray(address, ); n("content in reallocate memory: " + new String(readedBytes, "utf-8")); //释放内存 mory(address); }}UnsafeWrapper类源码如下:

import ;import tionTargetException;import ;public final class UnsafeWrapper { private static Class unsafeClass; private static Object unsafeInstance; private UnsafeWrapper() { //no instance } /** ==============================================*/ /** ====================内存块操作==================*/ /** ==============================================*/ /** * Allocates a new block of native memory, of the given size in bytes. The * contents of the memory are uninitialized; they will generally be * garbage. The resulting native pointer will never be zero, and will be * aligned for all value types. Dispose of this memory by calling {@link * #freeMemory}, or resize it with {@link #reallocateMemory}. * * Note: It is the resposibility of the caller to make * sure arguments are checked before the methods are called. While * some rudimentary checks are performed on the input, the checks * are best effort and when performance is an overriding priority, * as when methods of this class are optimized by the runtime * compiler, some or all checks (if any) may be elided. Hence, the * caller must not rely on the checks and corresponding * exceptions! * * @throws RuntimeException if the size is negative or too large * for the native size_t type * * @throws OutOfMemoryError if the allocation is refused by the system * * @see #getByte(long) * @see #putByte(long, byte) */ public static long allocateMemory(long bytes) { long address = 0; try { Method allocateMemoryMethod = unsafeClass().getDeclaredMethod("allocateMemory", ); address = (long) (unsafeInstance(), bytes); } catch (NoSuchMethodException e) { tackTrace(); } catch (IllegalAccessException e) { tackTrace(); } catch (InvocationTargetException e) { tackTrace(); }

return address; } /** * Resizes a new block of native memory, to the given size in bytes. The * contents of the new block past the size of the old block are * uninitialized; they will generally be garbage. The resulting native * pointer will be zero if and only if the requested size is zero. The * resulting native pointer will be aligned for all value types. Dispose * of this memory by calling {@link #freeMemory}, or resize it with {@link * #reallocateMemory}. The address passed to this method may be null, in * which case an allocation will be performed. * * Note: It is the resposibility of the caller to make * sure arguments are checked before the methods are called. While * some rudimentary checks are performed on the input, the checks * are best effort and when performance is an overriding priority, * as when methods of this class are optimized by the runtime * compiler, some or all checks (if any) may be elided. Hence, the * caller must not rely on the checks and corresponding * exceptions! * * @throws RuntimeException if the size is negative or too large * for the native size_t type * * @throws OutOfMemoryError if the allocation is refused by the system * * @see #allocateMemory */ public static long reallocateMemory(long address, long bytes) { long result = 0; try { Method allocateMemoryMethod = unsafeClass().getDeclaredMethod("reallocateMemory", , ); result = (long) (unsafeInstance(), address, bytes); } catch (NoSuchMethodException e) { tackTrace(); } catch (IllegalAccessException e) { tackTrace(); } catch (InvocationTargetException e) { tackTrace(); } return result; } /** * Disposes of a block of native memory, as obtained from {@link * #allocateMemory} or {@link #reallocateMemory}. The address passed to * this method may be null, in which case no action is taken. * * Note: It is the resposibility of the caller to make * sure arguments are checked before the methods are called. While * some rudimentary checks are performed on the input, the checks * are best effort and when performance is an overriding priority, * as when methods of this class are optimized by the runtime * compiler, some or all checks (if any) may be elided. Hence, the * caller must not rely on the checks and corresponding * exceptions! * * @throws RuntimeException if any of the arguments is invalid * * @see #allocateMemory */ public static void freeMemory(long address) { try { Method freeMemoryMethod = unsafeClass().getDeclaredMethod("freeMemory", ); (unsafeInstance(), address); } catch (NoSuchMethodException e) { tackTrace(); } catch (IllegalAccessException e) { tackTrace(); } catch (InvocationTargetException e) { tackTrace();

* @param startAddress 内存块的起始位置 * @param bytes 要被存放的内存 */ public static void putByteArray(long startAddress, byte[] bytes) { long currentAddress = startAddress; long lastAddress = startAddress + ; while(currentAddress < lastAddress) { int currentIndex = (int) (currentAddress - startAddress); putByte(currentAddress, bytes[currentIndex]); currentAddress++; } } /** @see #getByte(long) */ public static short getShort(long address) { short result = 0; try { Method method = unsafeClass().getDeclaredMethod("getShort", ); result = (short) (unsafeInstance(), address); } catch (NoSuchMethodException e) { tackTrace(); } catch (IllegalAccessException e) { tackTrace(); } catch (InvocationTargetException e) { tackTrace(); } return result; } /** @see #putByte(long, byte) */ public static void putShort(long address, short x) { try { Method method = unsafeClass().getDeclaredMethod("putShort", , ); (unsafeInstance(), address, x); } catch (NoSuchMethodException e) { tackTrace(); } catch (IllegalAccessException e) { tackTrace(); } catch (InvocationTargetException e) { tackTrace(); } } /** @see #getByte(long) */ public static char getChar(long address) { char result = 0; try { Method method = unsafeClass().getDeclaredMethod("getChar", ); result = (char) (unsafeInstance(), address); } catch (NoSuchMethodException e) { tackTrace(); } catch (IllegalAccessException e) { tackTrace(); } catch (InvocationTargetException e) { tackTrace(); } return result; } /** @see #putByte(long, byte) */ public static void putChar(long address, char x) { try { Method method = unsafeClass().getDeclaredMethod("putChar", , ); (unsafeInstance(), address, x); } catch (NoSuchMethodException e) { tackTrace(); } catch (IllegalAccessException e) { tackTrace(); } catch (InvocationTargetException e) { tackTrace(); } } /** @see #getByte(long) */

/** @see #getByte(long) */ public static int getInt(long address) { int result = 0; try { Method method = unsafeClass().getDeclaredMethod("getInt", ); result = (int) (unsafeInstance(), address); } catch (NoSuchMethodException e) { tackTrace(); } catch (IllegalAccessException e) { tackTrace(); } catch (InvocationTargetException e) { tackTrace(); } return result; } /** @see #putByte(long, byte) */ public static void putInt(long address, int x) { try { Method method = unsafeClass().getDeclaredMethod("putInt", , ); (unsafeInstance(), address, x); } catch (NoSuchMethodException e) { tackTrace(); } catch (IllegalAccessException e) { tackTrace(); } catch (InvocationTargetException e) { tackTrace(); } } /** @see #getByte(long) */ public static long getLong(long address) { long result = 0; try { Method method = unsafeClass().getDeclaredMethod("getLong", ); result = (long) (unsafeInstance(), address); } catch (NoSuchMethodException e) { tackTrace(); } catch (IllegalAccessException e) { tackTrace(); } catch (InvocationTargetException e) { tackTrace(); } return result; } /** @see #putByte(long, byte) */ public static void putLong(long address, long x) { try { Method method = unsafeClass().getDeclaredMethod("putLong", , ); (unsafeInstance(), address, x); } catch (NoSuchMethodException e) { tackTrace(); } catch (IllegalAccessException e) { tackTrace(); } catch (InvocationTargetException e) { tackTrace(); } } /** @see #getByte(long) */ public static float getFloat(long address) { float result = 0; try { Method method = unsafeClass().getDeclaredMethod("getFloat", ); result = (float) (unsafeInstance(), address); } catch (NoSuchMethodException e) { tackTrace(); } catch (IllegalAccessException e) { tackTrace(); } catch (InvocationTargetException e) { tackTrace(); } return result;

return result; } /** @see #putByte(long, byte) */ public static void putFloat(long address, float x) { try { Method method = unsafeClass().getDeclaredMethod("putFloat", , ); (unsafeInstance(), address, x); } catch (NoSuchMethodException e) { tackTrace(); } catch (IllegalAccessException e) { tackTrace(); } catch (InvocationTargetException e) { tackTrace(); } } /** @see #getByte(long) */ public static double getDouble(long address) { double result = 0; try { Method method = unsafeClass().getDeclaredMethod("getDouble", ); result = (double) (unsafeInstance(), address); } catch (NoSuchMethodException e) { tackTrace(); } catch (IllegalAccessException e) { tackTrace(); } catch (InvocationTargetException e) { tackTrace(); } return result; } /** @see #putByte(long, byte) */ public static void putDouble(long address, double x) { try { Method method = unsafeClass().getDeclaredMethod("putDouble", , ); (unsafeInstance(), address, x); } catch (NoSuchMethodException e) { tackTrace(); } catch (IllegalAccessException e) { tackTrace(); } catch (InvocationTargetException e) { tackTrace(); } } /** ==============================================*/ /** ====================对象操作====================*/ /** ==============================================*/ /** * Allocates an instance but does not run any constructor. * Initializes the class if it has not yet been. */ public static T allocateInstance(Class klass) { T result = null; try { Method allocateInstanceMethod = unsafeClass().getDeclaredMethod("allocateInstance", ); result = (T) (unsafeInstance(), klass); } catch (NoSuchMethodException e) { tackTrace(); } catch (InvocationTargetException e) { tackTrace(); } catch (IllegalAccessException e) { tackTrace();

tackTrace(); } return result; } /** @see #getInt(Object, long) */ public static byte getByte(Object o, long offset) { byte result = 0; try { Method method = unsafeClass().getDeclaredMethod("getByte", , ); result = (byte) (unsafeInstance(), o, offset); } catch (NoSuchMethodException e) { tackTrace(); } catch (IllegalAccessException e) { tackTrace(); } catch (InvocationTargetException e) { tackTrace(); } return result; } /** @see #putInt(Object, long, int) */ public static void putByte(Object o, long offset, byte x) { try { Method method = unsafeClass().getDeclaredMethod("putByte", , , ); (unsafeInstance(), o, offset, x); } catch (NoSuchMethodException e) { tackTrace(); } catch (IllegalAccessException e) { tackTrace(); } catch (InvocationTargetException e) { tackTrace(); } } /** * Fetches a value from a given Java variable. * More specifically, fetches a field or array element within the given * object {@code o} at the given offset, or (if {@code o} is null) * from the memory address whose numerical value is the given offset. *

* The results are undefined unless one of the following cases is true: *

    *
  • The offset was obtained from {@link #objectFieldOffset} on * the {@link } of some Java field and the object * referred to by {@code o} is of a class compatible with that * field's class. * *
  • The offset and object reference {@code o} (either null or * non-null) were both obtained via {@link #staticFieldOffset} * and {@link #staticFieldBase} (respectively) from the * reflective {@link Field} representation of some Java field. * *
  • The object referred to by {@code o} is an array, and the offset * is an integer of the form {@code B+N*S}, where {@code N} is * a valid index into the array, and {@code B} and {@code S} are * the values obtained by {@link #arrayBaseOffset} and {@link * #arrayIndexScale} (respectively) from the array's class. The value * referred to is the {@code N}th element of the array. * *
*

* If one of the above cases is true, the call references a specific Java * variable (field or array element). However, the results are undefined * if that variable is not in fact of the type returned by this method. *

* This method refers to a variable by means of two parameters, and so * it provides (in effect) a double-register addressing mode * for Java variables. When the object reference is null, this method * uses its offset as an absolute address. This is similar in operation * to methods such as {@link #getInt(long)}, which provide (in effect) a * single-register addressing mode for non-Java variables. * However, because Java variables may have a different layout in memory

* However, because Java variables may have a different layout in memory * from non-Java variables, programmers should not assume that these * two addressing modes are ever equivalent. Also, programmers should * remember that offsets from the double-register addressing mode cannot * be portably confused with longs used in the single-register addressing * mode. * * @param o Java heap object in which the variable resides, if any, else * null * @param offset indication of where the variable resides in a Java heap * object, if any, else a memory address locating the variable * statically * @return the value fetched from the indicated Java variable * @throws RuntimeException No defined exceptions are thrown, not even * {@link NullPointerException} */ public static int getInt(Object o, long offset) { int result = 0; try { Method method = unsafeClass().getDeclaredMethod("getInt", , ); result = (int) (unsafeInstance(), o, offset); } catch (NoSuchMethodException e) { tackTrace(); } catch (IllegalAccessException e) { tackTrace(); } catch (InvocationTargetException e) { tackTrace(); } return result; } /** * Stores a value into a given Java variable. *

* The first two parameters are interpreted exactly as with * {@link #getInt(Object, long)} to refer to a specific * Java variable (field or array element). The given value * is stored into that variable. *

* The variable must be of the same type as the method * parameter {@code x}. * * @param o Java heap object in which the variable resides, if any, else * null * @param offset indication of where the variable resides in a Java heap * object, if any, else a memory address locating the variable * statically * @param x the value to store into the indicated Java variable * @throws RuntimeException No defined exceptions are thrown, not even * {@link NullPointerException} */ public static void putInt(Object o, long offset, int x) { try { Method method = unsafeClass().getDeclaredMethod("putInt", , , ); (unsafeInstance(), o, offset, x); } catch (NoSuchMethodException e) { tackTrace(); } catch (IllegalAccessException e) { tackTrace(); } catch (InvocationTargetException e) { tackTrace(); } } /** * Fetches a reference value from a given Java variable. * @see #getInt(Object, long) */ public static Object getObject(Object o, long offset) { Object result = null; try { Method method = unsafeClass().getDeclaredMethod("getObject", , ); result = (unsafeInstance(), o, offset); } catch (NoSuchMethodException e) {

} catch (NoSuchMethodException e) { tackTrace(); } catch (IllegalAccessException e) { tackTrace(); } catch (InvocationTargetException e) { tackTrace(); } return result; } /** * Stores a reference value into a given Java variable. *

* Unless the reference {@code x} being stored is either null * or matches the field type, the results are undefined. * If the reference {@code o} is non-null, card marks or * other store barriers for that object (if the VM requires them) * are updated. * @see #putInt(Object, long, int) */ public static void putObject(Object o, long offset, Object x) { try { Method method = unsafeClass().getDeclaredMethod("putObject", , , ); (unsafeInstance(), o, offset, x); } catch (NoSuchMethodException e) { tackTrace(); } catch (IllegalAccessException e) { tackTrace(); } catch (InvocationTargetException e) { tackTrace(); } } /** @see #getInt(Object, long) */ public static boolean getBoolean(Object o, long offset) { boolean result = false; try { Method method = unsafeClass().getDeclaredMethod("getBoolean", , ); result = (boolean) (unsafeInstance(), o, offset); } catch (NoSuchMethodException e) { tackTrace(); } catch (IllegalAccessException e) { tackTrace(); } catch (InvocationTargetException e) { tackTrace(); } return result; } /** @see #putInt(Object, long, int) */ public static void putBoolean(Object o, long offset, boolean x) { try { Method method = unsafeClass().getDeclaredMethod("putBoolean", , , ); (unsafeInstance(), o, offset, x); } catch (NoSuchMethodException e) { tackTrace(); } catch (IllegalAccessException e) { tackTrace(); } catch (InvocationTargetException e) { tackTrace(); } } /** @see #getInt(Object, long) */ public static short getShort(Object o, long offset) { short result = 0; try { Method method = unsafeClass().getDeclaredMethod("getShort", , ); result = (short) (unsafeInstance(), o, offset); } catch (NoSuchMethodException e) { tackTrace();

tackTrace(); } catch (IllegalAccessException e) { tackTrace(); } catch (InvocationTargetException e) { tackTrace(); } return result; } /** @see #putInt(Object, long, int) */ public static void putShort(Object o, long offset, short x) { try { Method method = unsafeClass().getDeclaredMethod("putShort", , , ); (unsafeInstance(), o, offset, x); } catch (NoSuchMethodException e) { tackTrace(); } catch (IllegalAccessException e) { tackTrace(); } catch (InvocationTargetException e) { tackTrace(); } } /** @see #getInt(Object, long) */ public static char getChar(Object o, long offset) { char result = 0; try { Method method = unsafeClass().getDeclaredMethod("getChar", , ); result = (char) (unsafeInstance(), o, offset); } catch (NoSuchMethodException e) { tackTrace(); } catch (IllegalAccessException e) { tackTrace(); } catch (InvocationTargetException e) { tackTrace(); } return result; } /** @see #putInt(Object, long, int) */ public static void putChar(Object o, long offset, char x) { try { Method method = unsafeClass().getDeclaredMethod("putChar", , , ); (unsafeInstance(), o, offset, x); } catch (NoSuchMethodException e) { tackTrace(); } catch (IllegalAccessException e) { tackTrace(); } catch (InvocationTargetException e) { tackTrace(); } } /** @see #getInt(Object, long) */ public static long getLong(Object o, long offset) { long result = 0; try { Method method = unsafeClass().getDeclaredMethod("getLong", , ); result = (long) (unsafeInstance(), o, offset); } catch (NoSuchMethodException e) { tackTrace(); } catch (IllegalAccessException e) { tackTrace(); } catch (InvocationTargetException e) { tackTrace(); } return result; } /** @see #putInt(Object, long, int) */ public static void putLong(Object o, long offset, long x) { try { Method method = unsafeClass().getDeclaredMethod("putLong", , , ); (unsafeInstance(), o, offset, x);

(unsafeInstance(), o, offset, x); } catch (NoSuchMethodException e) { tackTrace(); } catch (IllegalAccessException e) { tackTrace(); } catch (InvocationTargetException e) { tackTrace(); } } /** @see #getInt(Object, long) */ public static float getFloat(Object o, long offset) { float result = 0f; try { Method method = unsafeClass().getDeclaredMethod("getFloat", , ); result = (float) (unsafeInstance(), o, offset); } catch (NoSuchMethodException e) { tackTrace(); } catch (IllegalAccessException e) { tackTrace(); } catch (InvocationTargetException e) { tackTrace(); } return result; } /** @see #putInt(Object, long, int) */ public static void putFloat(Object o, long offset, float x) { try { Method method = unsafeClass().getDeclaredMethod("putFloat", , , ); (unsafeInstance(), o, offset, x); } catch (NoSuchMethodException e) { tackTrace(); } catch (IllegalAccessException e) { tackTrace(); } catch (InvocationTargetException e) { tackTrace(); } } /** @see #getInt(Object, long) */ public static double getDouble(Object o, long offset) { double result = 0; try { Method method = unsafeClass().getDeclaredMethod("getDouble", , ); result = (double) (unsafeInstance(), o, offset); } catch (NoSuchMethodException e) { tackTrace(); } catch (IllegalAccessException e) { tackTrace(); } catch (InvocationTargetException e) { tackTrace(); } return result; } /** @see #putInt(Object, long, int) */ public static void putDouble(Object o, long offset, double x) { try { Method method = unsafeClass().getDeclaredMethod("putDouble", , , ); (unsafeInstance(), o, offset, x); } catch (NoSuchMethodException e) { tackTrace(); } catch (IllegalAccessException e) { tackTrace(); } catch (InvocationTargetException e) { tackTrace(); } } /** ==============================================*/

/** ==============================================*/ /** ================偏移量操作======================*/ /** ==============================================*/ /** * Reports the location of a given field in the storage allocation of its * class. Do not expect to perform any sort of arithmetic on this offset; * it is just a cookie which is passed to the unsafe heap memory accessors. * *

Any given field will always have the same offset and base, and no * two distinct fields of the same class will ever have the same offset * and base. * *

As of 1.4.1, offsets for fields are represented as long values, * although the Sun JVM does not use the most significant 32 bits. * However, JVM implementations which store static fields at absolute * addresses can use long offsets and null base pointers to express * the field locations in a form usable by {@link #getInt(Object,long)}. * Therefore, code which will be ported to such JVMs on 64-bit platforms * must preserve all bits of static field offsets. * @see #getInt(Object, long) */ public static long objectFieldOffset(Field field) { long result = 0; try { Method method = unsafeClass().getDeclaredMethod("objectFieldOffset", ); result = (long) (unsafeInstance(), field); } catch (NoSuchMethodException e) { tackTrace(); } catch (IllegalAccessException e) { tackTrace(); } catch (InvocationTargetException e) { tackTrace(); } return result; } /** * Reports the location of a given static field, in conjunction with {@link * #staticFieldBase}. *

Do not expect to perform any sort of arithmetic on this offset; * it is just a cookie which is passed to the unsafe heap memory accessors. * *

Any given field will always have the same offset, and no two distinct * fields of the same class will ever have the same offset. * *

As of 1.4.1, offsets for fields are represented as long values, * although the Sun JVM does not use the most significant 32 bits. * It is hard to imagine a JVM technology which needs more than * a few bits to encode an offset within a non-array object, * However, for consistency with other methods in this class, * this method reports its result as a long value. * @see #getInt(Object, long) */ public static long staticFieldOffset(Field field) { long result = 0; try { Method method = unsafeClass().getDeclaredMethod("staticFieldOffset", ); result = (long) (unsafeInstance(), field); } catch (NoSuchMethodException e) { tackTrace(); } catch (IllegalAccessException e) { tackTrace(); } catch (InvocationTargetException e) { tackTrace(); } return result; } /** * Reports the location of a given static field, in conjunction with {@link * #staticFieldOffset}. *

Fetch the base "Object", if any, with which static fields of the * given class can be accessed via methods like {@link #getInt(Object,

* given class can be accessed via methods like {@link #getInt(Object, * long)}. This value may be null. This value may refer to an object * which is a "cookie", not guaranteed to be a real Object, and it should * not be used in any way except as argument to the get and put routines in * this class. */ public static Object staticFieldBase(Field field) { Object result = false; try { Method method = unsafeClass().getDeclaredMethod("staticFieldBase", ); result = (unsafeInstance(), field); } catch (NoSuchMethodException e) { tackTrace(); } catch (IllegalAccessException e) { tackTrace(); } catch (InvocationTargetException e) { tackTrace(); } return result; } /** * Reports the offset of the first element in the storage allocation of a * given array class. If {@link #arrayIndexScale} returns a non-zero value * for the same class, you may use that scale factor, together with this * base offset, to form new offsets to access elements of arrays of the * given class. * * @see #getInt(Object, long) * @see #putInt(Object, long, int) */ public static int arrayBaseOffset(Class arrayClass) { int result = 0; try { Method method = unsafeClass().getDeclaredMethod("arrayBaseOffset", ); result = (int) (unsafeInstance(), arrayClass); } catch (NoSuchMethodException e) { tackTrace(); } catch (IllegalAccessException e) { tackTrace(); } catch (InvocationTargetException e) { tackTrace(); } return result; } /** * Reports the scale factor for addressing elements in the storage * allocation of a given array class. However, arrays of "narrow" types * will generally not work properly with accessors like {@link * #getByte(Object, long)}, so the scale factor for such classes is reported * as zero. * * @see #arrayBaseOffset * @see #getInt(Object, long) * @see #putInt(Object, long, int) */ public static int arrayIndexScale(Class arrayClass) { int result = 0; try { Method method = unsafeClass().getDeclaredMethod("arrayIndexScale", ); result = (int) (unsafeInstance(), arrayClass); } catch (NoSuchMethodException e) { tackTrace(); } catch (IllegalAccessException e) { tackTrace(); } catch (InvocationTargetException e) { tackTrace(); } return result; }

/** ==============================================*/ /** ==================CAS操作======================*/ /** ==============================================*/ /** * Atomically updates Java variable to {@code x} if it is currently * holding {@code expected}. * *

This operation has memory semantics of a {@code volatile} read * and write. Corresponds to C11 atomic_compare_exchange_strong. * * @return {@code true} if successful */ public static boolean compareAndSwapObject(Object o, long offset, Object expected, Object x) { boolean result = false; try { Method method = unsafeClass().getDeclaredMethod("compareAndSwapObject", , , , ); result = (boolean) (unsafeInstance(), o, offset, expected, x); } catch (NoSuchMethodException e) { tackTrace(); } catch (IllegalAccessException e) { tackTrace(); } catch (InvocationTargetException e) { tackTrace(); } return result; } /** * Atomically updates Java variable to {@code x} if it is currently * holding {@code expected}. * *

This operation has memory semantics of a {@code volatile} read * and write. Corresponds to C11 atomic_compare_exchange_strong. * * @return {@code true} if successful */ public static boolean compareAndSwapInt(Object o, long offset, int expected, int x) { boolean result = false; try { Method method = unsafeClass().getDeclaredMethod("compareAndSwapInt", , , , ); result = (boolean) (unsafeInstance(), o, offset, expected, x); } catch (NoSuchMethodException e) { tackTrace(); } catch (IllegalAccessException e) { tackTrace(); } catch (InvocationTargetException e) { tackTrace(); } return result; } /** * Atomically updates Java variable to {@code x} if it is currently * holding {@code expected}. * *

This operation has memory semantics of a {@code volatile} read * and write. Corresponds to C11 atomic_compare_exchange_strong. * * @return {@code true} if successful */ public static boolean compareAndSwapLong(Object o, long offset, long expected, long x) { boolean result = false; try { Method method = unsafeClass().getDeclaredMethod("compareAndSwapLong", , , , ); result = (boolean) (unsafeInstance(), o, offset, expected, x); } catch (NoSuchMethodException e) { tackTrace(); } catch (IllegalAccessException e) { tackTrace(); } catch (InvocationTargetException e) { tackTrace(); } return result;

return result; } /** * Unblocks the given thread blocked on {@code park}, or, if it is * not blocked, causes the subsequent call to {@code park} not to * block. Note: this operation is "unsafe" solely because the * caller must somehow ensure that the thread has not been * destroyed. Nothing special is usually required to ensure this * when called from Java (in which there will ordinarily be a live * reference to the thread) but this is not nearly-automatically * so when calling from native code. * * @param thread the thread to unpark. */ public void unpark(Object thread) { try { Method method = unsafeClass().getDeclaredMethod("park", ); (unsafeInstance(), thread); } catch (NoSuchMethodException e) { tackTrace(); } catch (IllegalAccessException e) { tackTrace(); } catch (InvocationTargetException e) { tackTrace(); } } /** * Blocks current thread, returning when a balancing * {@code unpark} occurs, or a balancing {@code unpark} has * already occurred, or the thread is interrupted, or, if not * absolute and time is not zero, the given time nanoseconds have * elapsed, or if absolute, the given deadline in milliseconds * since Epoch has passed, or spuriously (i.e., returning for no * "reason"). Note: This operation is in the Unsafe class only * because {@code unpark} is, so it would be strange to place it * elsewhere. */ public void park(boolean isAbsolute, long time) { try { Method method = unsafeClass().getDeclaredMethod("park", , ); (unsafeInstance(), isAbsolute, time); } catch (NoSuchMethodException e) { tackTrace(); } catch (IllegalAccessException e) { tackTrace(); } catch (InvocationTargetException e) { tackTrace(); } } private static void init() { try { unsafeClass = e(""); Field theUnsafeField = laredField("theUnsafe"); boolean orignialAccessible = ssible(); essible(true); unsafeInstance = (null); essible(orignialAccessible); } catch (IllegalAccessException e) { tackTrace(); } catch (NoSuchFieldException e) { tackTrace(); } catch (ClassNotFoundException e) { tackTrace(); } } private static Object unsafeInstance() { if(unsafeInstance == null) init(); return unsafeInstance;

return unsafeInstance; } private static Class unsafeClass() { if(unsafeClass == null) init(); return unsafeClass; }}References:


本文标签: 内存 操作 源码