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(int initialValue) |
Creates a new AtomicInteger with the given initial value.
|
Modifier and Type | Method | Description |
---|---|---|
int |
accumulateAndGet(int x,
IntBinaryOperator accumulatorFunction) |
Atomically updates 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 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 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 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) ). To avoid
confusion over plain or volatile memory effects it is recommended that
the method weakCompareAndSetPlain(int, int) be used instead. |
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...) . |
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 successfulweakCompareAndSetPlain(int, int)
public 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)
updateFunction
- a side-effect-free functionpublic final int updateAndGet(IntUnaryOperator updateFunction)
updateFunction
- a side-effect-free functionpublic final int getAndAccumulate(int x, IntBinaryOperator accumulatorFunction)
x
- the update valueaccumulatorFunction
- a side-effect-free function of two argumentspublic final int accumulateAndGet(int x, IntBinaryOperator accumulatorFunction)
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...)
.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 Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2017, Oracle and/or its affiliates. 500 Oracle Parkway
Redwood Shores, CA 94065 USA. All rights reserved.
DRAFT 9-internal+0-adhoc.mlchung.jdk9-jdeps