preface

  • Just like any other Atomic, the method is the same.
  • An object reference that can be updated atomically.
  • On the properties of the atomic variable description, see {@ link Java. Util. Concurrent. Atomic} package specification.

The source code



package java.util.concurrent.atomic;
public class AtomicReference<V> implements java.io.Serializable {
    private static final long serialVersionUID = -1848883965231344442L;

    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); }
    }

    private volatile V value;

    /**
     * Creates a new AtomicReference with the given initial value.
     *
     * @param initialValue the initial value
     */
    public AtomicReference(V initialValue) {
        value = initialValue;
    }

    /**
     * Creates a new AtomicReference with null initial value.
     */
    public AtomicReference() {
    }

    /**
     * Gets the current value.
     *
     * @return the current value
     */
    public final V get() {
        return value;
    }

    /**
     * Sets to the given value.
     *
     * @param newValue the new value
     */
    public final void set(V newValue) { value = newValue; } /** * Eventually sets to the given value. ** @param newValue the new value * @since 1.6 */ 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);
    }

    @SuppressWarnings("unchecked")
    public final V getAndSet(V newValue) {
        return(V)unsafe.getAndSetObject(this, valueOffset, newValue); } /** * updates the current value atomically with the result of the given function and returns the previous value. * This function should have no side effects because it may be reapplied when an attempt to update fails due to contention between threads. */ public final V getAndUpdate(UnaryOperator<V> updateFunction) { V prev, next;do {
            prev = get();
            next = updateFunction.apply(prev);
        } while(! compareAndSet(prev, next));returnprev; } /** * updates the current value atomically with the result of the given function and returns the updated value. * This function should have no side effects because it may be reapplied when an attempt to update fails due to contention between threads. */ public final V updateAndGet(UnaryOperator<V> updateFunction) { V prev, next;do {
            prev = get();
            next = updateFunction.apply(prev);
        } while(! compareAndSet(prev, next));returnnext; } /** * applies the given function to the current value and the given value, atomically updating the current value and returning the previous value. * This function should have no side effects because it may be reapplied when an attempt to update fails due to contention between threads. * When applied, the function takes the current value as its first argument and the given UPDATE as its second argument. */ public final V getAndAccumulate(V x, BinaryOperator<V> accumulatorFunction) { V prev, next;do {
            prev = get();
            next = accumulatorFunction.apply(prev, x);
        } while(! compareAndSet(prev, next));returnprev; } /** * The result of applying the given function to the current value updates the current value atomically and returns the updated value. * This function should have no side effects because it may be reapplied when an attempt to update fails due to contention between threads. * When applied, the function takes the current value as its first argument and the given UPDATE as its second argument. */ public final V accumulateAndGet(V x, BinaryOperator<V> accumulatorFunction) { V prev, next;do {
            prev = get();
            next = accumulatorFunction.apply(prev, x);
        } while(! compareAndSet(prev, next));return next;
    }

    /**
     * Returns the String representation of the current value.
     * @return the String representation of the current value
     */
    public String toString() {
        returnString.valueOf(get()); }}Copy the code