• 12.Unsafe原子性操作


    import sun.misc.Unsafe;
    
    /**
     * JDK8
     * JDK 的此jar 包中的 Unsafe 类提供了硬件级别的原子性操作
     */
    public class UnsafeTest {
        //获得Unsafe的一个实例
        static final Unsafe unsafe1 = Unsafe.getUnsafe();
        static final long stateOffset;
        private volatile long state = 0;
    
        static {
            try {
                //获取 UnsafeTest 类里面的 state 变量, 在 UnsafeTest 对象里面的内存偏移量地址并将其保存到 stateOffset 变量中。
                //getDeclaredFields():获得某个类的所有声明的字段
                stateOffset = unsafe1.objectFieldOffset(UnsafeTest.class.getDeclaredField("state"));
            } catch (NoSuchFieldException e) {
                System.out.println(e.getLocalizedMessage());
                throw new Error(e);
            }
        }
        public static void main(String[] args){
            UnsafeTest unsafeTest = new UnsafeTest();
            //如果 test 对象中 内存偏移量为 stateOffset 的 state 变量的值为 0,则更新该值为 1
            boolean b = unsafe1.compareAndSwapInt(unsafeTest, stateOffset, 0, 1);
            System.out.println(b);
            //Exception in thread "main" java.lang.ExceptionInInitializerError
            //Caused by: java.lang.SecurityException: Unsafe
            //	at sun.misc.Unsafe.getUnsafe(Unsafe.java:90)
            //	at com.fly.two.UnsafeTest.<clinit>(UnsafeTest.java:10)
            //正常无法实例化 Unsafe 类
        }
    }
    
    
    import sun.misc.Unsafe;
    
    import java.lang.reflect.Field;
    
    import static com.fly.two.UnsafeTest.unsafe1;
    
    /**
     * JDK8
     * 通过反射来获取 Unsafe 实例方法。
     */
    public class UnsafeTest2 {
        //获得Unsafe的一个实例
        static final Unsafe unsafe;
        static final long stateOffset;
        private volatile long state = 0;
    
        static {
            try {
                //使用反射获取Unsafe的成员变量theUnsafe
                Field field = Unsafe.class.getDeclaredField("theUnsafe");
                //设置为可存取
                field.setAccessible(true);
                //获取该变量的值
                unsafe = (Unsafe) field.get(null);
                //获取 UnsafeTest 类里面的 state 变量, 在 UnsafeTest 对象里面的内存偏移量地址并将其保存到 stateOffset 变量中。
                //getDeclaredFields():获得某个类的所有声明的字段
                stateOffset = unsafe.objectFieldOffset(UnsafeTest2.class.getDeclaredField("state"));
            } catch (Exception e) {
                System.out.println(e.getLocalizedMessage());
                throw new Error(e);
            }
        }
        public static void main(String[] args){
            UnsafeTest2 unsafeTest = new UnsafeTest2();
            //如果 test 对象中 内存偏移量为 stateOffset 的 state 变量的值为 0,则更新该值为 1
            boolean b = unsafe.compareAndSwapInt(unsafeTest, stateOffset, 0, 1);
            System.out.println(b);//true
        }
    }
    
  • 相关阅读:
    20210304. 3. 通讯协议及事件处理机制
    20210304. 2. 数据类型与底层数据结构
    20210304. 1. 缓存原理 & 设计
    20210304. 0.3. Redis Cluster 搭建
    20210304. 0.2. Redis 哨兵模式搭建
    20210304. 0.1. Redis 安装
    20210208. Neo4j
    20210207. MongoDB
    20210203 8. 运维和第三方工具
    Global Brain Dynamics Embed the Motor Command Sequence of Caenorhabditis elegans
  • 原文地址:https://www.cnblogs.com/fly-book/p/11368562.html
Copyright © 2020-2023  润新知