FileDocCategorySizeDatePackage
AtomicLong.javaAPI DocJava SE 5 API6330Fri Aug 26 14:57:26 BST 2005java.util.concurrent.atomic

AtomicLong

public class AtomicLong extends Number implements Serializable
A 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 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 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
Record 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)
Create a new AtomicLong with the given initial value.

param
initialValue the initial value

        value = initialValue;
    
public AtomicLong()
Create a new AtomicLong with initial value 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 add the given value to 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 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.compareAndSwapLong(this, valueOffset, expect, update);
    
public final longdecrementAndGet()
Atomically decrement 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()
Get the current value.

return
the current value

        return value;
    
public final longgetAndAdd(long delta)
Atomically add the given value to 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 decrement 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 increment 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)
Set to the give value and return 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 increment 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 longlongValue()

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