gpt4 book ai didi

列表的Java线程安全

转载 作者:搜寻专家 更新时间:2023-10-30 19:56:56 25 4
gpt4 key购买 nike

我有一个列表,它既可以在线程安全上下文中使用,也可以在非线程安全上下文中使用。会是哪一个,无法提前确定。

在这种特殊情况下,每当列表进入非线程安全上下文时,我都会使用

Collections.synchronizedList(...)

但我不想包装它,如果不进入非线程安全上下文。 F.e.,因为列表很大并且被密集使用。

我读过有关 Java 的文章,它的优化策略对多线程非常严格——如果你没有正确同步代码,则不能保证在线程间上下文中正确执行——它可以显着重组代码,在只有一个线程的上下文中提供一致性(参见 http://java.sun.com/docs/books/jls/third_edition/html/memory.html#17.3 )。例如,

op1; op2; op3;

可能会重组

op3; op2; op1;

,如果它产生相同的结果(在单线程上下文中)。

现在我想知道,如果我

  1. 在用 synchronizedList 包装之前填充我的列表,

  2. 然后把它包起来,

  3. 然后通过不同的线程使用

, - 是否有可能,不同的线程会看到此列表仅部分填充或根本未填充? JVM 可以将 (1) 推迟到 (3) 之后吗?是否有一种正确且快速的方法可以使(大)列表从非线程安全变为线程安全?

最佳答案

当您通过线程安全方式(例如使用同步块(synchronized block)、volatile 变量或AtomicReference)将您的列表提供给另一个线程时,可以保证第二个线程看到整个列表处于传输时的状态(或任何较晚的状态,但不是较早的状态)。

如果你之后不改变它,你也不需要你的 synchronizedList。


编辑(经过一些评论,以支持我的主张):

我假设如下:

  • 我们有一个可变变量 list

    volatile List<String> list = null;
  • 线程 A:

    1. 创建一个列表 L 并用元素填充 L。
    2. list 设置为指向 L(这意味着将 L 写入 list)
    3. 不对 L 做进一步修改。

    样本来源:

    public void threadA() {
    List<String> L = new ArrayList<String>();
    L.add("Hello");
    L.add("World");
    list = l;
    }
  • 线程 B:

    1. list 中读取 K
    2. 遍历 K,打印元素。

    样本来源:

    public void threadB() {
    List<String> K = list;
    for(String s : K) {
    System.out.println(s);
    }
    }
  • 所有其他线程不触及列表。

现在我们有这个:

  • 线程 A 中的操作 1-A 和 2-A 按 program order 排序所以 1 在 2 之前。
  • 线程 B 中的操作 1-B 和 2-B 按 program order 排序所以 1 在 2 之前。
  • 线程 A 中的操作 2-A 和线程中的操作 1-B 按 synchronization order 排序,所以 2-A 出现在 1-B 之前,因为

    A write to a volatile variable (§8.3.1.4) v synchronizes-with all subsequent reads of v by any thread (where subsequent is defined according to the synchronization order).

  • happens-before -order 是各个线程的程序顺序和同步顺序的传递闭包。所以我们有:

    1-A happens-before 2-A happens-before 1-B happens-before 2-B

    因此 1-A 发生在 2-B 之前。

  • 最后,

    If one action happens-before another, then the first is visible to and ordered before the second.

所以我们的迭代线程真的可以看到整个列表,而不仅仅是它的某些部分。因此,使用单个 volatile 变量传输列表就足够了,在这种简单的情况下我们不需要同步。


关于线程 A 的程序顺序的更多编辑(在这里,因为我比评论中有更多的格式自由)。(我还在上面添加了一些示例代码。)

来自 JLS(第 program order 节):

Among all the inter-thread actions performed by each thread t, the program order of t is a total order that reflects the order in which these actions would be performed according to the intra-thread semantics of t.

那么,线程 A 的线程内语义是什么?

一些 paragraphs above :

The memory model determines what values can be read at every point in the program. The actions of each thread in isolation must behave as governed by the semantics of that thread, with the exception that the values seen by each read are determined by the memory model. When we refer to this, we say that the program obeys intra-thread semantics. Intra-thread semantics are the semantics for single threaded programs, and allow the complete prediction of the behavior of a thread based on the values seen by read actions within the thread. To determine if the actions of thread t in an execution are legal, we simply evaluate the implementation of thread t as it would be performed in a single threaded context, as defined in the rest of this specification.

本规范的其余部分包括section 14.2 (Blocks) :

A block is executed by executing each of the local variable declaration statements and other statements in order from first to last (left to right).

所以,程序顺序确实是语句/表达式的顺序程序源码中给出。

因此,在我们的示例源代码中,内存操作创建一个新的 ArrayList添加“Hello”添加“World”,以及分配给 list(前三个包含更多子操作)确实在此程序顺序中。

(VM 不必按此顺序执行操作,但此程序顺序 仍然有助于发生在 顺序,因此有助于可见性其他线程。)

关于列表的Java线程安全,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4922173/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com