AtomicBoolean (Java SE 9 & JDK 9 ) (original) (raw)
- java.util.concurrent.atomic.AtomicBoolean
All Implemented Interfaces:
[Serializable](../../../../java/io/Serializable.html "interface in java.io")
public class AtomicBoolean
extends Object
implements Serializable
A boolean
value that may be updated atomically. See theVarHandle 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.
Since:
1.5
See Also:
Serialized Form
Constructor Summary
Constructors
Constructor Description AtomicBoolean() Creates a new AtomicBoolean with initial value false. AtomicBoolean(boolean initialValue) Creates a new AtomicBoolean with the given initial value. Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods
Modifier and Type Method Description 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 byVarHandle.compareAndExchange(java.lang.Object...). 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 byVarHandle.compareAndExchangeAcquire(java.lang.Object...). 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 byVarHandle.compareAndExchangeRelease(java.lang.Object...). 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(java.lang.Object...). boolean get() Returns the current value, with memory effects as specified by VarHandle.getVolatile(java.lang.Object...). boolean getAcquire() Returns the current value, with memory effects as specified by VarHandle.getAcquire(java.lang.Object...). boolean getAndSet(boolean newValue) Atomically sets the value to newValue and returns the old value, with memory effects as specified by VarHandle.getAndSet(java.lang.Object...). boolean getOpaque() Returns the current value, with memory effects as specified by VarHandle.getOpaque(java.lang.Object...). boolean getPlain() Returns the current value, with memory semantics of reading as if the variable was declared non-volatile. void lazySet(boolean newValue) Sets the value to newValue, with memory effects as specified by VarHandle.setRelease(java.lang.Object...). void set(boolean newValue) Sets the value to newValue, with memory effects as specified by VarHandle.setVolatile(java.lang.Object...). void setOpaque(boolean newValue) Sets the value to newValue, with memory effects as specified by VarHandle.setOpaque(java.lang.Object...). 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. void setRelease(boolean 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. boolean weakCompareAndSet(boolean expectedValue, boolean newValue) Deprecated. boolean weakCompareAndSetAcquire(boolean expectedValue, boolean newValue) Possibly atomically sets the value to newValue if the current value == expectedValue, with memory effects as specified byVarHandle.weakCompareAndSetAcquire(java.lang.Object...). 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(java.lang.Object...). boolean weakCompareAndSetRelease(boolean expectedValue, boolean newValue) Possibly atomically sets the value to newValue if the current value == expectedValue, with memory effects as specified byVarHandle.weakCompareAndSetRelease(java.lang.Object...). boolean weakCompareAndSetVolatile(boolean expectedValue, boolean newValue) Possibly atomically sets the value to newValue if the current value == expectedValue, with memory effects as specified byVarHandle.weakCompareAndSet(java.lang.Object...). * ### Methods inherited from class java.lang.[Object](../../../../java/lang/Object.html "class in java.lang") `[clone](../../../../java/lang/Object.html#clone--), [equals](../../../../java/lang/Object.html#equals-java.lang.Object-), [finalize](../../../../java/lang/Object.html#finalize--), [getClass](../../../../java/lang/Object.html#getClass--), [hashCode](../../../../java/lang/Object.html#hashCode--), [notify](../../../../java/lang/Object.html#notify--), [notifyAll](../../../../java/lang/Object.html#notifyAll--), [wait](../../../../java/lang/Object.html#wait--), [wait](../../../../java/lang/Object.html#wait-long-), [wait](../../../../java/lang/Object.html#wait-long-int-)`
Constructor Detail
* #### AtomicBoolean public AtomicBoolean(boolean initialValue) Creates a new `AtomicBoolean` with the given initial value. Parameters: `initialValue` \- the initial value * #### AtomicBoolean public AtomicBoolean() Creates a new `AtomicBoolean` with initial value `false`.
Method Detail
* #### get public final boolean get() Returns: the current value * #### compareAndSet public final boolean compareAndSet(boolean expectedValue, boolean newValue) Parameters: `expectedValue` \- the expected value `newValue` \- the new value Returns: `true` if successful. False return indicates that the actual value was not equal to the expected value. * #### weakCompareAndSet [@Deprecated](../../../../java/lang/Deprecated.html "annotation in java.lang")([since](../../../../java/lang/Deprecated.html#since--)="9") public boolean weakCompareAndSet(boolean expectedValue, boolean newValue) Parameters: `expectedValue` \- the expected value `newValue` \- the new value Returns: `true` if successful See Also: [weakCompareAndSetPlain(boolean, boolean)](../../../../java/util/concurrent/atomic/AtomicBoolean.html#weakCompareAndSetPlain-boolean-boolean-) * #### weakCompareAndSetPlain public boolean weakCompareAndSetPlain(boolean expectedValue, boolean newValue) Parameters: `expectedValue` \- the expected value `newValue` \- the new value Returns: `true` if successful Since: 9 * #### set public final void set(boolean newValue) Parameters: `newValue` \- the new value * #### lazySet public final void lazySet(boolean newValue) Parameters: `newValue` \- the new value Since: 1.6 * #### getAndSet public final boolean getAndSet(boolean newValue) Parameters: `newValue` \- the new value Returns: the previous value * #### toString public [String](../../../../java/lang/String.html "class in java.lang") toString() Returns the String representation of the current value. Overrides: `[toString](../../../../java/lang/Object.html#toString--)` in class `[Object](../../../../java/lang/Object.html "class in java.lang")` Returns: the String representation of the current value * #### getPlain public 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 * #### 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` \- the new value Since: 9 * #### getOpaque public final boolean getOpaque() Returns: the value Since: 9 * #### setOpaque public final void setOpaque(boolean newValue) Parameters: `newValue` \- the new value Since: 9 * #### getAcquire public final boolean getAcquire() Returns: the value Since: 9 * #### setRelease public final void setRelease(boolean newValue) Parameters: `newValue` \- the new value Since: 9 * #### 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(java.lang.Object...)](../../../../java/lang/invoke/VarHandle.html#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 * #### compareAndExchangeAcquire public final boolean compareAndExchangeAcquire(boolean expectedValue, boolean newValue) 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 * #### compareAndExchangeRelease public final boolean compareAndExchangeRelease(boolean expectedValue, boolean newValue) 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 * #### weakCompareAndSetVolatile public final boolean weakCompareAndSetVolatile(boolean expectedValue, boolean newValue) Parameters: `expectedValue` \- the expected value `newValue` \- the new value Returns: `true` if successful Since: 9 * #### weakCompareAndSetAcquire public final boolean weakCompareAndSetAcquire(boolean expectedValue, boolean newValue) Parameters: `expectedValue` \- the expected value `newValue` \- the new value Returns: `true` if successful Since: 9 * #### weakCompareAndSetRelease public final boolean weakCompareAndSetRelease(boolean expectedValue, boolean newValue) Parameters: `expectedValue` \- the expected value `newValue` \- the new value Returns: `true` if successful Since: 9
Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2017, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.