例如,采用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?
不需要它,并且此代码行在JDK的更高版本中已删除。
在JDK 1.7和1.8中的外观
public AtomicLongArray(int length) {
array = new long[length];
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句