获取Unsafe实例

/**
     * 通过反射获取Unsafe
     * @return
     * @throws Exception
     */
    public static Unsafe getUnsafe(){
        try {
          Field field = Unsafe.class.getDeclaredField("theUnsafe");
          //因为 Unsafe 的 theUnsafe 字段是private 的,所以这里需要设置成可访问的
          field.setAccessible(true);
          //Unsafe 的这个属性 theUnsafe 是静态的所以这里的get参数就是null
          Unsafe unsafe = (Unsafe)field.get(null);
          
          return unsafe;
        } catch (Exception e) {}
        return null;
    }

 

posted @ 2017-02-14 16:33  纵横四海1949  阅读(261)  评论(0编辑  收藏  举报