APIdia Launch Partner
Top Description Fields Constructors Methods
java.util.concurrent.atomic

public Class AtomicInteger

extends Number
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Imports
java.lang.invoke.VarHandle, java.util.function.IntBinaryOperator, .IntUnaryOperator, jdk.internal.misc.Unsafe

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 java.lang.Integer. However, this class does extend Number to allow uniform access by tools and utilities that deal with numerically-based classes.
Author
Doug Lea
Since
1.5

Field Summary

Modifier and TypeField and Description
private static final long
private static final Unsafe
U

private static final long
private volatile int

Constructor Summary

AccessConstructor and Description
public
AtomicInteger(int
the initial value
initialValue
)

Creates a new AtomicInteger with the given initial value.

public
AtomicInteger()

Creates a new AtomicInteger with initial value 0.

Method Summary

Modifier and TypeMethod and Description
public final int

Returns:

the updated value
accumulateAndGet
(int
the update value
x
,
IntBinaryOperator
a side-effect-free function of two arguments
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.

public final int

Returns:

the updated value
addAndGet
(int
the value to add
delta
)

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

public final int

Returns:

the witness value, which will be the same as the expected value if successful
compareAndExchange
(int
the expected value
expectedValue
,
int
the new value
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.

public final int

Returns:

the witness value, which will be the same as the expected value if successful
compareAndExchangeAcquire
(int
the expected value
expectedValue
,
int
the new value
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.

public final int

Returns:

the witness value, which will be the same as the expected value if successful
compareAndExchangeRelease
(int
the expected value
expectedValue
,
int
the new value
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.

public final boolean

Returns:

true if successful. False return indicates that the actual value was not equal to the expected value.
compareAndSet
(int
the expected value
expectedValue
,
int
the new value
newValue
)

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

public final int

Returns:

the updated value
decrementAndGet
()

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

public double
doubleValue()

Implements abstract java.lang.Number.doubleValue.

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

public float
floatValue()

Implements abstract java.lang.Number.floatValue.

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

public final int

Returns:

the current value
get
()

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

public final int

Returns:

the value
getAcquire
()

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

public final int

Returns:

the previous value
getAndAccumulate
(int
the update value
x
,
IntBinaryOperator
a side-effect-free function of two arguments
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.

public final int

Returns:

the previous value
getAndAdd
(int
the value to add
delta
)

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

public final int

Returns:

the previous value
getAndDecrement
()

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

public final int

Returns:

the previous value
getAndIncrement
()

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

public final int

Returns:

the previous value
getAndSet
(int
the new value
newValue
)

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

public final int

Returns:

the previous value
getAndUpdate
(IntUnaryOperator
a side-effect-free function
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.

public final int

Returns:

the value
getOpaque
()

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

public final int

Returns:

the value
getPlain
()

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

public final int

Returns:

the updated value
incrementAndGet
()

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

public int
intValue()

Implements abstract java.lang.Number.intValue.

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

public final void
lazySet(int
the new value
newValue
)

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

public long
longValue()

Implements abstract java.lang.Number.longValue.

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

public final void
set(int
the new value
newValue
)

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

public final void
setOpaque(int
the new value
newValue
)

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

public final void
setPlain(int
the new value
newValue
)

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

public final void
setRelease(int
the new value
newValue
)

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

public String

Returns:

the String representation of the current value
toString
()

Overrides java.lang.Object.toString.

Returns the String representation of the current value.

public final int

Returns:

the updated value
updateAndGet
(IntUnaryOperator
a side-effect-free function
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.

public final boolean

Returns:

true if successful
weakCompareAndSet
(int
the expected value
expectedValue
,
int
the new value
newValue
)
Deprecated since 9. This method has plain memory effects but the method name implies volatile memory effects (see methods such as compareAndExchange and compareAndSet).

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

public final boolean

Returns:

true if successful
weakCompareAndSetAcquire
(int
the expected value
expectedValue
,
int
the new value
newValue
)

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

public final boolean

Returns:

true if successful
weakCompareAndSetPlain
(int
the expected value
expectedValue
,
int
the new value
newValue
)

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

public final boolean

Returns:

true if successful
weakCompareAndSetRelease
(int
the expected value
expectedValue
,
int
the new value
newValue
)

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

public final boolean

Returns:

true if successful
weakCompareAndSetVolatile
(int
the expected value
expectedValue
,
int
the new value
newValue
)

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

Inherited from java.lang.Number:
byteValueshortValue

Field Detail

serialVersionUIDback to summary
private static final long serialVersionUID

Hides java.lang.Number.serialVersionUID.

Uback to summary
private static final Unsafe U
VALUEback to summary
private static final long VALUE
valueback to summary
private volatile int value

Constructor Detail

AtomicIntegerback to summary
public AtomicInteger(int initialValue)

Creates a new AtomicInteger with the given initial value.

Parameters
initialValue:int

the initial value

AtomicIntegerback to summary
public AtomicInteger()

Creates a new AtomicInteger with initial value 0.

Method Detail

accumulateAndGetback to summary
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

Since
1.8
addAndGetback to summary
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

compareAndExchangeback to summary
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

Since
9
compareAndExchangeAcquireback to summary
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

Since
9
compareAndExchangeReleaseback to summary
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

Since
9
compareAndSetback to summary
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.

decrementAndGetback to summary
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

doubleValueback to summary
public double doubleValue()

Implements abstract java.lang.Number.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 Language Specification
5.1.2 Widening Primitive Conversion
floatValueback to summary
public float floatValue()

Implements abstract java.lang.Number.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 Language Specification
5.1.2 Widening Primitive Conversion
getback to summary
public final int get()

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

Returns:int

the current value

getAcquireback to summary
public final int getAcquire()

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

Returns:int

the value

Since
9
getAndAccumulateback to summary
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

Since
1.8
getAndAddback to summary
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

getAndDecrementback to summary
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

getAndIncrementback to summary
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

getAndSetback to summary
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

getAndUpdateback to summary
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

Since
1.8
getOpaqueback to summary
public final int getOpaque()

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

Returns:int

the value

Since
9
getPlainback to summary
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

Since
9
incrementAndGetback to summary
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

intValueback to summary
public int intValue()

Implements abstract java.lang.Number.intValue.

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

lazySetback to summary
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

Since
1.6
longValueback to summary
public long longValue()

Implements abstract java.lang.Number.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 Language Specification
5.1.2 Widening Primitive Conversion
setback to summary
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

setOpaqueback to summary
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

Since
9
setPlainback to summary
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

Since
9
setReleaseback to summary
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

Since
9
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Returns the String representation of the current value.

Returns:String

the String representation of the current value

updateAndGetback to summary
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

Since
1.8
weakCompareAndSetback to summary
public final boolean weakCompareAndSet(int expectedValue, int newValue)

Deprecated

since 9.

This method has plain memory effects but the method name implies volatile memory effects (see methods such as compareAndExchange and compareAndSet). To avoid confusion over plain or volatile memory effects it is recommended that the method weakCompareAndSetPlain 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

Annotations
@Deprecated
since:9
See Also
weakCompareAndSetPlain
weakCompareAndSetAcquireback to summary
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

Since
9
weakCompareAndSetPlainback to summary
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

Since
9
weakCompareAndSetReleaseback to summary
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

Since
9
weakCompareAndSetVolatileback to summary
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

Since
9