FileDocCategorySizeDatePackage
AtomicInteger.javaAPI DocJava SE 6 API6202Tue Jun 10 00:25:56 BST 2008java.util.concurrent.atomic

AtomicInteger

public class AtomicInteger extends Number implements Serializable
An {@code int} 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 AtomicInteger} is used in applications such as atomically incremented counters, and cannot be used as a replacement for an {@link java.lang.Integer}. 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
private volatile int
value
Constructors Summary
public AtomicInteger(int initialValue)
Creates a new AtomicInteger with the given initial value.

param
initialValue the initial value

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

    
Methods Summary
public final intaddAndGet(int delta)
Atomically adds the given value to the current value.

param
delta the value to add
return
the updated value

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

return
the updated value

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

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

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

return
the current value

        return value;
    
public final intgetAndAdd(int delta)
Atomically adds the given value to the current value.

param
delta the value to add
return
the previous value

        for (;;) {
            int current = get();
            int next = current + delta;
            if (compareAndSet(current, next))
                return current;
        }
    
public final intgetAndDecrement()
Atomically decrements by one the current value.

return
the previous value

        for (;;) {
            int current = get();
            int next = current - 1;
            if (compareAndSet(current, next))
                return current;
        }
    
public final intgetAndIncrement()
Atomically increments by one the current value.

return
the previous value

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

param
newValue the new value
return
the previous value

        for (;;) {
            int current = get();
            if (compareAndSet(current, newValue))
                return current;
        }
    
public final intincrementAndGet()
Atomically increments by one the current value.

return
the updated value

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

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

param
newValue the new value
since
1.6

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

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