Serializable
public class AtomicBoolean extends Object implements Serializable
boolean
value that may be updated atomically. See the VarHandle
specification for descriptions of the properties of atomic accesses. An AtomicBoolean
is used in applications such as atomically updated flags, and cannot be used as a replacement for a Boolean
.Constructor | Description |
---|---|
AtomicBoolean() |
Creates a new AtomicBoolean with initial value false . |
AtomicBoolean |
Creates a new AtomicBoolean with the given initial value. |
Modifier and Type | Method | Description |
---|---|---|
final boolean |
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 boolean |
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 boolean |
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 boolean |
get() |
Returns the current value, with memory effects as specified by VarHandle.getVolatile(java.lang.Object...) . |
final boolean |
getAcquire() |
Returns the current value, with memory effects as specified by VarHandle.getAcquire(java.lang.Object...) . |
final boolean |
getAndSet |
Atomically sets the value to newValue and returns the old value, with memory effects as specified by VarHandle.getAndSet(java.lang.Object...) . |
final boolean |
getOpaque() |
Returns the current value, with memory effects as specified by VarHandle.getOpaque(java.lang.Object...) . |
final boolean |
getPlain() |
Returns the current value, with memory semantics of reading as if the variable was declared non- volatile . |
final void |
lazySet |
Sets the value to newValue , with memory effects as specified by VarHandle.setRelease(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. |
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...) . |
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...) . |
public AtomicBoolean(boolean initialValue)
AtomicBoolean
with the given initial value.initialValue
- the initial valuepublic AtomicBoolean()
AtomicBoolean
with initial value false
.public final boolean get()
VarHandle.getVolatile(java.lang.Object...)
.public final boolean compareAndSet(boolean expectedValue, boolean 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 boolean weakCompareAndSet(boolean expectedValue, boolean newValue)
compareAndExchange(boolean, boolean)
and compareAndSet(boolean, boolean)
). To avoid confusion over plain or volatile memory effects it is recommended that the method weakCompareAndSetPlain(boolean, boolean)
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 boolean weakCompareAndSetPlain(boolean expectedValue, boolean 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 void set(boolean newValue)
newValue
, with memory effects as specified by VarHandle.setVolatile(java.lang.Object...)
.newValue
- the new valuepublic final void lazySet(boolean newValue)
newValue
, with memory effects as specified by VarHandle.setRelease(java.lang.Object...)
.newValue
- the new valuepublic final boolean getAndSet(boolean newValue)
newValue
and returns the old value, with memory effects as specified by VarHandle.getAndSet(java.lang.Object...)
.newValue
- the new valuepublic String toString()
public final boolean getPlain()
volatile
.public final void setPlain(boolean newValue)
newValue
, with memory semantics of setting as if the variable was declared non-volatile
and non-final
.newValue
- the new valuepublic final boolean getOpaque()
VarHandle.getOpaque(java.lang.Object...)
.public final void setOpaque(boolean newValue)
newValue
, with memory effects as specified by VarHandle.setOpaque(java.lang.Object...)
.newValue
- the new valuepublic final boolean getAcquire()
VarHandle.getAcquire(java.lang.Object...)
.public final void setRelease(boolean newValue)
newValue
, with memory effects as specified by VarHandle.setRelease(java.lang.Object...)
.newValue
- the new valuepublic final boolean compareAndExchange(boolean expectedValue, boolean 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 boolean compareAndExchangeAcquire(boolean expectedValue, boolean 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 boolean compareAndExchangeRelease(boolean expectedValue, boolean 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(boolean expectedValue, boolean 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(boolean expectedValue, boolean 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(boolean expectedValue, boolean 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/AtomicBoolean.html
compareAndExchange(boolean, boolean)
andcompareAndSet(boolean, boolean)
).