FileDocCategorySizeDatePackage
AtomicReference.javaAPI DocAndroid 1.5 API3165Wed May 06 22:41:02 BST 2009java.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)
Create a new AtomicReference with the given initial value.

param
initialValue the initial value

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

    
Methods Summary
public final booleancompareAndSet(V expect, V update)
Atomically set the value to the given updated value if the current value == 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()
Get the current value.

return
the current value

        return value;
    
public final VgetAndSet(V newValue)
Set to the given value and return 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 voidset(V newValue)
Set 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 set the value to the given updated value if the current value == the expected value. May fail spuriously.

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

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