Java获取UnSafe类

UnSafe类功能强大,可直接操作内存等,具体功能见下图

由于JDK并没有开放对UnSafe的获取端口,所以可通过如下方式进行获取

    public static Unsafe getUnSafe() throws NoSuchFieldException, IllegalAccessException {
        Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
        theUnsafe.setAccessible(true);
        return (Unsafe) theUnsafe.get(null);
    }

 

posted @ 2021-05-12 23:45  手握钢叉的猹  阅读(253)  评论(0编辑  收藏  举报