- java.lang.Object
- 
- java.util.concurrent.atomic.AtomicBoolean
 
- 
- All Implemented Interfaces:
- Serializable
 
 public class AtomicBoolean extends Object implements Serializable Abooleanvalue that may be updated atomically. See theVarHandlespecification for descriptions of the properties of atomic accesses. AnAtomicBooleanis used in applications such as atomically updated flags, and cannot be used as a replacement for aBoolean.- Since:
- 1.5
- See Also:
- Serialized Form
 
- 
- 
Constructor SummaryConstructors Constructor Description AtomicBoolean()Creates a newAtomicBooleanwith initial valuefalse.AtomicBoolean(boolean initialValue)Creates a newAtomicBooleanwith the given initial value.
 - 
Method SummaryAll Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description booleancompareAndExchange(boolean expectedValue, boolean newValue)Atomically sets the value tonewValueif the current value, referred to as the witness value,== expectedValue, with memory effects as specified byVarHandle.compareAndExchange(java.lang.Object...).booleancompareAndExchangeAcquire(boolean expectedValue, boolean newValue)Atomically sets the value tonewValueif the current value, referred to as the witness value,== expectedValue, with memory effects as specified byVarHandle.compareAndExchangeAcquire(java.lang.Object...).booleancompareAndExchangeRelease(boolean expectedValue, boolean newValue)Atomically sets the value tonewValueif the current value, referred to as the witness value,== expectedValue, with memory effects as specified byVarHandle.compareAndExchangeRelease(java.lang.Object...).booleancompareAndSet(boolean expectedValue, boolean newValue)Atomically sets the value tonewValueif the current value== expectedValue, with memory effects as specified byVarHandle.compareAndSet(java.lang.Object...).booleanget()Returns the current value, with memory effects as specified byVarHandle.getVolatile(java.lang.Object...).booleangetAcquire()Returns the current value, with memory effects as specified byVarHandle.getAcquire(java.lang.Object...).booleangetAndSet(boolean newValue)Atomically sets the value tonewValueand returns the old value, with memory effects as specified byVarHandle.getAndSet(java.lang.Object...).booleangetOpaque()Returns the current value, with memory effects as specified byVarHandle.getOpaque(java.lang.Object...).booleangetPlain()Returns the current value, with memory semantics of reading as if the variable was declared non-volatile.voidlazySet(boolean newValue)Sets the value tonewValue, with memory effects as specified byVarHandle.setRelease(java.lang.Object...).voidset(boolean newValue)Sets the value tonewValue, with memory effects as specified byVarHandle.setVolatile(java.lang.Object...).voidsetOpaque(boolean newValue)Sets the value tonewValue, with memory effects as specified byVarHandle.setOpaque(java.lang.Object...).voidsetPlain(boolean newValue)Sets the value tonewValue, with memory semantics of setting as if the variable was declared non-volatileand non-final.voidsetRelease(boolean newValue)Sets the value tonewValue, with memory effects as specified byVarHandle.setRelease(java.lang.Object...).StringtoString()Returns the String representation of the current value.booleanweakCompareAndSet(boolean expectedValue, boolean newValue)Deprecated.This method has plain memory effects but the method name implies volatile memory effects (see methods such ascompareAndExchange(boolean, boolean)andcompareAndSet(boolean, boolean)).booleanweakCompareAndSetAcquire(boolean expectedValue, boolean newValue)Possibly atomically sets the value tonewValueif the current value== expectedValue, with memory effects as specified byVarHandle.weakCompareAndSetAcquire(java.lang.Object...).booleanweakCompareAndSetPlain(boolean expectedValue, boolean newValue)Possibly atomically sets the value tonewValueif the current value== expectedValue, with memory effects as specified byVarHandle.weakCompareAndSetPlain(java.lang.Object...).booleanweakCompareAndSetRelease(boolean expectedValue, boolean newValue)Possibly atomically sets the value tonewValueif the current value== expectedValue, with memory effects as specified byVarHandle.weakCompareAndSetRelease(java.lang.Object...).booleanweakCompareAndSetVolatile(boolean expectedValue, boolean newValue)Possibly atomically sets the value tonewValueif the current value== expectedValue, with memory effects as specified byVarHandle.weakCompareAndSet(java.lang.Object...).
 
- 
- 
- 
Method Detail- 
getpublic final boolean get() Returns the current value, with memory effects as specified byVarHandle.getVolatile(java.lang.Object...).- Returns:
- the current value
 
 - 
compareAndSetpublic final boolean compareAndSet(boolean expectedValue, boolean newValue)Atomically sets the value tonewValueif the current value== expectedValue, with memory effects as specified byVarHandle.compareAndSet(java.lang.Object...).- Parameters:
- expectedValue- the expected value
- newValue- the new value
- Returns:
- trueif successful. False return indicates that the actual value was not equal to the expected value.
 
 - 
weakCompareAndSet@Deprecated(since="9") public boolean weakCompareAndSet(boolean expectedValue, boolean newValue) Deprecated.This method has plain memory effects but the method name implies volatile memory effects (see methods such ascompareAndExchange(boolean, boolean)andcompareAndSet(boolean, boolean)). To avoid confusion over plain or volatile memory effects it is recommended that the methodweakCompareAndSetPlain(boolean, boolean)be used instead.Possibly atomically sets the value tonewValueif the current value== expectedValue, with memory effects as specified byVarHandle.weakCompareAndSetPlain(java.lang.Object...).- Parameters:
- expectedValue- the expected value
- newValue- the new value
- Returns:
- trueif successful
- See Also:
- weakCompareAndSetPlain(boolean, boolean)
 
 - 
