gpt4 book ai didi

java - 读取新值后读取旧值

转载 作者:行者123 更新时间:2023-12-02 19:06:29 26 4
gpt4 key购买 nike

考虑这个例子。我们有:

int var = 0;

线程A:

System.out.println(var);
System.out.println(var);

线程B:

var = 1;

线程同时运行。以下输出可能吗?

1
0

即读取新值后读取原值。 var不是不稳定的。我的直觉是这是不可能的。

最佳答案

您使用的System.out.println在内部执行synchronized(this) {...},这会让事情变得更糟。但即便如此,您的阅读器线程仍然可以观察 1, 0,即:一次生动的阅读。

到目前为止,我还不是这方面的专家,但在浏览了 Alexey Shipilev 的大量视频/示例/博客后,我想我至少了解了一些内容。

JLS states那:

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

由于var的两次读取都是按照程序顺序进行的,我们可以得出:

                (po) 
firstRead(var) ------> secondRead(var)
// po == program order

这句话还表明这会构建一个 happens-before 订单,因此:

                (hb) 
firstRead(var) ------> secondRead(var)
// hb == happens before

但那是在“同一线程”内。如果我们想推理多个线程,我们需要查看 synchronization order 。我们需要它,因为关于 happens-before order 的同一段落说:

If an action x synchronizes-with a following action y, then we also have hb(x, y).

因此,如果我们在程序顺序与顺序同步之间构建此操作链,我们就可以推断结果。让我们将其应用到您的代码中:

            (NO SW)                    (hb)
write(var) ---------> firstRead(var) -------> secondRead(var)

// NO SW == there is "no synchronizes-with order" here
// hb == happens-before

这就是 same chapter发生在一致性之前发挥作用的地方。 :

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).

In a happens-before consistent set of actions, each read sees a write that it is allowed to see by the happens-before ordering

我承认我对第一句话的理解非常模糊,这是 Alexey 对我帮助最大的地方,正如他所说:

Reads either see the last write that happened in the happens-before or any other write.

因为那里没有synchronizes-with order,并且隐含地没有happens-before order,所以允许读取线程通过竞争进行读取。从而得到1,而不是0


一旦引入正确的与订单同步for example one from here

An unlock action on monitor m synchronizes-with all subsequent lock actions on...

A write to a volatile variable v synchronizes-with all subsequent reads of v by any thread...

图表发生变化(假设您选择使 var volatile ):

               SW                       PO
write(var) ---------> firstRead(var) -------> secondRead(var)

// SW == there IS "synchronizes-with order" here
// PO == happens-before

PO(程序顺序)通过我在 JLS 的答案中引用的第一句话给出了 HB(之前发生)。 SW 给出 HB 因为:

If an action x synchronizes-with a following action y, then we also have hb(x, y).

因此:

               HB                       HB
write(var) ---------> firstRead(var) -------> secondRead(var)

现在happens-before order表示读取线程将读取“最后一个HB中写入的值”,或者意味着读取1然后0 是不可能的。


我举了例子jcstress samples并引入了一个小更改(就像您的 System.out.println 所做的那样):

@JCStressTest
@Outcome(id = "0, 0", expect = Expect.ACCEPTABLE, desc = "Doing both reads early.")
@Outcome(id = "1, 1", expect = Expect.ACCEPTABLE, desc = "Doing both reads late.")
@Outcome(id = "0, 1", expect = Expect.ACCEPTABLE, desc = "Doing first read early, not surprising.")
@Outcome(id = "1, 0", expect = Expect.ACCEPTABLE_INTERESTING, desc = "First read seen racy value early, and the second one did not.")
@State
public class SO64983578 {

private final Holder h1 = new Holder();
private final Holder h2 = h1;

private static class Holder {

int a;
int trap;
}

@Actor
public void actor1() {
h1.a = 1;
}

@Actor
public void actor2(II_Result r) {
Holder h1 = this.h1;
Holder h2 = this.h2;

h1.trap = 0;
h2.trap = 0;

synchronized (this) {
r.r1 = h1.a;
}

synchronized (this) {
r.r2 = h2.a;
}

}

}

请注意 synchronized(this){....} 不是初始示例的一部分。即使进行同步,我仍然可以看到结果 1, 0。这只是为了证明即使使用 synchronized (来自 System.out.println 内部),您仍然可以获得 1 而不是 0.

关于java - 读取新值后读取旧值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64983578/

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