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O1Q3xJZ4bnet3dlzjTDpVo++PTcn2+5l4nNHrU93wVPa19sLOaVRhBgu3O6dM18xnN+q3UuxO29mPAsWw5UhxICEN/6eav88H6mNv49Uaks+N9qBM2vPe4Y3s2L2ifqVXOpaTnbTc52t3lIxgz8kRB+xdCVbbWO80fkcFX7YvYxrkmIZgDglYceofmGTHCXsG7XVNi6HcmGgYiivd37UZgbkAYHef7q0g5iJQD03GrdD5UbT9ltzqfFWXVsudcdRy9z/rN72PmF20D+M2mrtS45a/qU9ypzsoIAjGvWXjSLUCF2Uw7XNQQn4Hrdv1FbGU8wu02HZbuV650SjGDrfj2kGUQzo00Hxz1RuNGm/NZcabzXUOTF2OmoZB8VkYZ36X211Ww8/rN6eLoawmEm8v2I/FV7Sf9w+fb1z4iZiIHKjSfutudR4q305Hs0VmH16fnSCZL9ViD+RiKHmjlTY41+nisGTT0QV+2knj1pu079XOYOR1zsn7mFmGLTfmkuNt1o9GaPcTQoDZDZSZlZHi8HuJYhPzpgZfLt9IGf03lbtp51snRlmy2RiQJ8bjduhcqNp+6251GmrfU52Yy71QcGlAoiVkxjaHVS2ZhgpBhis270Uf8oku7fjxZCURA3T9mEsl14LaZ+63jkxsJuUBjEC7BrFDR9lHPncaPI50EhudM0+Vr+eSw1POfG5znHpjO72gA2cXdefLQgkUkmQ4QowbnbxUGoqFSRiJw9j+imCsG90+JhdF9kudr3zY5rfGfi50cwMmEAM/NxoZh7wXasME2AxkKSbifzKX7f++0wPi4FhAiwGhgmwGBgmwGJgmACLgWECLAaGCbAYGCbAYmCYwD+1hIJH5lhbTgAAAABJRU5ErkJggg==" alt="" />
可以用原子方式更新的 boolean
值。有关原子变量属性的描述,请参阅 java.util.concurrent.atomic
包规范。AtomicBoolean
可用在应用程序中(如以原子方式更新的标志),但不能用于替换 Boolean
。
2.构造函数
1.AtomicBoolean()
使用初始值 false
创建新的 AtomicBoolean
。
2.AtomicBoolean(
boolean initialValue
)
使用给定的初始值创建新的 AtomicBoolean
。
3.方法详解
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()