JavaTM Platform
Standard Ed. 6

java.util.concurrent.atomic
类 AtomicBoolean

java.lang.Object
  继承者 java.util.concurrent.atomic.AtomicBoolean
所有已实现的接口:
Serializable

public class AtomicBoolean
extends Object
implements Serializable

可以用原子方式更新的 boolean 值。有关原子变量属性的描述,请参阅 java.util.concurrent.atomic 包规范。AtomicBoolean 可用在应用程序中(如以原子方式更新的标志),但不能用于替换 Boolean

从以下版本开始:
1.5
另请参见:
序列化表格

构造方法摘要
AtomicBoolean()
          使用初始值 false 创建新的 AtomicBoolean
AtomicBoolean(boolean initialValue)
          使用给定的初始值创建新的 AtomicBoolean
 
方法摘要
 boolean compareAndSet(boolean expect, boolean update)
          如果当前值 == 预期值,则以原子方式将该值设置为给定的更新值。
 boolean get()
          返回当前值。
 boolean getAndSet(boolean newValue)
          以原子方式设置为给定值,并返回以前的值。
 void lazySet(boolean newValue)
          最终设置为给定值。
 void set(boolean newValue)
          无条件地设置为给定值。
 String toString()
          返回当前值的字符串表示形式。
 boolean weakCompareAndSet(boolean expect, boolean update)
          如果当前值 == 预期值,则以原子方式将该值设置为给定的更新值。
 
从类 java.lang.Object 继承的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

构造方法详细信息

AtomicBoolean

public AtomicBoolean(boolean initialValue)
使用给定的初始值创建新的 AtomicBoolean

参数:
initialValue - 初始值

AtomicBoolean

public AtomicBoolean()
使用初始值 false 创建新的 AtomicBoolean

方法详细信息

get

public final boolean get()
返回当前值。

返回:
当前值

compareAndSet

public final boolean compareAndSet(boolean expect,
                                   boolean update)
如果当前值 == 预期值,则以原子方式将该值设置为给定的更新值。

参数:
expect - 预期值
update - 新值
返回:
如果成功,则返回 true。返回 False 指示实际值与预期值不相等。

weakCompareAndSet

public boolean weakCompareAndSet(boolean expect,
                                 boolean update)
如果当前值 == 预期值,则以原子方式将该值设置为给定的更新值。

可能意外失败并且不提供排序保证,因此几乎只是 compareAndSet 的适当替代方法。

参数:
expect - 预期值
update - 新值
返回:
如果成功,则返回 true。

set

public final void set(boolean newValue)
无条件地设置为给定值。

参数:
newValue - 新值

lazySet

public final void lazySet(boolean newValue)
最终设置为给定值。

参数:
newValue - 新值
从以下版本开始:
1.6

getAndSet

public final boolean getAndSet(boolean newValue)
以原子方式设置为给定值,并返回以前的值。

参数:
newValue - 新值
返回:
以前的值

toString

public String toString()
返回当前值的字符串表示形式。

覆盖:
Object 中的 toString
返回:
当前值的字符串表示形式。

JavaTM Platform
Standard Ed. 6

提交错误或意见
有关更多的 API 参考资料和开发人员文档,请参阅 Java SE 开发人员文档。该文档包含更详细的、面向开发人员的描述,以及总体概述、术语定义、使用技巧和工作代码示例。

版权所有 2007 Sun Microsystems, Inc. 保留所有权利。 请遵守许可证条款。另请参阅文档重新分发政策