例如,采用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);
}摘自AtomicLongArray.java的片段。
当数组字段是最终字段时,为什么在这个构造函数中需要volatileWrite?
发布于 2015-11-26 12:55:40
不需要它,这一行代码在JDK的后期版本中被删除了。
这是JDK 1.7和1.8中的样子。
public AtomicLongArray(int length) {
array = new long[length];
}https://stackoverflow.com/questions/33938964
复制相似问题