@@ -0,0 +1,2231 @@
+package org.banyan.concurrent.base;%0A%0Aimport sun.misc.Unsafe;%0A%0Aimport java.io.File;%0Aimport java.io.FileInputStream;%0Aimport java.io.IOException;%0Aimport java.lang.reflect.Field;%0A%0A/**%0A * @author krisjin%0A * @date 2020/11/20%0A */%0Apublic class UnsafeDemo %7B%0A%0A    public static void main(String%5B%5D args) throws Exception %7B%0A%0A        //new%E5%85%B3%E9%94%AE%E5%AD%97%E5%88%9B%E5%BB%BA%0A        Num num1 = new Num();%0A        System.out.println(%22new%E5%85%B3%E9%94%AE%E5%AD%97%E5%88%9B%E5%BB%BA: n=%22 + num1.getN());%0A%0A        //%E5%8F%8D%E5%B0%84%E5%88%9B%E5%BB%BA%0A        Num num2 = Num.class.newInstance();%0A        System.out.println(%22%E5%8F%8D%E5%B0%84%E5%88%9B%E5%BB%BA: n=%22 + num2.getN());%0A%0A%0A        //Unsafe%E5%88%9B%E5%BB%BA%0A        Unsafe unsafe = getUnsafe();%0A        Num num3 = (Num) unsafe.allocateInstance(Num.class);%0A        System.out.println(%22%E5%8F%8D%E5%B0%84%E5%88%9B%E5%BB%BA: n=%22 + num3.getN());%0A%0A        //%E4%BD%BF%E7%94%A8Unsafe%E4%BF%AE%E6%94%B9%E5%86%85%E5%AD%98%E6%95%B0%E6%8D%AE%0A        Field nField = num3.getClass().getDeclaredField(%22n%22);%0A        unsafe.putInt(num3, unsafe.objectFieldOffset(nField), 2);%0A%0A        System.out.println(%22Unsafe %E4%BF%AE%E6%94%B9%E5%90%8E%E7%9A%84%E5%80%BC: n=%22 + num3.getN());%0A%0A        //%E4%BD%BF%E7%94%A8Unsafe%E5%8A%A0%E8%BD%BD%E7%B1%BB%0A%0A        byte%5B%5D bytes = getClazzContent();%0A        Class numClazz = unsafe.defineClass(null, bytes, 0, bytes.length, null, null);%0A        Object result = numClazz.getMethod(%22getN%22).invoke(numClazz.newInstance());%0A%0A        System.out.println(%22Unsafe %E5%8A%A0%E8%BD%BDNum.class%E6%96%87%E4%BB%B6: n=%22 + result);%0A%0A    %7D%0A%0A%0A    public static Unsafe getUnsafe() %7B%0A        Field f;%0A        Unsafe unsafe;%0A        try %7B%0A            f = Unsafe.class.getDeclaredField(%22theUnsafe%22);%0A            f.setAccessible(true);%0A            unsafe = (Unsafe) f.get(null);%0A        %7D catch (Exception e) %7B%0A            throw new RuntimeException(%22initial the unsafe failure...%22);%0A        %7D%0A        return unsafe;%0A    %7D%0A%0A    public static byte%5B%5D getClazzContent() throws IOException %7B%0A        File f = new File(%22/usr/local/gitrep/banyan/target/classes/org/banyan/concurrent/base/UnsafeDemo$Num.class%22);%0A        try (FileInputStream fis = new FileInputStream(f)) %7B%0A            byte%5B%5D bytes = new byte%5Bfis.available()%5D;%0A            fis.read(bytes);%0A            return bytes;%0A        %7D%0A    %7D%0A%0A%0A    public static class Num %7B%0A        private int n;%0A%0A        public Num() %7B%0A            this.n = 1;%0A        %7D%0A%0A        public int getN() %7B%0A            return n;%0A        %7D%0A%0A        public void setN(int n) %7B%0A            this.n = n;%0A        %7D%0A    %7D%0A%0A%7D%0A

update UnsafeDemo