This threadsafe class holds a mutable reference to an object of type V , provides volatile access semantics, and defines atomic operations for manipulating that value. get( ) and set( ) are ordinary accessor methods for the reference. compareAndSet( ) , weakCompareAndSet( ) , and getAndSet( ) perform the two named operations atomically. compareAndSet( ) is the canonical atomic operation: the reference is compared to an expected value, and, if it matches, is set to a new value. compareAndSet( ) returns true if it set the value or false otherwise . weakCompareAndSet( ) is similar but may fail to set the reference even if it does match the expected value (it is guaranteed to succeed eventually if the operation is repeatedly retried, however). Figure 16-102. java.util.concurrent.atomic.AtomicReference<V>public class AtomicReference<V> implements Serializable { // Public Constructors public AtomicReference ( ); public AtomicReference (V initialValue ); // Public Instance Methods public final boolean compareAndSet (V expect , V update ); public final V get ( ); public final V getAndSet (V newValue ); public final void set (V newValue ); public final boolean weakCompareAndSet (V expect , V update ); // Public Methods Overriding Object public String toString ( ); } |