JMM 顺序一致性 [英] JMM sequential consistency

查看:74
本文介绍了JMM 顺序一致性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从 Java 内存模型的角度理解顺序一致性".JLS 第 17 章的定义对我来说不是很清楚.

I'm trying to understand "sequential consistency" in terms of Java memory model. Definition from JLS, chapter 17 is not that clear to me.

我会给出我的看法,如果我错了,请纠正我.具有一个线程顺序一致性的程序意味着如果在程序顺序中action1在action2之前,那么action2应该看到action1的结果.

I will give my vision, correct me if I'm wrong. Having program with one thread sequential consistency means that if action1 comes before action2 in the program order, than action2 should see the results of action1.

有两个线程.

线程 1:

action1
action2

线程 2:

action3
action4

如果action3看到action2的结果,那么它也应该看到action1的结果.

If action3 sees the result of action2, than it should see the results of action1 as well.

推荐答案

你说得对;顺序一致性意味着每个动作都以原子方式执行,并且对所有线程立即可见.就好像你把所有的线程交织成一个线程,一个一个地执行动作.

You are correct; sequential consistency means that each action is executed atomically and is immediately visible to all threads. It is as if you interleaved all threads into a single thread, executing actions one by one.

请注意顺序一致性并不是 Java 内存模型的实际工作方式.

引自 17.4.3、节目单和节目单:

如果我们使用顺序一致性作为我们的内存模型,我们讨论过的许多编译器和处理器优化都是非法的.

If we were to use sequential consistency as our memory model, many of the compiler and processor optimizations that we have discussed would be illegal.

这篇关于JMM 顺序一致性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