Skip to content

Instantly share code, notes, and snippets.

@zxkletters
Created March 10, 2015 06:01
Show Gist options
  • Save zxkletters/6c55a784198db9b63756 to your computer and use it in GitHub Desktop.
Save zxkletters/6c55a784198db9b63756 to your computer and use it in GitHub Desktop.
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