- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
JLS 中写到关于 JMM 的 happens-before 关系(第 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.
我对这个陈述的例子很感兴趣。
我的理解是否正确,例如以下内容:
Thread 1
x = 1
lock M
y = 2
unlock M ----------------------->Thread 2:
lock M
z = 3
w = 4
unlock M
显然,在此执行跟踪中,(x = 1) 和 (w = 4) 之间存在先行关系:
(x = 1) happens-before (w = 4)
同样在这种情况下,(x = 1) 在执行跟踪中与 (w = 4) 一起排序。
线程 1
不使用变量 w
。因此,我们可以将它放在 (x = 1) 之前,而不会违反 Thread 1
和 Thread 2
的逻辑。
这是否意味着如果我们对 (x = 1) 和 (w = 4) 重新排序,那么这些语句之间的 happens-before 关系会保留?
如果您有其他示例,请提供。
最佳答案
是的,您是对的,当独立数据发生变化时,先行发生关系成立。至于其他示例,请不要忘记 happens-before 也适用于同一线程中的事件。规则很简单:
If x and y are actions of the same thread and x comes before y in program order, then hb(x, y).
因此 Java 方法中的每个语句都发生在每个后续语句之前,但是 JIT 编译器和 CPU 当然可以自由地重新排序独立的语句(实际上它们经常这样做以优化性能)。有时您可以从与当前线程没有先行关系的另一个线程观察到这种重新排序。
关于java - 在 JMM 的 happens-before 关系中重新排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34775517/
一 点睛 并发编程的三大特性是原子性、可见性和顺序性。 JVM 采用内存模型的机制来屏蔽各个平台和操作系统之间内存访问的差异,以实现让 Java 程序在各个平台下达到一致的内存访问效果,比如 C 语言
我多次看到这样的结构: class OneTimeWriter { volatile Object o; void synchronized set(Object o) {
这里 http://docs.oracle.com/javase/specs/jls/se8/html/jls-17.html#jls-17.4.8 是什么意思 hbi|Ci = hb|Ci soi|
我试图从 Java 内存模型的角度理解“顺序一致性”。 JLS 的定义,第 17 章对我来说不是那么清楚。 我会给出我的看法,如果我错了请纠正我。具有单线程顺序一致性的程序意味着如果 action1
我有一个关于 JMM 的问题。我知道什么是“之前发生”,但我不明白一件事。有一段代码: private static int x = 0; private static volatile boolea
考虑以下用 Java 编写的多线程代码: 共享变量: boolean n; // non-volatile volatile boolean v; // volatile 线
我正在尝试了解 JMM 的因果关系要求。 JMM 的这一部分描述了它 https://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls
这里 [ http://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls-17.5][1] 它说: Given a write w,
我尝试理解最终字段的语义。 让我们研究代码: public class App { final int[] data; static App instance; public
我最近偶然发现了 this jcstress 中的示例: @JCStressTest @State @Outcome(id = "10", expect =
这篇文章是在阅读后提出的:https://shipilev.net/blog/2016/close-encounters-of-jmm-kind/#pitfall-semi-sync class Bo
我正在寻找一种将对象从后台线程传递到 UI 线程的安全方法。下面的代码是否安全? // on background thread final HugeObject object = construct
我正在阅读有关 java singleton 的内容,并且遇到了一些奇怪的事情。 我会引用following artice例如(您可以轻松找到更多) 作者提供了以下单例: public class A
以下是 volatile 关键字的使用示例。 public class Test3{ public static volatile boolean stop = false;// if vol
请考虑以下代码片段。 public class Visibility { private volatile SomeData data; public static class Som
对 trylock 的不当使用 T1 T2 x = 42; while (
我只是想知道下面的重新排序在新的 JMM 模型下是否有效 Original Code: instanceVar1 = value ;// normal read operation, no
关闭。这个问题需要更多 focused .它目前不接受答案。 想改进这个问题?更新问题,使其仅关注一个问题 editing this post . 2年前关闭。 Improve this questi
假设这个类: public class AmIThreadSafe { private int a; private int b; AmIThreadSafe(int a, i
JLS 中写到关于 JMM 的 happens-before 关系(第 17.4.5 节): It should be noted that the presence of a happens-bef
我是一名优秀的程序员,十分优秀!