例如,获取数组大小为java.util.concurrent.atomic.AtomicLongArray的构造函数定义如下:
public AtomicLongArray(int length) {
array = new long[length];
// must perform at least one volatile write to conform to JMM
if (length > 0)
unsafe.putLongVolatile(array, rawIndex(0), 0);
}
为什么当数组字段为final时,此构造函数中是否需要volatileWrite?
答案 0 :(得分:2)
没有必要,并且在JDK的更高版本中删除了这行代码
它在JDK 1.7和1.8中的显示方式
public AtomicLongArray(int length) {
array = new long[length];
}