如何使用AtomicBoolean以及该类的用途?
这是我做的笔记(摘自Brian Goetz的书),可能对你有帮助
原子XXX类
provide Non-blocking Compare-And-Swap implementation Takes advantage of the support provide by hardware (the CMPXCHG instruction on Intel) When lots of threads are running through your code that uses these atomic concurrency API, they will scale much better than code which uses Object level monitors/synchronization. Since, Java's synchronization mechanisms makes code wait, when there are lots of threads running through your critical sections, a substantial amount of CPU time is spent in managing the synchronization mechanism itself (waiting, notifying, etc). Since the new API uses hardware level constructs (atomic variables) and wait and lock free algorithms to implement thread-safety, a lot more of CPU time is spent "doing stuff" rather than in managing synchronization. not only offer better throughput, but they also provide greater resistance to liveness problems such as deadlock and priority inversion.
AtomicBoolean类为您提供了一个可以自动更新的布尔值。当有多个线程访问一个布尔变量时使用它。
atomic包概述为您提供了一个很好的高级描述,说明这个包中的类做什么以及何时使用它们。我还推荐Brian Goetz写的《Java并发实践》这本书。
为什么可以使用原子布尔值有两个主要原因。首先,它是可变的,例如,您可以将它作为引用传递进来,并更改与布尔值本身相关的值。
public final class MyThreadSafeClass{
private AtomicBoolean myBoolean = new AtomicBoolean(false);
private SomeThreadSafeObject someObject = new SomeThreadSafeObject();
public boolean doSomething(){
someObject.doSomeWork(myBoolean);
return myBoolean.get(); //will return true
}
}
和someObject类
public final class SomeThreadSafeObject{
public void doSomeWork(AtomicBoolean b){
b.set(true);
}
}
更重要的是,它是线程安全的,并且可以向维护该类的开发人员表明,该变量预计将从多个线程中修改和读取。如果不使用AtomicBoolean,则必须通过将布尔变量声明为volatile或围绕字段的读写同步来同步所使用的布尔变量。
当多个线程需要检查和更改布尔值时。例如:
if (!initialized) {
initialize();
initialized = true;
}
这不是线程安全的。你可以使用AtomicBoolean来修复它:
if (atomicInitialized.compareAndSet(false, true)) {
initialize();
}
节选自包装描述
Package java.util.concurrent.atomic description: A small toolkit of classes that support lock-free thread-safe programming on single variables.[...] The specifications of these methods enable implementations to employ efficient machine-level atomic instructions that are available on contemporary processors.[...] Instances of classes AtomicBoolean, AtomicInteger, AtomicLong, and AtomicReference each provide access and updates to a single variable of the corresponding type.[...] The memory effects for accesses and updates of atomics generally follow the rules for volatiles: get has the memory effects of reading a volatile variable. set has the memory effects of writing (assigning) a volatile variable. weakCompareAndSet atomically reads and conditionally writes a variable, is ordered with respect to other memory operations on that variable, but otherwise acts as an ordinary non-volatile memory operation. compareAndSet and all other read-and-update operations such as getAndIncrement have the memory effects of both reading and writing volatile variables.
推荐文章
- 同步vs锁定
- 在ExecutorService的提交和执行之间进行选择
- 如何在javadoc中转义@字符?
- 如何在Java中创建唯一的ID ?
- 为什么Java类的编译与空行不同?
- Android Studio无法找到有效的Jvm(与MAC OS相关)
- 为什么一个组合器需要减少方法转换类型在java 8
- CountDownLatch是如何在Java多线程中使用的?
- 如果我同步了同一个类上的两个方法,它们能同时运行吗?
- 如何减去X天从一个日期对象在Java?
- Java标识符中的“连接字符”是什么?
- 一对多、多对一、多对多的区别?
- 从枚举中选择一个随机值?
- 如何从URL获取参数与JSP
- 如何在Eclipse中生成Javadoc注释?