Added in API level 1

AtomicInteger

public class AtomicInteger
extends Number implements Serializable

java.lang.Object
   ↳ java.lang.Number
     ↳ java.util.concurrent.atomic.AtomicInteger


An int value that may be updated atomically. See the VarHandle specification for descriptions of the properties of atomic accesses. An AtomicInteger is used in applications such as atomically incremented counters, and cannot be used as a replacement for an Integer. However, this class does extend Number to allow uniform access by tools and utilities that deal with numerically-based classes.

Summary

Public constructors

AtomicInteger(int initialValue)

Creates a new AtomicInteger with the given initial value.

AtomicInteger()

Creates a new AtomicInteger with initial value 0.

Public methods

final int accumulateAndGet(int x, IntBinaryOperator accumulatorFunction)

Atomically updates (with memory effects as specified by VarHandle.compareAndSet(Object)) the current value with the results of applying the given function to the current and given values, returning the updated value.

final int addAndGet(int delta)

Atomically adds the given value to the current value, with memory effects as specified by VarHandle#getAndAdd.

final int compareAndExchange(int expectedValue, int newValue)

Atomically sets the value to newValue if the current value, referred to as the witness value, == expectedValue, with memory effects as specified by VarHandle#compareAndExchange.

final int compareAndExchangeAcquire(int expectedValue, int newValue)

Atomically sets the value to newValue if the current value, referred to as the witness value, == expectedValue, with memory effects as specified by VarHandle#compareAndExchangeAcquire.

final int compareAndExchangeRelease(int expectedValue, int newValue)

Atomically sets the value to newValue if the current value, referred to as the witness value, == expectedValue, with memory effects as specified by VarHandle#compareAndExchangeRelease.

final boolean compareAndSet(int expectedValue, int newValue)

Atomically sets the value to newValue if the current value == expectedValue, with memory effects as specified by VarHandle#compareAndSet.

final int decrementAndGet()

Atomically decrements the current value, with memory effects as specified by VarHandle#getAndAdd.

double doubleValue()

Returns the current value of this AtomicInteger as a double after a widening primitive conversion, with memory effects as specified by VarHandle#getVolatile.

float floatValue()

Returns the current value of this AtomicInteger as a float after a widening primitive conversion, with memory effects as specified by VarHandle#getVolatile.

final int get()

Returns the current value, with memory effects as specified by VarHandle#getVolatile.

final int getAcquire()

Returns the current value, with memory effects as specified by VarHandle#getAcquire.

final int getAndAccumulate(int x, IntBinaryOperator accumulatorFunction)

Atomically updates (with memory effects as specified by VarHandle.compareAndSet(Object)) the current value with the results of applying the given function to the current and given values, returning the previous value.

final int getAndAdd(int delta)

Atomically adds the given value to the current value, with memory effects as specified by VarHandle#getAndAdd.

final int getAndDecrement()

Atomically decrements the current value, with memory effects as specified by VarHandle#getAndAdd.

final int getAndIncrement()

Atomically increments the current value, with memory effects as specified by VarHandle#getAndAdd.

final int getAndSet(int newValue)

Atomically sets the value to newValue and returns the old value, with memory effects as specified by VarHandle#getAndSet.

final int getAndUpdate(IntUnaryOperator updateFunction)

Atomically updates (with memory effects as specified by VarHandle.compareAndSet(Object)) the current value with the results of applying the given function, returning the previous value.

final int getOpaque()

Returns the current value, with memory effects as specified by VarHandle#getOpaque.

final int getPlain()

Returns the current value, with memory semantics of reading as if the variable was declared non-volatile.

final int incrementAndGet()

Atomically increments the current value, with memory effects as specified by VarHandle#getAndAdd.

int intValue()

Returns the current value of this AtomicInteger as an int, with memory effects as specified by VarHandle#getVolatile.

final void lazySet(int newValue)

Sets the value to newValue, with memory effects as specified by VarHandle#setRelease.

long longValue()

Returns the current value of this AtomicInteger as a long after a widening primitive conversion, with memory effects as specified by VarHandle#getVolatile.

