atomicreference Introduction
Atomicreference is a function of atomic manipulation of an "object".
atomicreference Source Analysis (based on jdk1.7.0_40)
The source code of Atomicreference.java in Jdk1.7.0_40 is as follows:
public class Atomicreference<v> implements java.io.Serializable {private static final long Serialversionuid =- 1848883965231344442L; Get unsafe object, the function of unsafe is to provide CAS operation private static final unsafe unsafe = unsafe.getunsafe (); Private static final long valueoffset; static {try {Valueoffset = Unsafe.objectfieldoffset (AtomicReference.class.getDeclaredField ("Valu E ")); } catch (Exception ex) {throw new Error (ex);} }//volatile type private volatile V value; Public atomicreference (V initialvalue) {value = InitialValue; Public atomicreference () {} Public final V get () {return value; Public final void Set (V newvalue) {value = newvalue; Public final void Lazyset (V newvalue) {Unsafe.putorderedobject (this, valueoffset, newvalue); Public Final Boolean Compareandset (v expect, v update) {return Unsafe.compareandswapobject (this, Valueoffset, expect, update);Public Final Boolean Weakcompareandset (v expect, v update) {return Unsafe.compareandswapobject (this, valueoff Set, expect, update); Public final V Getandset (v newvalue) {while (true) {v x = Get (); if (Compareandset (x, NewValue)) return x; }} public String toString () {return string.valueof (get ()); }}<span style= "font-family: ' Courier New '!important; FONT-SIZE:12PX!important; line-height:1.5!important; Color:rgb (0, 0, 0); " ></span>
Description :
Atomicreference source code is relatively simple. It is implemented through "volatile" and "unsafe CAS functions" atomic operations.
Value is a volatile type. This ensures that when a thread modifies the value of value, the value values that other threads see are the most recent value values, that is, the volatile value after the modification.
(02) Set value through CAs. This ensures that when a thread pool sets value through a CAS function (such as the Compareandset function), its operation is atomic, that is, the thread will not be interrupted when manipulating value.
Atomicreference Example
//Atomicreferencetest.java source code import java.util.concurrent.atomic.AtomicReference; public class Atomicreferencetest {public static void main (string[] args) {//creates two person objects whose IDs are 101 and 102, respectively. person P1 = new person (101); person P2 = new person (102); Creates a new Atomicreference object, initializes its value to P1 object atomicreference ar = new atomicreference (p1); Set up AR through CAs. If the value of AR is P1, it is set to P2. Ar.compareandset (P1, p2); Person P3 = (person) ar.get (); System.out.println ("P3 is" +p3); System.out.println ("p3.equals (p1) =" +p3.equals (p1)); }}class Person {volatile long id; Public person (long id) {this.id = ID; Public String toString () {return ' ID: ' +id; }}<span style= "font-family: ' Courier New '!important; FONT-SIZE:12PX!important; line-height:1.5!important; Color:rgb (0, 0, 0); " ></span>
Operation result :
P3 is id:102p3.equals (p1) =false<span style= "font-family: ' Courier New '!important; FONT-SIZE:12PX!important; line-height:1.5!important; Color:rgb (0, 0, 255); " ></span>
Result Description :
When you create a new Atomicreference object AR, it is initialized to P1.
This is followed by a CAS function to set it. If the value of AR is P1, it is set to P2.
Finally, get the AR corresponding object and print the result. The result of P3.equals (P1) is false, because the person does not overwrite the Equals () method, but instead takes the Equals () method that inherits from Object.java, whereas the Equals () in Object.java is actually called "= = "To compare two objects, that is, compare the addresses of two objects for equality.
Java multithreaded--atomicreference