Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import java.lang.reflect.Field;
import sun.misc.Unsafe;
public class Direct {
public static void main(String... args) {
Unsafe unsafe = null;
try {
Field field = sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
field.setAccessible(true);
unsafe = (sun.misc.Unsafe) field.get(null);
} catch (Exception e) {
throw new AssertionError(e);
}
long value = 12345;
byte size = 8; // a long is 64 bits (http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html)
long allocateMemory = unsafe.allocateMemory(size);
unsafe.putLong(allocateMemory, value);
long readValue = unsafe.getLong(allocateMemory);
System.out.println("read value : " + readValue);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment