Serializable
public class AtomicInteger extends Number implements Serializable
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.Constructor | Description |
---|---|
AtomicInteger() |
Creates a new AtomicInteger with initial value 0 . |
AtomicInteger |
Creates a new AtomicInteger with the given initial value. |
Modifier and Type | Method | Description |
---|---|---|
final int |
accumulateAndGet |
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. |
final int |
addAndGet |
Atomically adds the given value to the current value, with memory effects as specified by VarHandle.getAndAdd(java.lang.Object...) . |
final int |
compareAndExchange |
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...) . |
final int |
compareAndExchangeAcquire |
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...) . |
final int |
compareAndExchangeRelease |
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...) . |
final boolean |
compareAndSet |
Atomically sets the value to newValue if the current value == expectedValue , with memory effects as specified by VarHandle.compareAndSet(java.lang.Object...) . |
final 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...) . |
final int |
get() |
Returns the current value, with memory effects as specified by VarHandle.getVolatile(java.lang.Object...) . |
final int |
getAcquire() |
Returns the current value, with memory effects as specified by VarHandle.getAcquire(java.lang.Object...) . |
final int |
getAndAccumulate |
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. |
final int |
getAndAdd |
Atomically adds the given value to the current value, with memory effects as specified by VarHandle.getAndAdd(java.lang.Object...) . |
final int |
getAndDecrement() |
Atomically decrements the current value, with memory effects as specified by VarHandle.getAndAdd(java.lang.Object...) . |
final int |
getAndIncrement() |
Atomically increments the current value, with memory effects as specified by VarHandle.getAndAdd(java.lang.Object...) . |
final int |
getAndSet |
Atomically sets the value to newValue and returns the old value, with memory effects as specified by VarHandle.getAndSet(java.lang.Object...) . |
final int |
getAndUpdate |
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. |
final int |
getOpaque() |
Returns the current value, with memory effects as specified by VarHandle.getOpaque(java.lang.Object...) . |
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(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...) . |
final void |
lazySet |
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...) . |
final void |
set |
Sets the value to newValue , with memory effects as specified by VarHandle.setVolatile(java.lang.Object...) . |
final void |
setOpaque |
Sets the value to newValue , with memory effects as specified by VarHandle.setOpaque(java.lang.Object...) . |
final void |
setPlain |
Sets the value to newValue , with memory semantics of setting as if the variable was declared non-volatile and non-final . |
final void |
setRelease |
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. |
final int |
updateAndGet |
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. |
final boolean |
weakCompareAndSet |
Deprecated. |
final boolean |
weakCompareAndSetAcquire |
Possibly atomically sets the value to newValue if the current value == expectedValue , with memory effects as specified by VarHandle.weakCompareAndSetAcquire(java.lang.Object...) . |
final boolean |
weakCompareAndSetPlain |
Possibly atomically sets the value to newValue if the current value == expectedValue , with memory effects as specified by VarHandle.weakCompareAndSetPlain(java.lang.Object...) . |
final boolean |
weakCompareAndSetRelease |
Possibly atomically sets the value to newValue if the current value == expectedValue , with memory effects as specified by VarHandle.weakCompareAndSetRelease(java.lang.Object...) . |
final boolean |
weakCompareAndSetVolatile |
Possibly atomically sets the value to newValue if the current value == expectedValue , with memory effects as specified by VarHandle.weakCompareAndSet(java.lang.Object...) . |
byteValue, shortValue
public AtomicInteger(int initialValue)
initialValue
- the initial valuepublic AtomicInteger()
0
.public final int get()
VarHandle.getVolatile(java.lang.Object...)
.public final void set(int newValue)
newValue
, with memory effects as specified by VarHandle.setVolatile(java.lang.Object...)
.newValue
- the new valuepublic final void lazySet(int newValue)
newValue
, with memory effects as specified by VarHandle.setRelease(java.lang.Object...)
.newValue
- the new valuepublic final int getAndSet(int newValue)
newValue
and returns the old value, with memory effects as specified by VarHandle.getAndSet(java.lang.Object...)
.newValue
- the new valuepublic final boolean compareAndSet(int expectedValue, int newValue)
newValue
if the current value == expectedValue
, with memory effects as specified by VarHandle.compareAndSet(java.lang.Object...)
.expectedValue
- the expected valuenewValue
- the new valuetrue
if successful. False return indicates that the actual value was not equal to the expected value.@Deprecated(since="9") public final boolean weakCompareAndSet(int expectedValue, int newValue)
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.newValue
if the current value == expectedValue
, with memory effects as specified by VarHandle.weakCompareAndSetPlain(java.lang.Object...)
.expectedValue
- the expected valuenewValue
- the new valuetrue
if successfulpublic final boolean weakCompareAndSetPlain(int expectedValue, int newValue)
newValue
if the current value == expectedValue
, with memory effects as specified by VarHandle.weakCompareAndSetPlain(java.lang.Object...)
.expectedValue
- the expected valuenewValue
- the new valuetrue
if successfulpublic final int getAndIncrement()
VarHandle.getAndAdd(java.lang.Object...)
. Equivalent to getAndAdd(1)
.
public final int getAndDecrement()
VarHandle.getAndAdd(java.lang.Object...)
. Equivalent to getAndAdd(-1)
.
public final int getAndAdd(int delta)
VarHandle.getAndAdd(java.lang.Object...)
.delta
- the value to addpublic final int incrementAndGet()
VarHandle.getAndAdd(java.lang.Object...)
. Equivalent to addAndGet(1)
.
public final int decrementAndGet()
VarHandle.getAndAdd(java.lang.Object...)
. Equivalent to addAndGet(-1)
.
public final int addAndGet(int delta)
VarHandle.getAndAdd(java.lang.Object...)
.delta
- the value to addpublic final int getAndUpdate(IntUnaryOperator updateFunction)
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.updateFunction
- a side-effect-free functionpublic final int updateAndGet(IntUnaryOperator updateFunction)
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.updateFunction
- a side-effect-free functionpublic final int getAndAccumulate(int x, IntBinaryOperator accumulatorFunction)
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.x
- the update valueaccumulatorFunction
- a side-effect-free function of two argumentspublic final int accumulateAndGet(int x, IntBinaryOperator accumulatorFunction)
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.x
- the update valueaccumulatorFunction
- a side-effect-free function of two argumentspublic String toString()
public int intValue()
AtomicInteger
as an int
, with memory effects as specified by VarHandle.getVolatile(java.lang.Object...)
. Equivalent to get()
.public long longValue()
AtomicInteger
as a long
after a widening primitive conversion, with memory effects as specified by VarHandle.getVolatile(java.lang.Object...)
.longValue
in class Number
long
.public float floatValue()
AtomicInteger
as a float
after a widening primitive conversion, with memory effects as specified by VarHandle.getVolatile(java.lang.Object...)
.floatValue
in class Number
float
.public double doubleValue()
AtomicInteger
as a double
after a widening primitive conversion, with memory effects as specified by VarHandle.getVolatile(java.lang.Object...)
.doubleValue
in class Number
double
.public final int getPlain()
volatile
.public final void setPlain(int newValue)
newValue
, with memory semantics of setting as if the variable was declared non-volatile
and non-final
.newValue
- the new valuepublic final int getOpaque()
VarHandle.getOpaque(java.lang.Object...)
.public final void setOpaque(int newValue)
newValue
, with memory effects as specified by VarHandle.setOpaque(java.lang.Object...)
.newValue
- the new valuepublic final int getAcquire()
VarHandle.getAcquire(java.lang.Object...)
.public final void setRelease(int newValue)
newValue
, with memory effects as specified by VarHandle.setRelease(java.lang.Object...)
.newValue
- the new valuepublic final int compareAndExchange(int expectedValue, int newValue)
newValue
if the current value, referred to as the witness value, == expectedValue
, with memory effects as specified by VarHandle.compareAndExchange(java.lang.Object...)
.expectedValue
- the expected valuenewValue
- the new valuepublic final int compareAndExchangeAcquire(int expectedValue, int newValue)
newValue
if the current value, referred to as the witness value, == expectedValue
, with memory effects as specified by VarHandle.compareAndExchangeAcquire(java.lang.Object...)
.expectedValue
- the expected valuenewValue
- the new valuepublic final int compareAndExchangeRelease(int expectedValue, int newValue)
newValue
if the current value, referred to as the witness value, == expectedValue
, with memory effects as specified by VarHandle.compareAndExchangeRelease(java.lang.Object...)
.expectedValue
- the expected valuenewValue
- the new valuepublic final boolean weakCompareAndSetVolatile(int expectedValue, int newValue)
newValue
if the current value == expectedValue
, with memory effects as specified by VarHandle.weakCompareAndSet(java.lang.Object...)
.expectedValue
- the expected valuenewValue
- the new valuetrue
if successfulpublic final boolean weakCompareAndSetAcquire(int expectedValue, int newValue)
newValue
if the current value == expectedValue
, with memory effects as specified by VarHandle.weakCompareAndSetAcquire(java.lang.Object...)
.expectedValue
- the expected valuenewValue
- the new valuetrue
if successfulpublic final boolean weakCompareAndSetRelease(int expectedValue, int newValue)
newValue
if the current value == expectedValue
, with memory effects as specified by VarHandle.weakCompareAndSetRelease(java.lang.Object...)
.expectedValue
- the expected valuenewValue
- the new valuetrue
if successful
© 1993, 2023, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/concurrent/atomic/AtomicInteger.html
compareAndExchange(int, int)
andcompareAndSet(int, int)
).