Java Multithreading: "Juc Atomic class" 04 Atomicreference Atomic class

Source: Internet
Author: User
Tags final volatile


Atomicreference Introduction and Functions List



Atomicreference is a function of atomic manipulation of "objects".



atomicreference Function List

// Create a new AtomicReference with null initial value.
AtomicReference ()
// Create a new AtomicReference with the given initial value.
AtomicReference (V initialValue)
    
// If the current value == the expected value, then atomically set the value to the given update value.
boolean compareAndSet (V expect, V update)
// Get the current value.
V get ()
// Set to the given value atomically and return the old value.
V getAndSet (V newValue)
// Finally set to the given value.
void lazySet (V newValue)
// Set to the given value.
void set (V newValue)
// Return the string representation of the current value.
String toString ()
// If the current value == the expected value, then atomically set the value to the given update value.
boolean weakCompareAndSet (V expect, V update)
AtomicReference source code 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 role 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 ("value"));
      } 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, valueOffset, 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 ());
    }
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.