java - 同步以确保对不可变对象(immutable对象)的引用将被另一个线程看到

标签 java multithreading concurrency immutability final

我在学习this了解新 JMM 中最终字段的行为(从 5 开始)。这个概念很明确:在正确构造对象后,保证初始化的 final 字段对所有线程的可见性。

但是在本节的末尾,我读到了这个,这让我很困惑:

Now, having said all of this, if, after a thread constructs an immutable object (that is, an object that only contains final fields), you want to ensure that it is seen correctly by all of the other thread, you still typically need to use synchronization. There is no other way to ensure, for example, that the reference to the immutable object will be seen by the second thread.

这是否意味着尽管各个最终字段(构成不可变对象(immutable对象))没有同步(例如,此处可见性)问题。但是在一个线程中首次创建的不可变对象(immutable对象)本身在其他线程中可能不可见(正确创建)?

如果是这样,虽然我们可以跨线程共享初始化的不可变对象(immutable对象)而无需担心线程不安全,但在创建时,它们需要像其他可变对象一样“特别注意”线程安全?

最佳答案

final 字段的语义,定义在 section 17.5 of the JLS 中, 保证:

A thread that can only see a reference to an object after that object has been completely initialized is guaranteed to see the correctly initialized values for that object's final fields.

换句话说,它表示如果一个线程看到一个完全初始化的对象,那么它保证能正确看到它的最终字段初始化。

但是,不能保证对象对给定线程可见。这是一个不同的问题。

如果您不使用某种同步来发布对象的引用,那么其他线程可能永远无法看到对它的引用。

考虑以下代码:

final class A {
  private final int x;
  A(int x) { this.x = x; }
  public getX() { return x; }
}

class Main {
  static volatile A a1 = null;
  static A a2 = null;
  public static void main(String[] args) {
    new Thread(new Runnable() { void run() { try {
      while (a1 == null) Thread.sleep(50);
      System.out.println(a1.getX()); } catch (Throwable t) {}
    }}).start()
    new Thread(new Runnable() { void run() { try {
      while (a2 == null) Thread.sleep(50);
      System.out.println(a2.getX()); } catch (Throwable t) {}
    }}).start()
    a1 = new A(1); a2 = new A(1);
  }
}

请注意,a1 字段是可变的。这确保了,最终,对该字段的写入将在一段时间后对所有读取它的线程可见。 a2 字段不是 volatile 的(因此,一个线程对该字段的写入可能永远不会被其他线程注意到)。

在这段代码中,我们可以确定线程 1 将完成执行(也就是说,它将看到 a1 != null。但是,线程 2 可能会停止,因为它永远不会看到对字段 a2 的写入,因为它不是 volatile 的。

关于java - 同步以确保对不可变对象(immutable对象)的引用将被另一个线程看到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6599058/

相关文章:

java - 使用vtd-xml解析xml文件

java - 用线程显示动画

c# - 无法从其他 STA 线程调用从 STAThread 创建的 COM 对象

java - 如何在没有多部分正文的情况下使用 okhttp3 发出 put 请求

java - 通过 Java 应用程序连接到另一台机器上的数据库

java - 如何通过单击事件以编程方式显示/隐藏操作栏项目

C# 使用事件进行线程通信

Java 8 : How can I convert a for loop to run in parallel?

ios - 为什么我必须注释掉我的 NSPredicate 才能让 NSFetchedResultsController 填充 UITableView?

Java多线程: Fast alternative to AtomicInteger wanted