Tutorial by Examples

public static Unsafe getUnsafe() { try { Field unsafe = Unsafe.class.getDeclaredField("theUnsafe"); unsafe.setAccessible(true); return (Unsafe) unsafe.get(null); } catch (IllegalAccessException e) { // Handle } catch (IllegalArgumentExcept...
public class UnsafeLoader { public static Unsafe loadUnsafe() { return Unsafe.getUnsafe(); } } While this example will compile, it is likely to fail at runtime unless the Unsafe class was loaded with the primary classloader. To ensure that happens the JVM should be loaded with...
Unsafe is stored as a private field that cannot be accessed directly. The constructor is private and the only method to access public static Unsafe getUnsafe() has privileged access. By use of reflection, there is a work-around to make private fields accessible: public static final Unsafe UNSAFE; ...
Some uses of unsafe is s follows: UseAPIOff heap / direct memory allocation, reallocation and deallocationallocateMemory(bytes), reallocateMemory(address, bytes) and freeMemory(address)Memory fencesloadFence(), storeFence(), fullFence()Parking current threadpark(isAbsolute, time), unpark(thread)Dir...

Page 1 of 1