Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 字符串数组-不必要的同步?_Java_Multithreading_Chronicle - Fatal编程技术网

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

Java 字符串数组-不必要的同步?,java,multithreading,chronicle,Java,Multithreading,Chronicle,我在研究HFT图书馆。我在下面找到了一个classStringInterner public class StringInterner { @NotNull protected final String[] interner; protected final int mask; protected final int shift; protected boolean toggle = false; public StringInterner(in

我在研究HFT图书馆。我在下面找到了一个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的yt视频,他在视频中解释(或者更准确地说,他只是说)这个类是线程安全的,在多线程环境中工作不需要任何额外的同步。视频yt链接:

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

  • 可见性如何?如果一个线程将某个内容放入interner[n],是否保证另一个线程可以看到它
  • 当调度器在方法中间产生一个线程时,会发生什么?是否会导致在同一索引中放置相同的值两次 说明它在技术上不是线程安全的:

    StringInterner只保证它将以正确的方式运行。当您为给定输入请求字符串时,它必须返回与该字符序列的toString()匹配的字符串

    它不能保证所有线程都看到相同的数据,也不能保证多个线程将为同一字符串返回相同的字符串对象。它被设计成一个尽力而为的基础,因此它可以尽可能轻量化

    因此,虽然从技术上讲它不是线程安全的,但当从多个线程使用它时,它不会阻止它正确运行,但它比添加显式锁定或线程安全更快。注意:它确实依赖于线程安全的字符串,这是从Java5.0开始就得到保证的


    顺便说一句,我对
    String
    在Java5之前不是线程安全的说法感到好奇;我很想看一篇引文。

    线程安全和保证线程安全并不是一回事。这可能只是规格上的一个变化。。。而且(所有/大多数/典型/任何)字符串实现在Java5之前实际上都是线程安全的。。。我们应该相信不会拼写“保证”这个词的javadoc吗?:-)JSR133改进了内存模型。这也包括在J2SE Tiger中。这就是Java5.0,Alexey Shipilev建议不要使用String.intern(),并在他的字符串问答中从interning的概念转向重复数据消除。实施重复数据消除器很容易。实施一个实习生是非常困难的!关于这个想法的更多信息,请访问