Ensure visibility among multiple threads, it is declared as volatile

  Reference: https: //www.cnblogs.com/duanxz/p/6097779.html

 

Copy the code
com.test Package; 

Import sun.misc.Unsafe; 

Import as java.lang.reflect.Field; 

public class CASLearn { 
    Private static Final the Unsafe the unsafe; 
    Private Long Final static nameOffset; 
    Private Long Final static valueOffset; 
    Private Long Final static intffset; 

    / / in order to ensure visibility among multiple threads, it is declared as volatile but this example is a single-threaded 
    Private volatile String name; 
    Private volatile Long value; 
    Private volatile int Age; 

    static { 
        the try { 
            // unsafe Unsafe.getUnsafe can not be called directly ( ) to acquire an example, this method only to access to trusted classes. 
            /// Get unsafe by reflection 
            Field f = Unsafe.class.getDeclaredField ( "theUnsafe" ); // Internal reference 
            f.setAccessible (to true); 
            unsafe = (the Unsafe) f.get (null);

            // offset static block of code initializes each attribute 
            //CASLearn.class.getDeclaredField("value ") can be obtained Cause value attribute is the class loading mechanism java: 
            // Reference: https: //blog.csdn .NET / m0_38075425 / Article This article was / Details / 81,627,349 
            @ 1. load: reading binary stream class, the data structure stored value class method area, then a new class object, the data structure used to refer to the class 
            // 2. verification: Class byte stream to ensure that the file contains information in line with the current requirements of the virtual machine, the virtual machine will not jeopardize their own safety. Which includes four authentication, verification file format, metadata validation, bytecode verification, verification of symbolic references 
            @ 3 Preparation: allocate memory for the class's static variables, and set a default initial value. (Int initial value is 0) 
            // 4. Analysis: The binary data class replaced symbolic references cited directly 
            @ 5. Initialization: top to bottom, to the class assignment static variables, static code block comprises performing static  
            valueOffset = unsafe.objectFieldOffset (CASLearn.class.getDeclaredField ( "value "));
            = unsafe.objectFieldOffset nameOffset (CASLearn.class.getDeclaredField ( "name")); 
            intffset = unsafe.objectFieldOffset (CASLearn.class.getDeclaredField ( "Age"));
        } catch (NoSuchFieldException | IllegalAccessException e) {
            throw new Error(e);
        }
    }

    private CASLearn(String name, long value, int age) {
        this.name = name;
        this.value = value;
        this.age = age;
    }

    @Override
    public String toString() {
        return "name:" + name + "  age" + age + "   value" + value;
    }

    public static void main(String[] args) {
        CASLearn cas = new CASLearn("dh", 100L, 18);
        //原子操作:修改age 18->19
        unsafe.compareAndSwapInt(cas, intffset, 18, 19);
        //原子操作,value的值为100L,不是200L,所以不修改
        unsafe.compareAndSwapLong (CAS, valueOffset, 200L, 999L); 
        // atoms, modify "DH" -> "hiahiahia" 
        unsafe.compareAndSwapObject (CAS, nameOffset, "DH", "hiahiahia"); 
        // result is: name : hiahiahia age19 value100 
        System.out.println (CAS); 
    }

Guess you like

Origin www.cnblogs.com/struohnssebxsc/p/11243549.html