报告声明为 volatile 的数组字段。 这样的声明可能会令人混淆,因为访问数组本身遵循 volatile 字段的规则,但访问数组的内容时并非如此。

示例:


  class Data {
    private volatile int[] idx = new int[0];
  }

如果需要对数组内容进行这样的 volatile 访问,不妨改为使用 java.util.concurrent.atomic 类:


  class Data {
    private final AtomicIntegerArray idx = new AtomicIntegerArray(new int[0]);
  }