final void set(int newValue)

Sets the value to newValue, with memory effects as specified by VarHandle#setVolatile.

final void setOpaque(int newValue)

Sets the value to newValue, with memory effects as specified by VarHandle#setOpaque.

final void setPlain(int newValue)

Sets the value to newValue, with memory semantics of setting as if the variable was declared non-volatile and non-final.

final void setRelease(int newValue)

Sets the value to newValue, with memory effects as specified by VarHandle#setRelease.

String toString()

Returns the String representation of the current value.

final int updateAndGet(IntUnaryOperator updateFunction)

Atomically updates (with memory effects as specified by VarHandle.compareAndSet(Object)) the current value with the results of applying the given function, returning the updated value.

final boolean weakCompareAndSet(int expectedValue, int newValue)

This method was deprecated in API level 33. This method has plain memory effects but the method name implies volatile memory effects (see methods such as compareAndExchange(int, int) and compareAndSet(int, int)). To avoid confusion over plain or volatile memory effects it is recommended that the method weakCompareAndSetPlain(int, int) be used instead.

final boolean weakCompareAndSetAcquire(int expectedValue, int newValue)

Possibly atomically sets the value to newValue if the current value == expectedValue, with memory effects as specified by VarHandle#weakCompareAndSetAcquire.

final boolean weakCompareAndSetPlain(int expectedValue, int newValue)

Possibly atomically sets the value to newValue if the current value == expectedValue, with memory effects as specified by VarHandle#weakCompareAndSetPlain.

final boolean weakCompareAndSetRelease(int expectedValue, int newValue)

Possibly atomically sets the value to newValue if the current value == expectedValue, with memory effects as specified by VarHandle#weakCompareAndSetRelease.

final boolean weakCompareAndSetVolatile(int expectedValue, int newValue)

Possibly atomically sets the value to newValue if the current value == expectedValue, with memory effects as specified by VarHandle#weakCompareAndSet.

Inherited methods

Public constructors

AtomicInteger

Added in API level 1
public AtomicInteger (int initialValue)

Creates a new AtomicInteger with the given initial value.

Parameters
initialValue int: the initial value

AtomicInteger

Added in API level 1
public AtomicInteger ()

Creates a new AtomicInteger with initial value 0.

Public methods

accumulateAndGet

Added in API level 24
public final int accumulateAndGet (int x, 
                IntBinaryOperator accumulatorFunction)

Atomically updates (with memory effects as specified by VarHandle.compareAndSet(Object)) the current value with the results of applying the given function to the current and given values, returning the updated value. The function should be side-effect-free, since it may be re-applied when attempted updates fail due to contention among threads. The function is applied with the current value as its first argument, and the given update as the second argument.

Parameters
x int: the update value

accumulatorFunction IntBinaryOperator: a side-effect-free function of two arguments

Returns
int the updated value

addAndGet

Added in API level 1
public final int addAndGet (int delta)

Atomically adds the given value to the current value, with memory effects as specified by VarHandle#getAndAdd.

Parameters
delta int: the value to add

Returns
int the updated value

compareAndExchange

Added in API level 33
public final int compareAndExchange (int expectedValue, 
                int newValue)

Atomically sets the value to newValue if the current value, referred to as the witness value, == expectedValue, with memory effects as specified by VarHandle#compareAndExchange.

Parameters
expectedValue int: the expected value

newValue int: the new value

Returns
int the witness value, which will be the same as the expected value if successful

compareAndExchangeAcquire

Added in API level 33
public final int compareAndExchangeAcquire (int expectedValue, 
                int newValue)

Atomically sets the value to newValue if the current value, referred to as the witness value, == expectedValue, with memory effects as specified by VarHandle#compareAndExchangeAcquire.

Parameters
expectedValue int: the expected value

newValue int: the new value

Returns
int the witness value, which will be the same as the expected value if successful

compareAndExchangeRelease

Added in API level 33
public final int compareAndExchangeRelease (int expectedValue, 
                int newValue)

Atomically sets the value to newValue if the current value, referred to as the witness value, == expectedValue, with memory effects as specified by VarHandle#compareAndExchangeRelease.

