Serializable
public class AtomicLong extends Number implements Serializable
long
value that may be updated atomically. See the
VarHandle
specification for descriptions of the properties
of atomic accesses. An AtomicLong
is used in applications
such as atomically incremented sequence numbers, and cannot be used
as a replacement for a Long
. However, this class
does extend Number
to allow uniform access by tools and
utilities that deal with numerically-based classes.Constructor | Description |
---|---|
AtomicLong() |
Creates a new AtomicLong with initial value
0 . |
AtomicLong(long initialValue) |
Creates a new AtomicLong with the given initial value.
|
Modifier and Type | Method | Description |
---|---|---|
long |
accumulateAndGet(long x,
LongBinaryOperator accumulatorFunction) |
Atomically updates the current value with the results of
applying the given function to the current and given values,
returning the updated value.
|
long |
addAndGet(long delta) |
Atomically adds the given value to the current value,
with memory effects as specified by
VarHandle.getAndAdd(java.lang.Object...) . |
long |
compareAndExchange(long expectedValue,
long 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...) . |
long |
compareAndExchangeAcquire(long expectedValue,
long 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...) . |
long |
compareAndExchangeRelease(long expectedValue,
long 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(long expectedValue,
long newValue) |
Atomically sets the value to
newValue
if the current value == expectedValue ,
with memory effects as specified by VarHandle.compareAndSet(java.lang.Object...) . |
long |
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
AtomicLong 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
AtomicLong as a float
after a widening primitive conversion,
with memory effects as specified by VarHandle.getVolatile(java.lang.Object...) . |
long |
get() |
Returns the current value,
with memory effects as specified by
VarHandle.getVolatile(java.lang.Object...) . |
long |
getAcquire() |
Returns the current value,
with memory effects as specified by
VarHandle.getAcquire(java.lang.Object...) . |
long |
getAndAccumulate(long x,
LongBinaryOperator accumulatorFunction) |
Atomically updates the current value with the results of
applying the given function to the current and given values,
returning the previous value.
|
long |
getAndAdd(long delta) |
Atomically adds the given value to the current value,
with memory effects as specified by
VarHandle.getAndAdd(java.lang.Object...) . |
long |
getAndDecrement() |
Atomically decrements the current value,
with memory effects as specified by
VarHandle.getAndAdd(java.lang.Object...) . |
long |
getAndIncrement() |
Atomically increments the current value,
with memory effects as specified by
VarHandle.getAndAdd(java.lang.Object...) . |
long |
getAndSet(long newValue) |
Atomically sets the value to
newValue and returns the old value,
with memory effects as specified by VarHandle.getAndSet(java.lang.Object...) . |
long |
getAndUpdate(LongUnaryOperator updateFunction) |
Atomically updates the current value with the results of
applying the given function, returning the previous value.
|
long |
getOpaque() |
Returns the current value,
with memory effects as specified by
VarHandle.getOpaque(java.lang.Object...) . |
long |
getPlain() |
Returns the current value, with memory semantics of reading as if the
variable was declared non-
volatile . |
long |
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
AtomicLong as an int
after a narrowing primitive conversion,
with memory effects as specified by VarHandle.getVolatile(java.lang.Object...) . |
void |
lazySet(long 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
AtomicLong as a long ,
with memory effects as specified by VarHandle.getVolatile(java.lang.Object...) . |
void |
set(long newValue) |
Sets the value to
newValue ,
with memory effects as specified by VarHandle.setVolatile(java.lang.Object...) . |
void |
setOpaque(long newValue) |
Sets the value to
newValue ,
with memory effects as specified by VarHandle.setOpaque(java.lang.Object...) . |
void |
setPlain(long newValue) |
Sets the value to
newValue , with memory semantics
of setting as if the variable was declared non-volatile
and non-final . |
void |
setRelease(long 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.
|
long |
updateAndGet(LongUnaryOperator updateFunction) |
Atomically updates the current value with the results of
applying the given function, returning the updated value.
|
boolean |
weakCompareAndSet(long expectedValue,
long newValue) |
Deprecated.
This method has plain memory effects but the method
name implies volatile memory effects (see methods such as
compareAndExchange(long, long) and compareAndSet(long, long) ). To avoid
confusion over plain or volatile memory effects it is recommended that
the method weakCompareAndSetPlain(long, long) be used instead. |
boolean |
weakCompareAndSetAcquire(long expectedValue,
long 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(long expectedValue,
long 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(long expectedValue,
long 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(long expectedValue,
long 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 AtomicLong(long initialValue)
initialValue
- the initial valuepublic AtomicLong()
0
.public final long get()
VarHandle.getVolatile(java.lang.Object...)
.public final void set(long newValue)
newValue
,
with memory effects as specified by VarHandle.setVolatile(java.lang.Object...)
.newValue
- the new valuepublic final void lazySet(long newValue)
newValue
,
with memory effects as specified by VarHandle.setRelease(java.lang.Object...)
.newValue
- the new valuepublic final long getAndSet(long 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(long expectedValue, long 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(long expectedValue, long newValue)
compareAndExchange(long, long)
and compareAndSet(long, long)
). To avoid
confusion over plain or volatile memory effects it is recommended that
the method weakCompareAndSetPlain(long, long)
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(long, long)
public final boolean weakCompareAndSetPlain(long expectedValue, long 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 long getAndIncrement()
VarHandle.getAndAdd(java.lang.Object...)
.
Equivalent to getAndAdd(1)
.
public final long getAndDecrement()
VarHandle.getAndAdd(java.lang.Object...)
.
Equivalent to getAndAdd(-1)
.
public final long getAndAdd(long delta)
VarHandle.getAndAdd(java.lang.Object...)
.delta
- the value to addpublic final long incrementAndGet()
VarHandle.getAndAdd(java.lang.Object...)
.
Equivalent to addAndGet(1)
.
public final long decrementAndGet()
VarHandle.getAndAdd(java.lang.Object...)
.
Equivalent to addAndGet(-1)
.
public final long addAndGet(long delta)
VarHandle.getAndAdd(java.lang.Object...)
.delta
- the value to addpublic final long getAndUpdate(LongUnaryOperator updateFunction)
updateFunction
- a side-effect-free functionpublic final long updateAndGet(LongUnaryOperator updateFunction)
updateFunction
- a side-effect-free functionpublic final long getAndAccumulate(long x, LongBinaryOperator accumulatorFunction)
x
- the update valueaccumulatorFunction
- a side-effect-free function of two argumentspublic final long accumulateAndGet(long x, LongBinaryOperator accumulatorFunction)
x
- the update valueaccumulatorFunction
- a side-effect-free function of two argumentspublic String toString()
public int intValue()
AtomicLong
as an int
after a narrowing primitive conversion,
with memory effects as specified by VarHandle.getVolatile(java.lang.Object...)
.public long longValue()
AtomicLong
as a long
,
with memory effects as specified by VarHandle.getVolatile(java.lang.Object...)
.
Equivalent to get()
.public float floatValue()
AtomicLong
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()
AtomicLong
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 long getPlain()
volatile
.public final void setPlain(long newValue)
newValue
, with memory semantics
of setting as if the variable was declared non-volatile
and non-final
.newValue
- the new valuepublic final long getOpaque()
VarHandle.getOpaque(java.lang.Object...)
.public final void setOpaque(long newValue)
newValue
,
with memory effects as specified by VarHandle.setOpaque(java.lang.Object...)
.newValue
- the new valuepublic final long getAcquire()
VarHandle.getAcquire(java.lang.Object...)
.public final void setRelease(long newValue)
newValue
,
with memory effects as specified by VarHandle.setRelease(java.lang.Object...)
.newValue
- the new valuepublic final long compareAndExchange(long expectedValue, long 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 long compareAndExchangeAcquire(long expectedValue, long 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 long compareAndExchangeRelease(long expectedValue, long 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(long expectedValue, long 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(long expectedValue, long 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(long expectedValue, long 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