字符串数组 - 不必要的同步?

String array - needless synchronization?

我正在研究 ChronicleHFT 库。我在下面找到了 class StringInterner

public class StringInterner {
    @NotNull
    protected final String[] interner;
    protected final int mask;
    protected final int shift;
    protected boolean toggle = false;

    public StringInterner(int capacity) throws IllegalArgumentException {
        int n = Maths.nextPower2(capacity, 128);
        this.shift = Maths.intLog2((long)n);
        this.interner = new String[n];
        this.mask = n - 1;
    }

    @Nullable
    public String intern(@Nullable CharSequence cs) {
        if (cs == null) {
            return null;
        } else if (cs.length() > this.interner.length) {
            return cs.toString();
        } else {
            int hash = Maths.hash32(cs);
            int h = hash & this.mask;
            String s = this.interner[h];
            if (StringUtils.isEqual(cs, s)) {
                return s;
            } else {
                int h2 = hash >> this.shift & this.mask;
                String s2 = this.interner[h2];
                if (StringUtils.isEqual(cs, s2)) {
                    return s2;
                } else {
                    String s3 = cs.toString();
                    this.interner[s != null && (s2 == null || !this.toggle()) ? h2 : h] = s3;
                    return s3;
                }
            }
   

我找到了 Peter Lawrey 的视频,他在视频中解释(或者更准确地说,他只是说)这个 class 是线程安全的,不需要任何额外的同步就可以在多线程环境中工作.视频 yt link:https://www.youtube.com/watch?v=sNSD6AUG5a0&t=1200

我的问题是为什么这个 class 不需要任何同步?

  1. 可见性如何 - 如果一个线程将某些内容放入内部[n],是否保证另一个线程可以看到它?
  2. 如果调度程序在方法中间产生一个线程,会发生什么情况?它会导致将相同的值两次放入同一索引中吗?

Javadoc for StringInterner 解释说它在技术上不是 thread-safe:

StringInterner only guarantees it will behave in a correct manner. When you ask it for a String for a given input, it must return a String which matches the toString() of that CharSequence.

It doesn't guarantee that all threads see the same data, nor that multiple threads will return the same String object for the same string. It is designed to be a best-effort basis so it can be as lightweight as possible.

So while technically not thread safe, it doesn't prevent it operating correctly when used from multiple threads, but it is faster than added explicit locking or thread safety. NOTE: It does rely on String being thread safe, something which was guarenteed from Java 5.0 onwards.

顺便说一句,我很好奇 String 不是 thread-safe 早于 Java 5 的说法;我很想看到引用。