weakCompareAndSetPlainpublic boolean weakCompareAndSetPlain(boolean expectedValue, boolean newValue)Possibly atomically sets the value tonewValueif the current value== expectedValue, with memory effects as specified byVarHandle.weakCompareAndSetPlain(java.lang.Object...).- Parameters:
- expectedValue- the expected value
- newValue- the new value
- Returns:
- trueif successful
- Since:
- 9
 
 - 
setpublic final void set(boolean newValue) Sets the value tonewValue, with memory effects as specified byVarHandle.setVolatile(java.lang.Object...).- Parameters:
- newValue- the new value
 
 - 
lazySetpublic final void lazySet(boolean newValue) Sets the value tonewValue, with memory effects as specified byVarHandle.setRelease(java.lang.Object...).- Parameters:
- newValue- the new value
- Since:
- 1.6
 
 - 
getAndSetpublic final boolean getAndSet(boolean newValue) Atomically sets the value tonewValueand returns the old value, with memory effects as specified byVarHandle.getAndSet(java.lang.Object...).- Parameters:
- newValue- the new value
- Returns:
- the previous value
 
 - 
toStringpublic String toString() Returns the String representation of the current value.
 - 
getPlainpublic final boolean getPlain() Returns the current value, with memory semantics of reading as if the variable was declared non-volatile.- Returns:
- the value
- Since:
- 9
 
 - 
setPlainpublic final void setPlain(boolean newValue) Sets the value tonewValue, with memory semantics of setting as if the variable was declared non-volatileand non-final.- Parameters:
- newValue- the new value
- Since:
- 9
 
 - 
getOpaquepublic final boolean getOpaque() Returns the current value, with memory effects as specified byVarHandle.getOpaque(java.lang.Object...).- Returns:
- the value
- Since:
- 9
 
 - 
setOpaquepublic final void setOpaque(boolean newValue) Sets the value tonewValue, with memory effects as specified byVarHandle.setOpaque(java.lang.Object...).- Parameters:
- newValue- the new value
- Since:
- 9
 
 - 
getAcquirepublic final boolean getAcquire() Returns the current value, with memory effects as specified byVarHandle.getAcquire(java.lang.Object...).- Returns:
- the value
- Since:
- 9
 
 - 
setReleasepublic final void setRelease(boolean newValue) Sets the value tonewValue, with memory effects as specified byVarHandle.setRelease(java.lang.Object...).- Parameters:
- newValue- the new value
- Since:
- 9
 
 - 
compareAndExchangepublic final boolean compareAndExchange(boolean expectedValue, boolean newValue)Atomically sets the value tonewValueif the current value, referred to as the witness value,== expectedValue, with memory effects as specified byVarHandle.compareAndExchange(java.lang.Object...).- Parameters:
- expectedValue- the expected value
- newValue- the new value
- Returns:
- the witness value, which will be the same as the expected value if successful
- Since:
- 9
 
 - 
compareAndExchangeAcquirepublic final boolean compareAndExchangeAcquire(boolean expectedValue, boolean newValue)Atomically sets the value tonewValueif the current value, referred to as the witness value,== expectedValue, with memory effects as specified byVarHandle.compareAndExchangeAcquire(java.lang.Object...).- Parameters:
- expectedValue- the expected value
- newValue- the new value
- Returns:
- the witness value, which will be the same as the expected value if successful
- Since:
- 9
 
 - 
compareAndExchangeReleasepublic final boolean compareAndExchangeRelease(boolean expectedValue, boolean newValue)Atomically sets the value tonewValueif the current value, referred to as the witness value,== expectedValue, with memory effects as specified byVarHandle.compareAndExchangeRelease(java.lang.Object...).- Parameters:
- expectedValue- the expected value
- newValue- the new value
- Returns:
- the witness value, which will be the same as the expected value if successful
- Since:
- 9
 
 - 
weakCompareAndSetVolatilepublic final boolean weakCompareAndSetVolatile(boolean expectedValue, boolean newValue)Possibly atomically sets the value tonewValueif the current value== expectedValue, with memory effects as specified byVarHandle.weakCompareAndSet(java.lang.Object...).- Parameters:
- expectedValue- the expected value
- newValue- the new value
- Returns:
- trueif successful
- Since:
- 9
 
 - 
weakCompareAndSetAcquirepublic final boolean weakCompareAndSetAcquire(boolean expectedValue, boolean newValue)Possibly atomically sets the value tonewValueif the current value== expectedValue, with memory effects as specified byVarHandle.weakCompareAndSetAcquire(java.lang.Object...).- Parameters:
- expectedValue- the expected value
- newValue- the new value
- Returns:
- trueif successful
- Since:
- 9
 
 - 
weakCompareAndSetReleasepublic final boolean weakCompareAndSetRelease(boolean expectedValue, boolean newValue)Possibly atomically sets the value tonewValueif the current value== expectedValue, with memory effects as specified byVarHandle.weakCompareAndSetRelease(java.lang.Object...).- Parameters:
- expectedValue- the expected value
- newValue- the new value
- Returns:
- trueif successful
- Since:
- 9
 
 
- 
 
-