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

AtomicLong

public class AtomicLong extends Number implements Serializable
A {@code long} value that may be updated atomically. See the {@link java.util.concurrent.atomic} package specification for description of the properties of atomic variables. An {@code AtomicLong} is used in applications such as atomically incremented sequence numbers, and cannot be used as a replacement for a {@link java.lang.Long}. However, this class does extend {@code Number} to allow uniform access by tools and utilities that deal with numerically-based classes.
since
1.5
author
Doug Lea

Fields Summary
private static final long
serialVersionUID
private static final Unsafe
unsafe
private static final long
valueOffset
static final boolean
VM_SUPPORTS_LONG_CAS
Records whether the underlying JVM supports lockless CompareAndSet for longs. While the unsafe.CompareAndSetLong method works in either case, some constructions should be handled at Java level to avoid locking user-visible locks.
private volatile long
value
Constructors Summary
public AtomicLong(long initialValue)
Creates a new AtomicLong with the given initial value.

param
initialValue the initial value

        value = initialValue;
    
public AtomicLong()
Creates a new AtomicLong with initial value {@code 0}.

    
Methods Summary
private static native booleanVMSupportsCS8()
Returns whether underlying JVM supports lockless CompareAndSet for longs. Called only once and cached in VM_SUPPORTS_LONG_CAS.

public final longaddAndGet(long delta)
Atomically adds the given value to the current value.

param
delta the value to add
return
the updated value

        for (;;) {
            long current = get();
            long next = current + delta;
            if (compareAndSet(current, next))
                return next;
        }
    
public final booleancompareAndSet(long expect, long 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.compareAndSwapLong(this, valueOffset, expect, update);
    
public final longdecrementAndGet()
Atomically decrements by one the current value.

return
the updated value

        for (;;) {
            long current = get();
            long next = current - 1;
            if (compareAndSet(current, next))
                return next;
        }
    
public doubledoubleValue()

	return (double)get();
    
public floatfloatValue()

	return (float)get();
    
public final longget()
Gets the current value.

return
the current value

        return value;
    
public final longgetAndAdd(long delta)
Atomically adds the given value to the current value.

param
delta the value to add
return
the previous value

        while (true) {
            long current = get();
            long next = current + delta;
            if (compareAndSet(current, next))
                return current;
        }
    
public final longgetAndDecrement()
Atomically decrements by one the current value.

return
the previous value

        while (true) {
            long current = get();
            long next = current - 1;
            if (compareAndSet(current, next))
                return current;
        }
    
public final longgetAndIncrement()
Atomically increments by one the current value.

return
the previous value

        while (true) {
            long current = get();
            long next = current + 1;
            if (compareAndSet(current, next))
                return current;
        }
    
public final longgetAndSet(long newValue)
Atomically sets to the given value and returns the old value.

param
newValue the new value
return
the previous value

        while (true) {
            long current = get();
            if (compareAndSet(current, newValue))
                return current;
        }
    
public final longincrementAndGet()
Atomically increments by one the current value.

return
the updated value

        for (;;) {
            long current = get();
            long next = current + 1;
            if (compareAndSet(current, next))
                return next;
        }
    
public intintValue()

	return (int)get();
    
public final voidlazySet(long newValue)
Eventually sets to the given value.

param
newValue the new value
since
1.6

        unsafe.putOrderedLong(this, valueOffset, newValue);
    
public longlongValue()

	return (long)get();
    
public final voidset(long 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 Long.toString(get());
    
public final booleanweakCompareAndSet(long expect, long 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.compareAndSwapLong(this, valueOffset, expect, update);