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

问题描述

我正在观看来自Java jpoint会议的视频.

I am watching video from java jpoint conference.

我对以下 Alexey Shipilev 报告中的幻灯片有疑问:

I have question about following slide from Alexey Shipilev report:

不好意思,请不要错过幻灯片.实际上,作者说变量集不可能是

Excuse me for non-english on slide. Actually author says that it is impossible that variable set will be

r1 = 1 (Y)
r2 = 0 (x)
r3 = 1 (x)
r4 = 0 (Y)

根据视频,他暗示这显然是

According the video he implies that it is obviously.

有人可以澄清为什么JMM无法设置此值吗?

Can someone clarify why this value set impossible according JMM?

PS

如果我理解正确的Alexey表示法,则表示遵守以下代码:

If I understand Alexey notation correct it respects the following code:

public class SequentialConsistency {
    static volatile int x;
    static volatile int y;

    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                x = 1;
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                y = 1;
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("r1=" + x + ", r2=" + y);
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("r3=" + x + ", r4=" + y);
            }
        }).start();
    }
}

推荐答案

您可以为此代码构造SC执行的详尽列表,而不会实现SC执行收益(1、0、1、0).

You can construct exhaustive list of SC executions for this code, and realize no SC execution yields (1, 0, 1, 0).

在模型方面,这很容易争论.同步顺序(SO)一致性表示同步读取应该看到SO中的最后一个同步写入. SO-PO的一致性表示SO应该与程序顺序保持一致.

Model-wise, it is very easy to argue about. Synchronization order (SO) consistency says that synchronized reads should see the last synchronized write in SO. SO-PO consistency says SO should be consistent with program order.

这允许通过矛盾来勾画证明.假设产生(1,0,1,0)的执行.然后,在这些执行中,由于SO的一致性,看到的零必须按此顺序排列:

This allows to sketch the proof by contradiction. Suppose the execution that yields (1, 0, 1, 0) exists. Then, in those executions read that see zeroes must be in this order, due to SO consistency:

(r2 = x):0 --so--> (x = 1)  [1]
(r4 = y):0 --so--> (y = 1)  [2]

...并且其他两个读取必须按此顺序进行写入才能看到它们(由于SO的一致性):

...and the other two reads must be in this order with writes to see them (due to SO consistency):

(x = 1) --so--> (r3 = x):1  [3]
(y = 1) --so--> (r1 = y):1  [4]

...以及由于SO-PO的一致性:

...and further, due to SO-PO consistency:

(r1 = y):1 --po--> (r2 = x):0  [5]
(r3 = x):1 --po--> (r4 = y):0  [6]

这会产生奇怪的循环传递SO:

This yields weird transitive SO that is cyclic:

(r2 = x):0 --so--> (r3 = x):1 --so--> (r4 = y):0 --so--> (r1 = y):1 --so--> (r2 = x):0
            [1,3]               [6]               [2,4]               [5]

请注意,对于上面执行中的任何一对动作A!= B,我们可以说(A --so--> B)(B --so--> A) -这称为对称性.根据定义,SO是总阶,而总阶是反对称,这里我们有一个对称的.我们已经陷入矛盾,因此不存在这种处决. Q.E.D.

Notice that for any pair of actions A != B in the execution above, we can say (A --so--> B) and (B --so--> A) -- this is called symmetry. By definition, SO is total order, and total order is antisymmetric, and here we have the symmetric one. We have arrived to contradiction, and therefore such execution does not exist. Q.E.D.

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

09-02 15:25