AtomicInteger (Java SE 15 & JDK 15)
Description: declaration: module: java.base, package: java.util.concurrent.atomic, class: AtomicInteger
Keywords: java.util.concurrent.atomic.AtomicInteger class

Module java.base
Package java.util.concurrent.atomic

# Class AtomicInteger

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

All Implemented Interfaces:
  `Serializable`

    public class AtomicInteger
    extends Number
    implements Serializable

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.

Since:
  1.5
See Also:
  Serialized Form

- ## Constructor Summary

  Constructors
  Constructor | Description
  --- | ---
  `AtomicInteger()` | Creates a new AtomicInteger with initial value `0`.
  `AtomicInteger​(int initialValue)` | Creates a new AtomicInteger with the given initial value.

- ## Method Summary

  Modifier and Type | Method | Description
  --- | --- | ---
  `int` | `accumulateAndGet​(int x, IntBinaryOperator accumulatorFunction)` | Atomically updates (with memory effects as specified by `VarHandle.compareAndSet(java.lang.Object...)`) the current value with the results of applying the given function to the current and given values, returning the updated value.
  `int` | `addAndGet​(int delta)` | Atomically adds the given value to the current value, with memory effects as specified by `VarHandle.getAndAdd(java.lang.Object...)`.
  `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(java.lang.Object...)`.
  `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(java.lang.Object...)`.
  `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(java.lang.Object...)`.
  `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(java.lang.Object...)`.
  `int` | `decrementAndGet()` | Atomically decrements the current value, with memory effects as specified by `VarHandle.getAndAdd(java.lang.Object...)`.
  `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(java.lang.Object...)`.
  `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(java.lang.Object...)`.
  `int` | `get()` | Returns the current value, with memory effects as specified by `VarHandle.getVolatile(java.lang.Object...)`.
  `int` | `getAcquire()` | Returns the current value, with memory effects as specified by `VarHandle.getAcquire(java.lang.Object...)`.
  `int` | `getAndAccumulate​(int x, IntBinaryOperator accumulatorFunction)` | Atomically updates (with memory effects as specified by `VarHandle.compareAndSet(java.lang.Object...)`) the current value with the results of applying the given function to the current and given values, returning the previous value.
  `int` | `getAndAdd​(int delta)` | Atomically adds the given value to the current value, with memory effects as specified by `VarHandle.getAndAdd(java.lang.Object...)`.
  `int` | `getAndDecrement()` | Atomically decrements the current value, with memory effects as specified by `VarHandle.getAndAdd(java.lang.Object...)`.
  `int` | `getAndIncrement()` | Atomically increments the current value, with memory effects as specified by `VarHandle.getAndAdd(java.lang.Object...)`.
  `int` | `getAndSet​(int newValue)` | Atomically sets the value to `newValue` and returns the old value, with memory effects as specified by `VarHandle.getAndSet(java.lang.Object...)`.
  `int` | `getAndUpdate​(IntUnaryOperator updateFunction)` | Atomically updates (with memory effects as specified by `VarHandle.compareAndSet(java.lang.Object...)`) the current value with the results of applying the given function, returning the previous value.
  `int` | `getOpaque()` | Returns the current value, with memory effects as specified by `VarHandle.getOpaque(java.lang.Object...)`.
  `int` | `getPlain()` | Returns the current value, with memory semantics of reading as if the variable was declared non-`volatile`.
  `int` | `incrementAndGet()` | Atomically increments the current value, with memory effects as specified by `VarHandle.getAndAdd(java.lang.Object...)`.
  `int` | `intValue()` | Returns the current value of this `AtomicInteger` as an `int`, with memory effects as specified by `VarHandle.getVolatile(java.lang.Object...)`.
  `void` | `lazySet​(int newValue)` | Sets the value to `newValue`, with memory effects as specified by `VarHandle.setRelease(java.lang.Object...)`.
  `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(java.lang.Object...)`.
  `void` | `set​(int newValue)` | Sets the value to `newValue`, with memory effects as specified by `VarHandle.setVolatile(java.lang.Object...)`.
  `void` | `setOpaque​(int newValue)` | Sets the value to `newValue`, with memory effects as specified by `VarHandle.setOpaque(java.lang.Object...)`.
  `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`.
  `void` | `setRelease​(int newValue)` | Sets the value to `newValue`, with memory effects as specified by `VarHandle.setRelease(java.lang.Object...)`.
  `String` | `toString()` | Returns the String representation of the current value.
  `int` | `updateAndGet​(IntUnaryOperator updateFunction)` | Atomically updates (with memory effects as specified by `VarHandle.compareAndSet(java.lang.Object...)`) the current value with the results of applying the given function, returning the updated value.
  `boolean` | `weakCompareAndSet​(int expectedValue, int newValue)` | Deprecated. 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)`).
  `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(java.lang.Object...)`.
  `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(java.lang.Object...)`.
  `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(java.lang.Object...)`.
  `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(java.lang.Object...)`.

  ### Methods declared in class java.lang.Number

  `byteValue, shortValue`

  ### Methods declared in class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- ## Constructor Details

  - ### AtomicInteger

    public AtomicInteger​(int initialValue)
    Creates a new AtomicInteger with the given initial value.
    Parameters:
      `initialValue` - the initial value

  - ### AtomicInteger

    public AtomicInteger()
    Creates a new AtomicInteger with initial value `0`.

- ## Method Details

  - ### get

    public final int get()
    Returns the current value, with memory effects as specified by `VarHandle.getVolatile(java.lang.Object...)`.
    Returns:
      the current value

  - ### set

    public final void set​(int newValue)
    Sets the value to `newValue`, with memory effects as specified by `VarHandle.setVolatile(java.lang.Object...)`.
    Parameters:
      `newValue` - the new value

  - ### lazySet

    public final void lazySet​(int newValue)
    Sets the value to `newValue`, with memory effects as specified by `VarHandle.setRelease(java.lang.Object...)`.
    Parameters:
      `newValue` - the new value
    Since:
      1.6

  - ### 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(java.lang.Object...)`.
    Parameters:
      `newValue` - the new value
    Returns:
      the previous value

  - ### 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(java.lang.Object...)`.
    Parameters:
      `expectedValue` - the expected value
      `newValue` - the new value
    Returns:
      `true` if successful. False return indicates that the actual value was not equal to the expected value.

  - ### weakCompareAndSet

    @Deprecated(since="9") public final boolean weakCompareAndSet​(int expectedValue, int newValue)
    Deprecated.
    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(java.lang.Object...)`.
    Parameters:
      `expectedValue` - the expected value
      `newValue` - the new value
    Returns:
      `true` if successful
    See Also:
      `weakCompareAndSetPlain(int, int)`

  - ### 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(java.lang.Object...)`.
    Parameters:
      `expectedValue` - the expected value
      `newValue` - the new value
    Returns:
      `true` if successful
    Since:
      9

  - ### getAndIncrement

    public final int getAndIncrement()
    Atomically increments the current value, with memory effects as specified by `VarHandle.getAndAdd(java.lang.Object...)`.

    Equivalent to `getAndAdd(1)`.

    Returns:
      the previous value

  - ### getAndDecrement

    public final int getAndDecrement()
    Atomically decrements the current value, with memory effects as specified by `VarHandle.getAndAdd(java.lang.Object...)`.

    Equivalent to `getAndAdd(-1)`.

    Returns:
      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(java.lang.Object...)`.
    Parameters:
      `delta` - the value to add
    Returns:
      the previous value

  - ### incrementAndGet

    public final int incrementAndGet()
    Atomically increments the current value, with memory effects as specified by `VarHandle.getAndAdd(java.lang.Object...)`.

    Equivalent to `addAndGet(1)`.

    Returns:
      the updated value

  - ### decrementAndGet

    public final int decrementAndGet()
    Atomically decrements the current value, with memory effects as specified by `VarHandle.getAndAdd(java.lang.Object...)`.

    Equivalent to `addAndGet(-1)`.

    Returns:
      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(java.lang.Object...)`.
    Parameters:
      `delta` - the value to add
    Returns:
      the updated value

  - ### getAndUpdate

    public final int getAndUpdate​(IntUnaryOperator updateFunction)
    Atomically updates (with memory effects as specified by `VarHandle.compareAndSet(java.lang.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` - a side-effect-free function
    Returns:
      the previous value
    Since:
      1.8

  - ### updateAndGet

    public final int updateAndGet​(IntUnaryOperator updateFunction)
    Atomically updates (with memory effects as specified by `VarHandle.compareAndSet(java.lang.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` - a side-effect-free function
    Returns:
      the updated value
    Since:
      1.8

  - ### getAndAccumulate

    public final int getAndAccumulate​(int x, IntBinaryOperator accumulatorFunction)
    Atomically updates (with memory effects as specified by `VarHandle.compareAndSet(java.lang.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` - the update value
      `accumulatorFunction` - a side-effect-free function of two arguments
    Returns:
      the previous value
    Since:
      1.8

  - ### accumulateAndGet

    public final int accumulateAndGet​(int x, IntBinaryOperator accumulatorFunction)
    Atomically updates (with memory effects as specified by `VarHandle.compareAndSet(java.lang.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` - the update value
      `accumulatorFunction` - a side-effect-free function of two arguments
    Returns:
      the updated value
    Since:
      1.8

  - ### toString

    public String toString()
    Returns the String representation of the current value.
    Overrides:
      `toString` in class `Object`
    Returns:
      the String representation of the current value

  - ### intValue

    public int intValue()
    Returns the current value of this `AtomicInteger` as an `int`, with memory effects as specified by `VarHandle.getVolatile(java.lang.Object...)`. Equivalent to `get()`.
    Specified by:
      `intValue` in class `Number`
    Returns:
      the numeric value represented by this object after conversion to type `int`.

  - ### 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(java.lang.Object...)`.
    Specified by:
      `longValue` in class `Number`
    Returns:
      the numeric value represented by this object after conversion to type `long`.
    See Java Language Specification:
      5.1.2 Widening Primitive Conversion

  - ### 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(java.lang.Object...)`.
    Specified by:
      `floatValue` in class `Number`
    Returns:
      the numeric value represented by this object after conversion to type `float`.
    See Java Language Specification:
      5.1.2 Widening Primitive Conversion

  - ### 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(java.lang.Object...)`.
    Specified by:
      `doubleValue` in class `Number`
    Returns:
      the numeric value represented by this object after conversion to type `double`.
    See Java Language Specification:
      5.1.2 Widening Primitive Conversion

  - ### getPlain

    public final int getPlain()
    Returns the current value, with memory semantics of reading as if the variable was declared non-`volatile`.
    Returns:
      the value
    Since:
      9

  - ### 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` - the new value
    Since:
      9

  - ### getOpaque

    public final int getOpaque()
    Returns the current value, with memory effects as specified by `VarHandle.getOpaque(java.lang.Object...)`.
    Returns:
      the value
    Since:
      9

  - ### setOpaque

    public final void setOpaque​(int newValue)
    Sets the value to `newValue`, with memory effects as specified by `VarHandle.setOpaque(java.lang.Object...)`.
    Parameters:
      `newValue` - the new value
    Since:
      9

  - ### getAcquire

    public final int getAcquire()
    Returns the current value, with memory effects as specified by `VarHandle.getAcquire(java.lang.Object...)`.
    Returns:
      the value
    Since:
      9

  - ### setRelease

    public final void setRelease​(int newValue)
    Sets the value to `newValue`, with memory effects as specified by `VarHandle.setRelease(java.lang.Object...)`.
    Parameters:
      `newValue` - the new value
    Since:
      9

  - ### 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(java.lang.Object...)`.
    Parameters:
      `expectedValue` - the expected value
      `newValue` - the new value
    Returns:
      the witness value, which will be the same as the expected value if successful
    Since:
      9

  - ### 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(java.lang.Object...)`.
    Parameters:
      `expectedValue` - the expected value
      `newValue` - the new value
    Returns:
      the witness value, which will be the same as the expected value if successful
    Since:
      9

  - ### 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(java.lang.Object...)`.
    Parameters:
      `expectedValue` - the expected value
      `newValue` - the new value
    Returns:
      the witness value, which will be the same as the expected value if successful
    Since:
      9

  - ### 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(java.lang.Object...)`.
    Parameters:
      `expectedValue` - the expected value
      `newValue` - the new value
    Returns:
      `true` if successful
    Since:
      9

  - ### 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(java.lang.Object...)`.
    Parameters:
      `expectedValue` - the expected value
      `newValue` - the new value
    Returns:
      `true` if successful
    Since:
      9

  - ### 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(java.lang.Object...)`.
    Parameters:
      `expectedValue` - the expected value
      `newValue` - the new value
    Returns:
      `true` if successful
    Since:
      9