python操作底层

package com.example.core.cas;

import sun.misc.Unsafe;

import java.lang.reflect.Field;
import java.util.Arrays;

public class UseUnsafe {
    private static int byteArrayBaseOffset;

    public static void main(String[] args) throws NoSuchFieldException,SecurityException,IllegalArgumentException,IllegalAccessException{
        Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");//映射
        theUnsafe.setAccessible(true);
        Unsafe UNSAFE = (Unsafe) theUnsafe.get(null);
        System.out.println(UNSAFE);//实际存储的物理位置 sun.misc.Unsafe@edf4efb
        byte[] data = new byte[10];
        System.out.println(Arrays.toString(data));//[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
        byteArrayBaseOffset = UNSAFE.arrayBaseOffset(byte[].class);
        System.out.println("byte[]字节数组的第一个元素的便宜地址:" + byteArrayBaseOffset);
        //设置指定的字节数组,修改其内存字段的位置
        //1,在data这个数组对象设置 修改其内存字段的位置为1
        UNSAFE.putByte(data,byteArrayBaseOffset,(byte)1);
        //2,在data这个数组对象设置 修改其内存字段的位置为8
        UNSAFE.putByte(data,byteArrayBaseOffset+6,(byte)8);
        System.out.println(Arrays.toString(data));//[1, 0, 0, 0, 0, 0, 8, 0, 0, 0]
    }
}
发布了53 篇原创文章 · 获赞 1 · 访问量 1125

猜你喜欢

转载自blog.csdn.net/CHYabc123456hh/article/details/104720662