Created
March 10, 2015 06:01
-
-
Save zxkletters/6c55a784198db9b63756 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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