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()

Creates a new AtomicInteger with initial value 0.

AtomicInteger(int initialValue)

Creates a new AtomicInteger with the given initial value.

Public methods

final int accumulateAndGet(int x, IntBinaryOperator accumulatorFunction)

Atomically updates (with memory effects as specified by VarHandle.compareAndSet) 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) 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) 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()

final int updateAndGet(IntUnaryOperator updateFunction)

Atomically updates (with memory effects as specified by VarHandle.compareAndSet) 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

public AtomicInteger ()

Creates a new AtomicInteger with initial value 0.

AtomicInteger

public AtomicInteger (int initialValue)

Creates a new AtomicInteger with the given initial value.

Parameters
initialValue int: the initial value

Public methods

accumulateAndGet

public final int accumulateAndGet (int x, 
                IntBinaryOperator accumulatorFunction)

Atomically updates (with memory effects as specified by VarHandle.compareAndSet) 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

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

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

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

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

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

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

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

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

public final int get ()

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

Returns
int the current value

getAcquire

public final int getAcquire ()

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

Returns
int the value

getAndAccumulate

public final int getAndAccumulate (int x, 
                IntBinaryOperator accumulatorFunction)

Atomically updates (with memory effects as specified by VarHandle.compareAndSet) 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

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

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

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

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

public final int getAndUpdate (IntUnaryOperator updateFunction)

Atomically updates (with memory effects as specified by VarHandle.compareAndSet) 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

public final int getOpaque ()

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

Returns
int the value

getPlain

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

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

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

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

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

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

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

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

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

public String toString ()

Returns
String the String representation of the current value

updateAndGet

public final int updateAndGet (IntUnaryOperator updateFunction)

Atomically updates (with memory effects as specified by VarHandle.compareAndSet) 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


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

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

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

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

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