AtomicBoolean
public
class
AtomicBoolean
extends Object
implements
Serializable
java.lang.Object | |
↳ | java.util.concurrent.atomic.AtomicBoolean |
A 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
.
Summary
Public constructors | |
---|---|
AtomicBoolean(boolean initialValue)
Creates a new |
|
AtomicBoolean()
Creates a new |
Public methods | |
---|---|
final
boolean
|
compareAndExchange(boolean expectedValue, boolean newValue)
Atomically sets the value to |
final
boolean
|
compareAndExchangeAcquire(boolean expectedValue, boolean newValue)
Atomically sets the value to |
final
boolean
|
compareAndExchangeRelease(boolean expectedValue, boolean newValue)
Atomically sets the value to |
final
boolean
|
compareAndSet(boolean expectedValue, boolean newValue)
Atomically sets the value to |
final
boolean
|
get()
Returns the current value,
with memory effects as specified by |
final
boolean
|
getAcquire()
Returns the current value,
with memory effects as specified by |
final
boolean
|
getAndSet(boolean newValue)
Atomically sets the value to |
final
boolean
|
getOpaque()
Returns the current value,
with memory effects as specified by |
final
boolean
|
getPlain()
Returns the current value, with memory semantics of reading as
if the variable was declared non- |
final
void
|
lazySet(boolean newValue)
Sets the value to |
final
void
|
set(boolean newValue)
Sets the value to |
final
void
|
setOpaque(boolean newValue)
Sets the value to |
final
void
|
setPlain(boolean newValue)
Sets the value to |
final
void
|
setRelease(boolean newValue)
Sets the value to |
String
|
toString()
Returns the String representation of the current value. |
boolean
|
weakCompareAndSet(boolean expectedValue, boolean newValue)
This method was deprecated
in API level 33.
This method has plain memory effects but the method
name implies volatile memory effects (see methods such as
|
final
boolean
|
weakCompareAndSetAcquire(boolean expectedValue, boolean newValue)
Possibly atomically sets the value to |
boolean
|
weakCompareAndSetPlain(boolean expectedValue, boolean newValue)
Possibly atomically sets the value to |
final
boolean
|
weakCompareAndSetRelease(boolean expectedValue, boolean newValue)
Possibly atomically sets the value to |
final
boolean
|
weakCompareAndSetVolatile(boolean expectedValue, boolean newValue)
Possibly atomically sets the value to |
Inherited methods | |
---|---|
Public constructors
AtomicBoolean
public AtomicBoolean (boolean initialValue)
Creates a new AtomicBoolean
with the given initial value.
Parameters | |
---|---|
initialValue |
boolean : the initial value |
AtomicBoolean
public AtomicBoolean ()
Creates a new AtomicBoolean
with initial value false
.
Public methods
compareAndExchange
public final boolean compareAndExchange (boolean expectedValue, boolean 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 |
boolean : the expected value |
newValue |
boolean : the new value |
Returns | |
---|---|
boolean |
the witness value, which will be the same as the expected value if successful |
compareAndExchangeAcquire
public final boolean compareAndExchangeAcquire (boolean expectedValue, boolean 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 |
boolean : the expected value |
newValue |
boolean : the new value |
Returns | |
---|---|
boolean |
the witness value, which will be the same as the expected value if successful |
compareAndExchangeRelease
public final boolean compareAndExchangeRelease (boolean expectedValue, boolean 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 |
boolean : the expected value |
newValue |
boolean : the new value |
Returns | |
---|---|
boolean |
the witness value, which will be the same as the expected value if successful |
compareAndSet
public final boolean compareAndSet (boolean expectedValue, boolean newValue)
Atomically sets the value to newValue
if the current value == expectedValue
,
with memory effects as specified by VarHandle#compareAndSet
.
Parameters | |
---|---|
expectedValue |
boolean : the expected value |
newValue |
boolean : the new value |
Returns | |
---|---|
boolean |
true if successful. False return indicates that
the actual value was not equal to the expected value. |
get
public final boolean get ()
Returns the current value,
with memory effects as specified by VarHandle#getVolatile
.
Returns | |
---|---|
boolean |
the current value |
getAcquire
public final boolean getAcquire ()
Returns the current value,
with memory effects as specified by VarHandle#getAcquire
.
Returns | |
---|---|
boolean |
the value |
getAndSet
public final boolean getAndSet (boolean newValue)
Atomically sets the value to newValue
and returns the old value,
with memory effects as specified by VarHandle#getAndSet
.
Parameters | |
---|---|
newValue |
boolean : the new value |
Returns | |
---|---|
boolean |
the previous value |
getOpaque
public final boolean getOpaque ()
Returns the current value,
with memory effects as specified by VarHandle#getOpaque
.
Returns | |
---|---|
boolean |
the value |
getPlain
public final boolean getPlain ()
Returns the current value, with memory semantics of reading as
if the variable was declared non-volatile
.
Returns | |
---|---|
boolean |
the value |
lazySet
public final void lazySet (boolean newValue)
Sets the value to newValue
,
with memory effects as specified by VarHandle#setRelease
.
Parameters | |
---|---|
newValue |
boolean : the new value |
set
public final void set (boolean newValue)
Sets the value to newValue
,
with memory effects as specified by VarHandle#setVolatile
.
Parameters | |
---|---|
newValue |
boolean : the new value |
setOpaque
public final void setOpaque (boolean newValue)
Sets the value to newValue
,
with memory effects as specified by VarHandle#setOpaque
.
Parameters | |
---|---|
newValue |
boolean : the new value |
setPlain
public final void setPlain (boolean newValue)
Sets the value to newValue
, with memory semantics
of setting as if the variable was declared non-volatile
and non-final
.
Parameters | |
---|---|
newValue |
boolean : the new value |
setRelease
public final void setRelease (boolean newValue)
Sets the value to newValue
,
with memory effects as specified by VarHandle#setRelease
.
Parameters | |
---|---|
newValue |
boolean : the new value |
toString
public String toString ()
Returns the String representation of the current value.
Returns | |
---|---|
String |
the String representation of the current value |
weakCompareAndSet
public boolean weakCompareAndSet (boolean expectedValue, boolean newValue)
This method was deprecated
in API level 33.
This method has plain memory effects but the method
name implies volatile memory effects (see methods such as
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.
Possibly atomically sets the value to newValue
if the current value == expectedValue
,
with memory effects as specified by VarHandle#weakCompareAndSetPlain
.
Parameters | |
---|---|
expectedValue |
boolean : the expected value |
newValue |
boolean : the new value |
Returns | |
---|---|
boolean |
true if successful |
See also:
weakCompareAndSetAcquire
public final boolean weakCompareAndSetAcquire (boolean expectedValue, boolean newValue)
Possibly atomically sets the value to newValue
if the current
value == expectedValue
,
with memory effects as specified by
VarHandle#weakCompareAndSetAcquire
.
Parameters | |
---|---|
expectedValue |
boolean : the expected value |
newValue |
boolean : the new value |
Returns | |
---|---|
boolean |
true if successful |
weakCompareAndSetPlain
public boolean weakCompareAndSetPlain (boolean expectedValue, boolean newValue)
Possibly atomically sets the value to newValue
if the current value == expectedValue
,
with memory effects as specified by VarHandle#weakCompareAndSetPlain
.
Parameters | |
---|---|
expectedValue |
boolean : the expected value |
newValue |
boolean : the new value |
Returns | |
---|---|
boolean |
true if successful |
weakCompareAndSetRelease
public final boolean weakCompareAndSetRelease (boolean expectedValue, boolean newValue)
Possibly atomically sets the value to newValue
if the current
value == expectedValue
,
with memory effects as specified by
VarHandle#weakCompareAndSetRelease
.
Parameters | |
---|---|
expectedValue |
boolean : the expected value |
newValue |
boolean : the new value |
Returns | |
---|---|
boolean |
true if successful |
weakCompareAndSetVolatile
public final boolean weakCompareAndSetVolatile (boolean expectedValue, boolean newValue)
Possibly atomically sets the value to newValue
if the current
value == expectedValue
,
with memory effects as specified by
VarHandle#weakCompareAndSet
.
Parameters | |
---|---|
expectedValue |
boolean : the expected value |
newValue |
boolean : the new value |
Returns | |
---|---|
boolean |
true if successful |