本文介绍了为什么java.util.concurrent.atomic.AtomicBoolean在内部用int实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
AtomicBoolean将其值存储在:
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;
}
其背后的原因是什么?为什么不使用boolean
?
What is the reason behind it? Why boolean
was not used?
推荐答案
AFAIK,int
是可在不同机器类型上实现的最小CAS操作类型.
AFAIK, int
is the smallest type CAS operations can be implemented across different machine types.
注意:由于对象分配是8字节对齐的,因此使用较小的类型将不会节省任何内存.
Note: as object allocations are 8 byte aligned, using a smaller type wouldn't save any memory.
这篇关于为什么java.util.concurrent.atomic.AtomicBoolean在内部用int实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!