AtomicBoolean
Stay organized with collections
Save and categorize content based on your preferences.
A boolean
value that may be updated atomically. See the
java.util.concurrent.atomic
package specification for
description of the properties of atomic variables. An
AtomicBoolean
is used in applications such as atomically
updated flags, and cannot be used as a replacement for a
Boolean
.
Public Constructor Summary
|
AtomicBoolean(boolean initialValue)
Creates a new AtomicBoolean with the given initial value.
|
|
AtomicBoolean()
Creates a new AtomicBoolean with initial value false .
|
Public Method Summary
final
boolean
|
compareAndSet(boolean expect, boolean update)
Atomically sets the value to the given updated value
if the current value == the expected value.
|
final
boolean
|
get()
Returns the current value.
|
final
boolean
|
getAndSet(boolean newValue)
Atomically sets to the given value and returns the previous value.
|
final
void
|
lazySet(boolean newValue)
Eventually sets to the given value.
|
final
void
|
set(boolean newValue)
Unconditionally sets to the given value.
|
String
|
toString()
Returns the String representation of the current value.
|
boolean
|
weakCompareAndSet(boolean expect, boolean update)
Atomically sets the value to the given updated value
if the current value == the expected value.
|
Inherited Method Summary
From class
java.lang.Object
Object
|
clone()
Creates and returns a copy of this Object .
|
boolean
|
equals( Object obj)
Compares this instance with the specified object and indicates if they
are equal.
|
void
|
finalize()
Invoked when the garbage collector has detected that this instance is no longer reachable.
|
final
Class<?>
|
getClass()
Returns the unique instance of Class that represents this
object's class.
|
int
|
hashCode()
Returns an integer hash code for this object.
|
final
void
|
notify()
Causes a thread which is waiting on this object's monitor (by means of
calling one of the wait() methods) to be woken up.
|
final
void
|
notifyAll()
Causes all threads which are waiting on this object's monitor (by means
of calling one of the wait() methods) to be woken up.
|
String
|
toString()
Returns a string containing a concise, human-readable description of this
object.
|
final
void
|
wait(long timeout, int nanos)
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the
specified timeout expires.
|
final
void
|
wait(long timeout)
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the
specified timeout expires.
|
final
void
|
wait()
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object.
|
Public Constructors
public
AtomicBoolean
(boolean initialValue)
Creates a new AtomicBoolean
with the given initial value.
Parameters
initialValue |
the initial value
|
public
AtomicBoolean
()
Creates a new AtomicBoolean
with initial value false
.
Public Methods
public
final
boolean
compareAndSet
(boolean expect, boolean update)
Atomically sets the value to the given updated value
if the current value ==
the expected value.
Parameters
expect |
the expected value |
update |
the new value |
Returns
true
if successful. False return indicates that
the actual value was not equal to the expected value.
public
final
boolean
get
()
Returns the current value.
public
final
boolean
getAndSet
(boolean newValue)
Atomically sets to the given value and returns the previous value.
public
final
void
lazySet
(boolean newValue)
Eventually sets to the given value.
public
final
void
set
(boolean newValue)
Unconditionally sets to the given value.
public
String
toString
()
Returns the String representation of the current value.
Returns
- the String representation of the current value
public
boolean
weakCompareAndSet
(boolean expect, boolean update)
Parameters
expect |
the expected value |
update |
the new value |
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-07-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-07-10 UTC."],[[["\u003cp\u003e\u003ccode\u003eAtomicBoolean\u003c/code\u003e provides a way to update a \u003ccode\u003eboolean\u003c/code\u003e value atomically, ensuring thread safety during concurrent operations.\u003c/p\u003e\n"],["\u003cp\u003eIt offers methods like \u003ccode\u003ecompareAndSet\u003c/code\u003e, \u003ccode\u003eget\u003c/code\u003e, and \u003ccode\u003eset\u003c/code\u003e for manipulating the boolean value in a thread-safe manner.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eAtomicBoolean\u003c/code\u003e is commonly used for atomically updated flags but cannot be used as a replacement for a regular \u003ccode\u003eBoolean\u003c/code\u003e object.\u003c/p\u003e\n"],["\u003cp\u003eConstructors allow initialization with a specific boolean value or a default value of \u003ccode\u003efalse\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eweakCompareAndSet\u003c/code\u003e is available but should be used cautiously due to its potential for spurious failures and lack of ordering guarantees.\u003c/p\u003e\n"]]],["`AtomicBoolean` is a class for atomically updating boolean values, used for flags. It offers two constructors: one initializing with a given boolean, the other with `false`. Key methods include `compareAndSet` and `weakCompareAndSet` which conditionally update the value, `get` to retrieve the current value, `getAndSet` for atomic updates with retrieval of the old value, and `set`/`lazySet` for unconditional updates. Additionally, a `toString` is available for current value representation.\n"],null,["public class **AtomicBoolean** extends [Object](../../../../../reference/java/lang/Object.html) \nimplements [Serializable](../../../../../reference/java/io/Serializable.html) \nA `boolean` value that may be updated atomically. See the\n[java.util.concurrent.atomic](../../../../../reference/java/util/concurrent/atomic/package-summary.html) package specification for\ndescription of the properties of atomic variables. An\n`AtomicBoolean` is used in applications such as atomically\nupdated flags, and cannot be used as a replacement for a\n[Boolean](../../../../../reference/java/lang/Boolean.html). \n\nPublic Constructor Summary\n\n|---|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| | [AtomicBoolean](../../../../../reference/java/util/concurrent/atomic/AtomicBoolean.html#AtomicBoolean(boolean))(boolean initialValue) Creates a new `AtomicBoolean` with the given initial value. |\n| | [AtomicBoolean](../../../../../reference/java/util/concurrent/atomic/AtomicBoolean.html#AtomicBoolean())() Creates a new `AtomicBoolean` with initial value `false`. |\n\nPublic Method Summary\n\n|----------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| final boolean | [compareAndSet](../../../../../reference/java/util/concurrent/atomic/AtomicBoolean.html#compareAndSet(boolean,%20boolean))(boolean expect, boolean update) Atomically sets the value to the given updated value if the current value `==` the expected value. |\n| final boolean | [get](../../../../../reference/java/util/concurrent/atomic/AtomicBoolean.html#get())() Returns the current value. |\n| final boolean | [getAndSet](../../../../../reference/java/util/concurrent/atomic/AtomicBoolean.html#getAndSet(boolean))(boolean newValue) Atomically sets to the given value and returns the previous value. |\n| final void | [lazySet](../../../../../reference/java/util/concurrent/atomic/AtomicBoolean.html#lazySet(boolean))(boolean newValue) Eventually sets to the given value. |\n| final void | [set](../../../../../reference/java/util/concurrent/atomic/AtomicBoolean.html#set(boolean))(boolean newValue) Unconditionally sets to the given value. |\n| [String](../../../../../reference/java/lang/String.html) | [toString](../../../../../reference/java/util/concurrent/atomic/AtomicBoolean.html#toString())() Returns the String representation of the current value. |\n| boolean | [weakCompareAndSet](../../../../../reference/java/util/concurrent/atomic/AtomicBoolean.html#weakCompareAndSet(boolean,%20boolean))(boolean expect, boolean update) Atomically sets the value to the given updated value if the current value `==` the expected value. |\n\nInherited Method Summary \nFrom class [java.lang.Object](../../../../../reference/java/lang/Object.html) \n\n|-------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [Object](../../../../../reference/java/lang/Object.html) | [clone](../../../../../reference/java/lang/Object.html#clone())() Creates and returns a copy of this `Object`. |\n| boolean | [equals](../../../../../reference/java/lang/Object.html#equals(java.lang.Object))([Object](../../../../../reference/java/lang/Object.html) obj) Compares this instance with the specified object and indicates if they are equal. |\n| void | [finalize](../../../../../reference/java/lang/Object.html#finalize())() Invoked when the garbage collector has detected that this instance is no longer reachable. |\n| final [Class](../../../../../reference/java/lang/Class.html)\\\u003c?\\\u003e | [getClass](../../../../../reference/java/lang/Object.html#getClass())() Returns the unique instance of [Class](../../../../../reference/java/lang/Class.html) that represents this object's class. |\n| int | [hashCode](../../../../../reference/java/lang/Object.html#hashCode())() Returns an integer hash code for this object. |\n| final void | [notify](../../../../../reference/java/lang/Object.html#notify())() Causes a thread which is waiting on this object's monitor (by means of calling one of the `wait()` methods) to be woken up. |\n| final void | [notifyAll](../../../../../reference/java/lang/Object.html#notifyAll())() Causes all threads which are waiting on this object's monitor (by means of calling one of the `wait()` methods) to be woken up. |\n| [String](../../../../../reference/java/lang/String.html) | [toString](../../../../../reference/java/lang/Object.html#toString())() Returns a string containing a concise, human-readable description of this object. |\n| final void | [wait](../../../../../reference/java/lang/Object.html#wait(long,%20int))(long timeout, int nanos) Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object or until the specified timeout expires. |\n| final void | [wait](../../../../../reference/java/lang/Object.html#wait(long))(long timeout) Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object or until the specified timeout expires. |\n| final void | [wait](../../../../../reference/java/lang/Object.html#wait())() Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object. |\n\nPublic Constructors \n\npublic\n**AtomicBoolean**\n(boolean initialValue) \nCreates a new `AtomicBoolean` with the given initial value. \n\nParameters\n\n| initialValue | the initial value |\n|--------------|-------------------|\n\npublic\n**AtomicBoolean**\n() \nCreates a new `AtomicBoolean` with initial value `false`.\n\nPublic Methods \n\npublic final boolean\n**compareAndSet**\n(boolean expect, boolean update) \nAtomically sets the value to the given updated value\nif the current value `==` the expected value. \n\nParameters\n\n| expect | the expected value |\n| update | the new value |\n|--------|--------------------|\n\nReturns\n\n- `true` if successful. False return indicates that the actual value was not equal to the expected value. \n\npublic final boolean\n**get**\n() \nReturns the current value. \n\nReturns\n\n- the current value \n\npublic final boolean\n**getAndSet**\n(boolean newValue) \nAtomically sets to the given value and returns the previous value. \n\nParameters\n\n| newValue | the new value |\n|----------|---------------|\n\nReturns\n\n- the previous value \n\npublic final void\n**lazySet**\n(boolean newValue) \nEventually sets to the given value. \n\nParameters\n\n| newValue | the new value |\n|----------|---------------|\n\npublic final void\n**set**\n(boolean newValue) \nUnconditionally sets to the given value. \n\nParameters\n\n| newValue | the new value |\n|----------|---------------|\n\npublic [String](../../../../../reference/java/lang/String.html)\n**toString**\n() \nReturns the String representation of the current value. \n\nReturns\n\n- the String representation of the current value \n\npublic boolean\n**weakCompareAndSet**\n(boolean expect, boolean update) \nAtomically sets the value to the given updated value\nif the current value `==` the expected value.\n\n[May fail\nspuriously and does not provide ordering guarantees](/j2objc/javadoc/jre/reference/java/util/concurrent/atomic/package-summary#weakCompareAndSet), so is\nonly rarely an appropriate alternative to `compareAndSet`. \n\nParameters\n\n| expect | the expected value |\n| update | the new value |\n|--------|--------------------|\n\nReturns\n\n- `true` if successful"]]