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 static Unsafe 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)); } }