gpt4 book ai didi

java - 指令重新排序和发生之前的关系

转载 作者:IT老高 更新时间:2023-10-28 20:32:43 26 4
gpt4 key购买 nike

在 Java Concurrency In Practice 一书中,我们多次被告知,程序的指令可以由编译器、运行时的 JVM 甚至处理器重新排序。所以我们应该假设执行的程序不会按照与我们在源代码中指定的顺序完全相同的顺序执行其指令。

然而,讨论 Java 内存模型的最后一章提供了 happens-before 规则列表,表明 JVM 保留了哪些指令顺序。这些规则中的第一条是:

  • “程序顺序规则。线程中的每个操作都发生在该线程中按程序顺序后面的每个操作之前。”

我相信“程序顺序”是指源代码。

我的问题:假设这条规则,我想知道实际上可能会重新排序什么指令。

“ Action ”定义如下:

The Java Memory Model is specified in terms of actions, which include reads and writes to variables, locks and unlocks of monitors, and starting and joining with threads. The JMM defines a partial ordering called happens before on all actions within the program. To guarantee that the thread executing action B can see the results of action A (whether or not A and B occur in different threads), there must be a happens before relationship between A and B. In the absence of a happens before ordering between two operations, the JVM is free to reorder them as it pleases.

提到的其他订单规则有:

  • 监控锁定规则。监视器锁上的解锁发生在同一监视器锁上的每个后续锁之前。
  • 易变的变量规则。对 volatile 字段的写入发生在对同一字段的每次后续读取之前。
  • 线程启动规则。线程上的 Thread.start 调用发生在已启动线程中的每个操作之前。
  • 线程终止规则。线程中的任何操作发生在任何其他线程检测到该线程已终止之前,通过从 Thread.join 成功返回或通过 Thread.isAlive 返回 false。
  • 中断规则。一个线程在另一个线程上调用中断发生在被中断线程检测到中断之前(通过引发 InterruptedException,或者调用 isInterrupted 或中断)。
  • 终结器规则。对象的构造函数结束发生在该对象的终结器开始之前。
  • 传递性。如果 A 在 B 之前发生,B 在 C 之前发生,那么 A 在 C 之前发生。

最佳答案

程序顺序规则的重点是:在一个线程中

想象一下这个简单的程序(所有变量最初都是 0):

T1:

x = 5;
y = 6;

T2:

if (y == 6) System.out.println(x);

从 T1 的角度来看,执行必​​须与在 x 之后分配的 y(程序顺序)一致。然而,从 T2 的角度来看,情况并非如此,T2 可能会打印 0。

实际上允许 T1 先分配 y,因为这 2 个分配是独立的,交换它们不会影响 T1 的执行。

如果同步正确,T2 将始终打印 5 或什么也不打印。

编辑

您似乎误解了程序顺序的含义。 The program order rule boils down to :

If x and y are actions of the same thread and x comes before y in program order, then hb(x, y) (i.e. x happens-before y).

happens-before 在 JMM 中具有非常特殊的含义。特别是,它意味着从挂钟的角度来看,y=6 必须在 T1 中的 x=5 之后。这只意味着 T1 执行的 Action 顺序必须该顺序一致。也可以引用JLS 17.4.5 :

It should be noted that the presence of a happens-before relationship between two actions does not necessarily imply that they have to take place in that order in an implementation. If the reordering produces results consistent with a legal execution, it is not illegal.

在我上面给出的示例中,您会同意,从 T1 的角度来看(即在单线程程序中),x=5;y=6;y=6 一致;x=5; 因为您没有读取这些值。在 T1 中,下一行的语句保证可以查看这 2 个操作,无论它们执行的顺序如何。

关于java - 指令重新排序和发生之前的关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16213443/

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