Java Unsafe 常用API 例子。
阿新 • • 發佈:2018-02-04
常用 ref ssa string 獲取值 內存管理 [] cep all
1 線程掛起恢復
2 CAS操作
2 直接內存操作(非JVM內存)
3 實例化對象
4 直接操作對象屬性
5 直接操作數組
示例代碼如下
public class Player { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() {return age; } public void setAge(int age) { this.age = age; } }
import java.lang.reflect.Field; import com.alibaba.fastjson.JSON; import com.guo.util.Util; import sun.misc.Unsafe; /** * unsafe 操作數組 * @author GuoJunWei * */ public class Test1 { public staticUnsafe unsafe = Util.getUnsafe(); public static void main(String[] args) throws Exception { //function1(); //function2(); //function3(); //function4(); //function5(); function6(); } /** * park():方法返回條件 * 1 當前現在調用過 unpark 方法 (多次調用 按照一次計算) * 2 當前線程被中斷 * 3 當park 為 false:時間塊到了 單位納秒 * 4 當park 為 true:時間是絕對時間(1970)年 到期 單位毫秒*/ public static void function6() { System.out.println("Start"); long time = System.currentTimeMillis()+3000l; unsafe.park(true,time); System.out.println("end"); } /** * CAS操作 * @throws Exception */ public static void function5() throws Exception { Player player = (Player) unsafe.allocateInstance(Player.class); Field age = player.getClass().getDeclaredField("age"); long addressAge = unsafe.objectFieldOffset(age); unsafe.compareAndSwapInt(player, addressAge, 0, 100); System.out.println(player.getAge()); } /** * 直接分配內存地址:內存管理 */ public static void function4() { //分配100字節內存 返回初始地址 long address = unsafe.allocateMemory(100); //往分配的內存地址寫入值 unsafe.putInt(address, 55); //獲取值 System.out.println(unsafe.getInt(address)); //分配100字節內存 返回初始地址 long address1 = unsafe.allocateMemory(100); //copy 內存值 unsafe.copyMemory(address, address1, 4); System.out.println(unsafe.getInt(address1)); //釋放內存 unsafe.freeMemory(address); unsafe.freeMemory(address1); } /** * 操作對象屬性值 * @throws Exception */ public static void function3() throws Exception { Player player = (Player) unsafe.allocateInstance(Player.class); Field fieldName = player.getClass().getDeclaredField("name"); Field fieldAge = player.getClass().getDeclaredField("age"); long fileNameaddres = unsafe.objectFieldOffset(fieldName); long fileAgeaddres = unsafe.objectFieldOffset(fieldAge); unsafe.putObjectVolatile(player, fileNameaddres, "wangWu"); unsafe.putInt(player,fileAgeaddres, 100); System.out.println(player.getAge()+" "+player.getName()); } /** * 實例化對象 * @throws InstantiationException */ public static void function2() throws InstantiationException { Player player = (Player) unsafe.allocateInstance(Player.class); player.setAge(100); player.setName("zhangShan"); System.out.println(player.getAge()+" "+player.getName()); } /** * 對數組的操作 */ public static void function1() { int[] num = new int[7]; //數組的起始地址 long adress = unsafe.arrayBaseOffset(int[].class); //block 大小 long index = unsafe.arrayIndexScale(int[].class); unsafe.putInt(num, adress,1); unsafe.putInt(num, adress+index, 2); unsafe.putInt(num, adress+index+index, 3); unsafe.putInt(num, adress+index+index+index, 4); System.out.println(JSON.toJSONString(num)); } }
Java Unsafe 常用API 例子。