Parameters
expectedValue int: the expected value

newValue int: the new value

Returns
int the witness value, which will be the same as the expected value if successful

compareAndSet

Added in API level 1
public final boolean compareAndSet (int expectedValue, 
                int newValue)

Atomically sets the value to newValue if the current value == expectedValue, with memory effects as specified by VarHandle#compareAndSet.

Parameters
expectedValue int: the expected value

newValue int: the new value

Returns
boolean true if successful. False return indicates that the actual value was not equal to the expected value.

decrementAndGet

Added in API level 1
public final int decrementAndGet ()

Atomically decrements the current value, with memory effects as specified by VarHandle#getAndAdd.

Equivalent to addAndGet(-1).

Returns
int the updated value

doubleValue

Added in API level 1
public double doubleValue ()

Returns the current value of this AtomicInteger as a double after a widening primitive conversion, with memory effects as specified by VarHandle#getVolatile.

Returns
double the numeric value represented by this object after conversion to type double.

floatValue

Added in API level 1
public float floatValue ()

Returns the current value of this AtomicInteger as a float after a widening primitive conversion, with memory effects as specified by VarHandle#getVolatile.

Returns
float the numeric value represented by this object after conversion to type float.

get

Added in API level 1
public final int get ()

Returns the current value, with memory effects as specified by VarHandle#getVolatile.

Returns
int the current value

getAcquire

Added in API level 33
public final int getAcquire ()

Returns the current value, with memory effects as specified by VarHandle#getAcquire.

Returns
int the value

getAndAccumulate

Added in API level 24
public final int getAndAccumulate (int x, 
                IntBinaryOperator accumulatorFunction)

Atomically updates (with memory effects as specified by VarHandle.compareAndSet(Object)) the current value with the results of applying the given function to the current and given values, returning the previous value. The function should be side-effect-free, since it may be re-applied when attempted updates fail due to contention among threads. The function is applied with the current value as its first argument, and the given update as the second argument.

Parameters
x int: the update value

accumulatorFunction IntBinaryOperator: a side-effect-free function of two arguments

Returns
int the previous value

getAndAdd

Added in API level 1
public final int getAndAdd (int delta)

Atomically adds the given value to the current value, with memory effects as specified by VarHandle#getAndAdd.

Parameters
delta int: the value to add

Returns
int the previous value

getAndDecrement

Added in API level 1
public final int getAndDecrement ()

Atomically decrements the current value, with memory effects as specified by VarHandle#getAndAdd.

Equivalent to getAndAdd(-1).

Returns
int the previous value

getAndIncrement

Added in API level 1
public final int getAndIncrement ()

Atomically increments the current value, with memory effects as specified by VarHandle#getAndAdd.

Equivalent to getAndAdd(1).

Returns
int the previous value

getAndSet

Added in API level 1
public final int getAndSet (int newValue)

Atomically sets the value to newValue and returns the old value, with memory effects as specified by VarHandle#getAndSet.

Parameters
newValue int: the new value

Returns
int the previous value

getAndUpdate

Added in API level 24
public final int getAndUpdate (IntUnaryOperator updateFunction)

Atomically updates (with memory effects as specified by VarHandle.compareAndSet(Object)) the current value with the results of applying the given function, returning the previous value. The function should be side-effect-free, since it may be re-applied when attempted updates fail due to contention among threads.

Parameters
updateFunction IntUnaryOperator: a side-effect-free function

Returns
int the previous value

getOpaque

Added in API level 33
public final int getOpaque ()

Returns the current value, with memory effects as specified by VarHandle#getOpaque.

Returns
int the value

getPlain

Added in API level 33
public final int getPlain ()

Returns the current value, with memory semantics of reading as if the variable was declared non-volatile.

Returns
int the value

incrementAndGet

Added in API level 1
public final int incrementAndGet ()

Atomically increments the current value, with memory effects as specified by VarHandle#getAndAdd.

Equivalent to addAndGet(1).

Returns
int the updated value

intValue

Added in API level 1
public int intValue ()

