同步块是否会触发数组的完整内存栅栏?

Does a synchronized block trigger a full memory fence for arrays?

我对在 Java 中的线程之间安全地共享数组感到困惑,特别是内存栅栏和关键字 synchronized

此问答很有帮助,但并未回答我的所有问题:Java arrays: synchronized + Atomic*, or synchronized suffices?

以下是演示该问题的示例代码。假设有一个工作线程池通过方法 add(...) 填充 SharedTable。所有工作线程完成后,最后一个线程读取并保存数据。

演示问题的示例代码:

public final class SharedTable {

    // Column-oriented data entries
    private final String[] data1Arr;
    private final int[] data2Arr;
    private final long[] data3Arr;
    private final AtomicInteger nextIndex;

    public SharedTable(int size) {
        this.data1Arr = new String[size];
        this.data2Arr = new int[size];
        this.data3Arr = new long[size];
        this.nextIndex = new AtomicInteger(0);
    }

    // Thread-safe: Called by worker threads
    public void addEntry(String data1, int data2, long data3) {
        final int index = nextIndex.getAndIncrement();
        data1Arr[index] = data1;
        data2Arr[index] = data2;
        data3Arr[index] = data3;
    }

    // Not thread-safe: Called by clean-up/joiner/collator thread...
    // after worker threads are complete
    public void save() {
        // Does this induce a full memory fence to ensure thread-safe reading of 
        synchronized (this) {
            final int usedSide = nextIndex.get();
            for (int i = 0; i < usedSide; ++i) {
                final String data1 = data1Arr[i];
                final int    data2 = data2Arr[i];
                final long   data3 = data3Arr[i];
                // TODO: Save data here
            }
        }
    }
}

上面的示例代码也可以使用 Atomic*Array 来实现,它充当 "array of volatile values/references".

public final class SharedTable2 {

    // Column-oriented data entries
    private final AtomicReferenceArray<String> data1Arr;
    private final AtomicIntegerArray  data2Arr;
    private final AtomicLongArray data3Arr;
    private final AtomicInteger nextIndex;

    public SharedTable2(int size) { ... }

    // Thread-safe: Called by worker threads
    public void addEntry(String data1, int data2, long data3) {
        final int index = nextIndex.getAndIncrement();
        data1Arr.set(index, data1);
        ...
    }

    // Not thread-safe: Called by clean-up/joiner/collator thread...
    // after worker threads are complete
    public void save() {
        final int usedSide = nextIndex.get();
        for (int i = 0; i < usedSide; ++i) {
            final String data1 = data1Arr.get(i);
            final int    data2 = data2Arr.get(i);
            final long   data3 = data3Arr.get(i);
            // TODO: Save data here
        }
    }
}
  1. SharedTable 线程安全(和缓存一致)吗?
  2. SharedTable(很多?)效率更高,因为只需要一个内存栅栏,而 SharedTable2 每次调用 Atomic*Array.set(...) 都会调用一个内存栅栏吗?

如果有帮助,我在 64 位 x86 硬件(Windows 和 Linux)上使用 Java 8。

不,SharedTable 不是 thread-safe。 happens-before 仅当您从同步块中读取已使用相同锁从同步块中写入的内容时才能得到保证。

由于写入是在同步块中进行的,因此 JMM 不保证 reader 线程可以看到写入。