Java内存模型是否可以保证线程内写入的可见性? [英] Does the Java Memory Model guarantee visibility of intra-thread writes?

查看:62
本文介绍了Java内存模型是否可以保证线程内写入的可见性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一个简单的单线程Java程序执行,该程序不涉及同步操作,仅对实例变量进行普通读写.只是忽略所有写入的实现似乎符合Java内存规范.首先,来自§17.4的适用一般声明:

Consider a simple, single-threaded Java program execution involving no synchronization actions, just plain reads and writes of instance variables. An implementation that simply ignores all writes seems to comply with the Java Memory Specification. First, the applicable general statement from §17.4:

内存模型决定了程序中每个点都可以读取哪些值.孤立的每个线程的行为必须遵循该线程的语义,除非每次读取所看到的值均由内存模型确定.

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.

相关的约束如下(§17.4.5):

如果x和y是同一线程的动作,并且x按程序顺序位于y之前,则hb(x,y).

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

2.在一致性之前发生:

如果对A中的所有读取r都执行了一组动作A,则该动作A是一致的,其中W(r)是r看到的写入动作,则无论hb(r,W(r))都不是这种情况或者在A中存在一个写w,使得wv = rv且hb(W(r),w)和hb(w,r).

A set of actions A is happens-before consistent if for all reads r in A, where W(r) is the write action seen by r, it is not the case that either hb(r, W(r)) or that there exists a write w in A such that w.v = r.v and hb(W(r), w) and hb(w, r).

这基本上排除了它观察到的发生之前的情况.另一个规定只是一个理智的条款,它防止读取某些 v 的同时看到该 v 的较早写入,而随后又写入了相同的> v .

This basically precludes a read happening-before the write it observes. The other provision is just a sanity clause that prevents a read of some v seeing an earlier write of that v that was in the meantime followed by another write of the same v.

我找不到任何可以肯定地观察到写入的保证,只能观察到对写入内容的限制.

I cannot find any guarantee whatsoever that a write will positively be observed, only restrictions on what writes may not be observed.

我在这里想念什么?JVM是否真的有可能忽略了这样的琐碎保证?

What am I missing here? Is it really possible that the JVM leaves out such a trivial guarantee?

推荐答案

让我们使用:

class MyClass {
    private static int i = 0;

    public static void main(String[] args) {
        i = 3; //w
        System.out.println(i); //r
    }
}

  • 只有并且在所有顺序一致的执行都没有数据争用的情况下,程序才能正确同步.
  • 如果程序已正确同步,则该程序的所有执行将看起来是顺序一致的(第17.4.3节).
  • 当程序包含两个冲突访问(第17.4.1节)时,它们之间没有按事前发生的关系进行排序,则可以说它包含一个数据争用.

    • A program is correctly synchronized if and only if all sequentially consistent executions are free of data races.
    • If a program is correctly synchronized, then all executions of the program will appear to be sequentially consistent (§17.4.3).
    • When a program contains two conflicting accesses (§17.4.1) that are not ordered by a happens-before relationship, it is said to contain a data race.

      您的程序是单线程的
      =>根据程序顺序约束,我们有hb(w,r)
      =>已正确同步
      =>该程序的所有执行将看起来是顺序一致的.
      =>它将打印3

      Your program is single threaded
      => we have hb(w,r) from the program order constraint
      => it is correctly synchronized
      => all executions of the program will appear to be sequentially consistent.
      => it will print 3

      这篇关于Java内存模型是否可以保证线程内写入的可见性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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