Top Description Fields Constructors Methods
java.util.concurrent.atomic

public Class AtomicBoolean

extends Object
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Imports
java.lang.invoke.MethodHandles, .VarHandle

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 java.lang.Boolean.
Author
Doug Lea
Since
1.5

Field Summary

Modifier and TypeField and Description
private static final long
private static final VarHandle
private volatile int

Constructor Summary

AccessConstructor and Description
public
AtomicBoolean(boolean
the initial value
initialValue
)

Creates a new AtomicBoolean with the given initial value.

public
AtomicBoolean()

Creates a new AtomicBoolean with initial value false.

Method Summary

Modifier and TypeMethod and Description
public final boolean

Returns:

the witness value, which will be the same as the expected value if successful
compareAndExchange
(boolean
the expected value
expectedValue
,
boolean
the new value
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.

public final boolean

Returns:

the witness value, which will be the same as the expected value if successful
compareAndExchangeAcquire
(boolean
the expected value
expectedValue
,
boolean
the new value
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.

public final boolean

Returns:

the witness value, which will be the same as the expected value if successful
compareAndExchangeRelease
(boolean
the expected value
expectedValue
,
boolean
the new value
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.

public final boolean

Returns:

true if successful. False return indicates that the actual value was not equal to the expected value.
compareAndSet
(boolean
the expected value
expectedValue
,
boolean
the new value
newValue
)

Atomically sets the value to newValue if the current value == expectedValue, with memory effects as specified by VarHandle#compareAndSet.

public final boolean

Returns:

the current value
get
()

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

public final boolean

Returns:

the value
getAcquire
()

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

public final boolean

Returns:

the previous value
getAndSet
(boolean
the new value
newValue
)

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

public final boolean

Returns:

the value
getOpaque
()

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

public final boolean

Returns:

the value
getPlain
()

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

public final void
lazySet(boolean
the new value
newValue
)

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

public final void
set(boolean
the new value
newValue
)

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

public final void
setOpaque(boolean
the new value
newValue
)

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

public final void
setPlain(boolean
the new value
newValue
)

Sets the value to newValue, with memory semantics of setting as if the variable was declared non-volatile and non-final.

public final void
setRelease(boolean
the new value
newValue
)

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

public String

Returns:

the String representation of the current value
toString
()

Overrides java.lang.Object.toString.

Returns the String representation of the current value.

public boolean

Returns:

true if successful
weakCompareAndSet
(boolean
the expected value
expectedValue
,
boolean
the new value
newValue
)
Deprecated since 9. This method has plain memory effects but the method name implies volatile memory effects (see methods such as compareAndExchange and compareAndSet).

Possibly atomically sets the value to newValue if the current value == expectedValue, with memory effects as specified by VarHandle#weakCompareAndSetPlain.

public final boolean

Returns:

true if successful
weakCompareAndSetAcquire
(boolean
the expected value
expectedValue
,
boolean
the new value
newValue
)

Possibly atomically sets the value to newValue if the current value == expectedValue, with memory effects as specified by VarHandle#weakCompareAndSetAcquire.

public boolean

Returns:

true if successful
weakCompareAndSetPlain
(boolean
the expected value
expectedValue
,
boolean
the new value
newValue
)

Possibly atomically sets the value to newValue if the current value == expectedValue, with memory effects as specified by VarHandle#weakCompareAndSetPlain.

public final boolean

Returns:

true if successful
weakCompareAndSetRelease
(boolean
the expected value
expectedValue
,
boolean
the new value
newValue
)

Possibly atomically sets the value to newValue if the current value == expectedValue, with memory effects as specified by VarHandle#weakCompareAndSetRelease.

public final boolean

Returns:

true if successful
weakCompareAndSetVolatile
(boolean
the expected value
expectedValue
,
boolean
the new value
newValue
)

Possibly atomically sets the value to newValue if the current value == expectedValue, with memory effects as specified by VarHandle#weakCompareAndSet.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

serialVersionUIDback to summary
private static final long serialVersionUID
VALUEback to summary
private static final VarHandle VALUE
valueback to summary
private volatile int value

Constructor Detail

AtomicBooleanback to summary
public AtomicBoolean(boolean initialValue)

Creates a new AtomicBoolean with the given initial value.

Parameters
initialValue:boolean

the initial value

AtomicBooleanback to summary
public AtomicBoolean()

Creates a new AtomicBoolean with initial value false.

Method Detail

compareAndExchangeback to summary
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

Since
9
compareAndExchangeAcquireback to summary
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

Since
9
compareAndExchangeReleaseback to summary
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

Since
9
compareAndSetback to summary
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.

getback to summary
public final boolean get()

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

Returns:boolean

the current value

getAcquireback to summary
public final boolean getAcquire()

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

Returns:boolean

the value

Since
9
getAndSetback to summary
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

getOpaqueback to summary
public final boolean getOpaque()

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

Returns:boolean

the value

Since
9
getPlainback to summary
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

Since
9
lazySetback to summary
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

Since
1.6
setback to summary
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

setOpaqueback to summary
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

Since
9
setPlainback to summary
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

Since
9
setReleaseback to summary
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

Since
9
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Returns the String representation of the current value.

Returns:String

the String representation of the current value

weakCompareAndSetback to summary
public boolean weakCompareAndSet(boolean expectedValue, boolean newValue)

Deprecated

since 9.

This method has plain memory effects but the method name implies volatile memory effects (see methods such as compareAndExchange and compareAndSet). To avoid confusion over plain or volatile memory effects it is recommended that the method weakCompareAndSetPlain 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

Annotations
@Deprecated
since:9
See Also
weakCompareAndSetPlain
weakCompareAndSetAcquireback to summary
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

Since
9
weakCompareAndSetPlainback to summary
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

Since
9
weakCompareAndSetReleaseback to summary
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

Since
9
weakCompareAndSetVolatileback to summary
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

Since
9