AtomicBoolean stores its value in:
private volatile int value;
Then, for example, extracting its value is done like this:
public final boolean get() {
return value != 0;
}
What is the reason behind it? Why boolean
was not used?
AtomicBoolean stores its value in:
private volatile int value;
Then, for example, extracting its value is done like this:
public final boolean get() {
return value != 0;
}
What is the reason behind it? Why boolean
was not used?
AFAIK,int
是可以跨不同机器类型实现的最小类型 CAS 操作。
注意:由于对象分配是 8 字节对齐的,因此使用较小的类型不会节省任何内存。
这可能是为了能够将几个Atomic
类建立在同一个基 ( Unsafe
) 上,该基使用整数并提供比较和交换操作。
Concurrency in Practice很好地解释了内部工作原理。