AtomicBoolean做了哪些volatile boolean不能做到的事情?


当前回答

I use volatile fields when said field is ONLY UPDATED by its owner thread and the value is only read by other threads, you can think of it as a publish/subscribe scenario where there are many observers but only one publisher. However if those observers must perform some logic based on the value of the field and then push back a new value then I go with Atomic* vars or locks or synchronized blocks, whatever suits me best. In many concurrent scenarios it boils down to get the value, compare it with another one and update if necessary, hence the compareAndSet and getAndSet methods present in the Atomic* classes.

检查java.util.concurrent.atomic包中的JavaDocs,以获得Atomic类的列表和它们如何工作的出色解释(刚刚了解到它们是无锁的,因此它们比锁或同步块更有优势)

其他回答

布尔基元类型是原子的,用于写和读操作,volatile保证happens before原则。因此,如果您需要一个简单的get()和set(),那么您不需要AtomicBoolean。

另一方面,如果你需要在设置一个变量的值之前执行一些检查,例如。"如果为真,则设置为假",那么您也需要原子地执行此操作,在这种情况下使用compareAndSet和AtomicBoolean提供的其他方法,因为如果您尝试使用volatile boolean实现此逻辑,则需要一些同步来确保get和set之间的值没有更改。

你不能将compareAndSet, getAndSet作为带有volatile boolean的原子操作(除非你同步它)。

记住成语——

READ - MODIFY- WRITE这个你不能用volatile实现

I use volatile fields when said field is ONLY UPDATED by its owner thread and the value is only read by other threads, you can think of it as a publish/subscribe scenario where there are many observers but only one publisher. However if those observers must perform some logic based on the value of the field and then push back a new value then I go with Atomic* vars or locks or synchronized blocks, whatever suits me best. In many concurrent scenarios it boils down to get the value, compare it with another one and update if necessary, hence the compareAndSet and getAndSet methods present in the Atomic* classes.

检查java.util.concurrent.atomic包中的JavaDocs,以获得Atomic类的列表和它们如何工作的出色解释(刚刚了解到它们是无锁的,因此它们比锁或同步块更有优势)

它们完全不同。考虑下面这个易变整数的例子:

volatile int i = 0;
void incIBy5() {
    i += 5;
}

如果两个线程并发调用这个函数,i可能在后面是5,因为编译后的代码会有点类似于这个(除了你不能同步int):

void incIBy5() {
    int temp;
    synchronized(i) { temp = i }
    synchronized(i) { i = temp + 5 }
}

如果一个变量是volatile的,那么对它的每个原子访问都是同步的,但是并不总是很明显什么才是原子访问。使用Atomic*对象,可以保证每个方法都是“原子的”。

因此,如果使用AtomicInteger和getAndAdd(int delta),可以确保结果为10。以同样的方式,如果两个线程同时对一个布尔变量求反,使用AtomicBoolean可以确保它之后具有原始值,而使用volatile布尔则不能。

因此,每当有多个线程修改一个字段时,您需要使其原子化或使用显式同步。

volatile的用途是不同的。考虑这个例子

volatile boolean stop = false;
void loop() {
    while (!stop) { ... }
}
void stop() { stop = true; }

如果有一个线程运行loop(),另一个线程调用stop(),如果省略volatile,可能会陷入无限循环,因为第一个线程可能缓存stop的值。这里,volatile提示编译器在优化时要更加小心。