Added in API level 1

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 with the given initial value.

AtomicBoolean()

Creates a new AtomicBoolean with initial value false.

Public methods

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.

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.

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.

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.

final boolean get()

Returns the current value, with memory effects as specified by VarHandle#getVolatile.

final boolean getAcquire()

Returns the current value, with memory effects as specified by VarHandle#getAcquire.

final boolean getAndSet(boolean newValue)

Atomically sets the value to newValue and returns the old value, with memory effects as specified by VarHandle#getAndSet.

final boolean getOpaque()

Returns the current value, with memory effects as specified by VarHandle#getOpaque.

final boolean getPlain()

Returns the current value, with memory semantics of reading as if the variable was declared non-volatile.

final void lazySet(boolean newValue)

Sets the value to newValue, with memory effects as specified by VarHandle#setRelease.

final void set(boolean newValue)

Sets the value to newValue, with memory effects as specified by VarHandle#setVolatile.

final void setOpaque(boolean newValue)

Sets the value to newValue, with memory effects as specified by VarHandle#setOpaque.

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.

final void setRelease(boolean newValue)

Sets the value to newValue, with memory effects as specified by VarHandle#setRelease.

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 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.

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.

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.

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.

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.

Inherited methods

Public constructors

AtomicBoolean

Added in API level 1
public AtomicBoolean (boolean initialValue)

Creates a new AtomicBoolean with the given initial value.

Parameters
initialValue boolean: the initial value

AtomicBoolean

Added in API level 1
public AtomicBoolean ()

Creates a new AtomicBoolean with initial value false.

Public methods

compareAndExchange

Added in API level 33
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

Added in API level 33
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

Added in API level 33
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

Added in API level 1
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

Added in API level 1
public final boolean get ()

Returns the current value, with memory effects as specified by VarHandle#getVolatile.

Returns
boolean the current value

getAcquire

Added in API level 33
public final boolean getAcquire ()

Returns the current value, with memory effects as specified by VarHandle#getAcquire.

Returns
boolean the value

getAndSet

Added in API level 1
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

Added in API level 33
public final boolean getOpaque ()

Returns the current value, with memory effects as specified by VarHandle#getOpaque.

Returns
boolean the value

getPlain

Added in API level 33
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

Added in API level 9
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

Added in API level 1
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

Added in API level 33
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

Added in API level 33
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

Added in API level 33
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

Added in API level 1
public String toString ()

Returns the String representation of the current value.

Returns
String the String representation of the current value

weakCompareAndSet

Added in API level 1
Deprecated in API level 33
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

weakCompareAndSetAcquire

Added in API level 33
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

Added in API level 33
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

Added in API level 33
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

Added in API level 33
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