FileDocCategorySizeDatePackage
AtomicReference.javaAPI DocJava SE 6 API3530Tue Jun 10 00:25:58 BST 2008java.util.concurrent.atomic

AtomicReference

public class AtomicReference extends Object implements Serializable
An object reference that may be updated atomically. See the {@link java.util.concurrent.atomic} package specification for description of the properties of atomic variables.
since
1.5
author
Doug Lea
param
The type of object referred to by this reference

Fields Summary
private static final long
serialVersionUID
private static final Unsafe
unsafe
private static final long
valueOffset
private volatile V
value
Constructors Summary
public AtomicReference(V initialValue)
Creates a new AtomicReference with the given initial value.

param
initialValue the initial value

        value = initialValue;
    
public AtomicReference()
Creates a new AtomicReference with null initial value.

    
Methods Summary
public final booleancompareAndSet(V expect, V update)
Atomically sets the value to the given updated value if the current value {@code ==} the expected value.

param
expect the expected value
param
update the new value
return
true if successful. False return indicates that the actual value was not equal to the expected value.

        return unsafe.compareAndSwapObject(this, valueOffset, expect, update);
    
public final Vget()
Gets the current value.

return
the current value

        return value;
    
public final VgetAndSet(V newValue)
Atomically sets to the given value and returns the old value.

param
newValue the new value
return
the previous value

        while (true) {
            V x = get();
            if (compareAndSet(x, newValue))
                return x;
        }
    
public final voidlazySet(V newValue)
Eventually sets to the given value.

param
newValue the new value
since
1.6

        unsafe.putOrderedObject(this, valueOffset, newValue);
    
public final voidset(V newValue)
Sets to the given value.

param
newValue the new value

        value = newValue;
    
public java.lang.StringtoString()
Returns the String representation of the current value.

return
the String representation of the current value.

        return String.valueOf(get());
    
public final booleanweakCompareAndSet(V expect, V update)
Atomically sets the value to the given updated value if the current value {@code ==} the expected value.

May fail spuriously and does not provide ordering guarantees, so is only rarely an appropriate alternative to {@code compareAndSet}.

param
expect the expected value
param
update the new value
return
true if successful.

        return unsafe.compareAndSwapObject(this, valueOffset, expect, update);