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

AtomicInteger

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

param
initialValue the initial value

        value = initialValue;
    
public AtomicInteger()
Create a new AtomicInteger with initial value 0.

    
Methods Summary
public final intaddAndGet(int delta)
Atomically add the given value to 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 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.compareAndSwapInt(this, valueOffset, expect, update);
    
public final intdecrementAndGet()
Atomically decrement 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()
Get the current value.

return
the current value

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

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