Returns the current value of this AtomicInteger as an int, with memory effects as specified by VarHandle#getVolatile. Equivalent to get().

Returns
int the numeric value represented by this object after conversion to type int.

lazySet

Added in API level 9
public final void lazySet (int newValue)

Sets the value to newValue, with memory effects as specified by VarHandle#setRelease.

Parameters
newValue int: the new value

longValue

Added in API level 1
public long longValue ()

Returns the current value of this AtomicInteger as a long after a widening primitive conversion, with memory effects as specified by VarHandle#getVolatile.

Returns
long the numeric value represented by this object after conversion to type long.

set

Added in API level 1
public final void set (int newValue)

Sets the value to newValue, with memory effects as specified by VarHandle#setVolatile.

Parameters
newValue int: the new value

setOpaque

Added in API level 33
public final void setOpaque (int newValue)

Sets the value to newValue, with memory effects as specified by VarHandle#setOpaque.

Parameters
newValue int: the new value

setPlain

Added in API level 33
public final void setPlain (int newValue)

Sets the value to newValue, with memory semantics of setting as if the variable was declared non-volatile and non-final.

Parameters
newValue int: the new value

setRelease

Added in API level 33
public final void setRelease (int newValue)

Sets the value to newValue, with memory effects as specified by VarHandle#setRelease.

Parameters
newValue int: the new value

toString

Added in API level 1
public String toString ()

Returns the String representation of the current value.

Returns
String the String representation of the current value

updateAndGet

Added in API level 24
public final int updateAndGet (IntUnaryOperator updateFunction)

Atomically updates (with memory effects as specified by VarHandle.compareAndSet(Object)) the current value with the results of applying the given function, returning the updated value. The function should be side-effect-free, since it may be re-applied when attempted updates fail due to contention among threads.

Parameters
updateFunction IntUnaryOperator: a side-effect-free function

Returns
int the updated value

weakCompareAndSet

Added in API level 1
Deprecated in API level 33
public final boolean weakCompareAndSet (int expectedValue, 
                int newValue)

This method was deprecated in API level 33.
This method has plain memory effects but the method name implies volatile memory effects (see methods such as compareAndExchange(int, int) and compareAndSet(int, int)). To avoid confusion over plain or volatile memory effects it is recommended that the method weakCompareAndSetPlain(int, int) be used instead.

Possibly atomically sets the value to newValue if the current value == expectedValue, with memory effects as specified by VarHandle#weakCompareAndSetPlain.

Parameters
expectedValue int: the expected value

newValue int: the new value

Returns
boolean true if successful

weakCompareAndSetAcquire

Added in API level 33
public final boolean weakCompareAndSetAcquire (int expectedValue, 
                int newValue)

Possibly atomically sets the value to newValue if the current value == expectedValue, with memory effects as specified by VarHandle#weakCompareAndSetAcquire.

Parameters
expectedValue int: the expected value

newValue int: the new value

Returns
boolean true if successful

weakCompareAndSetPlain

Added in API level 33
public final boolean weakCompareAndSetPlain (int expectedValue, 
                int newValue)

Possibly atomically sets the value to newValue if the current value == expectedValue, with memory effects as specified by VarHandle#weakCompareAndSetPlain.

Parameters
expectedValue int: the expected value

newValue int: the new value

Returns
boolean true if successful

weakCompareAndSetRelease

Added in API level 33
public final boolean weakCompareAndSetRelease (int expectedValue, 
                int newValue)

Possibly atomically sets the value to newValue if the current value == expectedValue, with memory effects as specified by VarHandle#weakCompareAndSetRelease.

Parameters
expectedValue int: the expected value

newValue int: the new value

Returns
boolean true if successful

weakCompareAndSetVolatile

Added in API level 33
public final boolean weakCompareAndSetVolatile (int expectedValue, 
                int newValue)

Possibly atomically sets the value to newValue if the current value == expectedValue, with memory effects as specified by VarHandle#weakCompareAndSet.

Parameters
expectedValue int: the expected value

newValue int: the new value

Returns
boolean true if successful