gpt4 book ai didi

java - 关于重新排序 : Why this code throws RuntimeException despite using the volatile?

转载 作者:搜寻专家 更新时间:2023-10-31 19:59:35 25 4
gpt4 key购买 nike

public class ReOrdering implements Runnable {
int one, two, three, four, five, six;
volatile int volaTile;

@Override
public void run() {
one = 1;
two = 2;
three = 3;
volaTile = 92;
int x = four;
int y = five;
int z = six;
}
}

The assignments of one, two and three may be reordered, as long as they all happen before the volatile write. Similarly, the x, y, and z statements may be reordered as the volatile write happens before all of them. The volatile operation is often called a memory barrier. The happens before guarantee ensures that read and write instructions of volatile variables cannot be reordered across a memory barrier.

The happens before guarantee has another effect: When a thread writes to a volatile variable, then all other variables - including non-volatiles - changed by the thread before writing to the volatile variable are also flushed to main memory. When a thread reads a volatile variable it also reads all other variables - including non-volatile - that were flushed to main memory together with the volatile variable.

© Bruce Eckel“论 Java 8”

我一定是误解了什么,因为这段代码不是这样工作的

public class Reordering {
private int x;
private volatile int y;

public void writer() {
x = 1;
y = 2;
}

public void reader() {
if (y == 2) {
if(x == 0) {
// X assignment is happens-before for
// volatile Y assignment
// so X can't be 0 when Y equals 2
throw new RuntimeException();
}

x = 0;
y = 0;
}
}

public static void main(String[] args) {
Reordering reordering = new Reordering();

Thread thread = new Thread(() -> {
while (true) {
reordering.writer();
}
});

thread.setDaemon(true);
thread.start();

while (true) {
reordering.reader();
}
}
}

最佳答案

我认为,问题可能发生在编写者刚刚设置 x=1,而读者已经在 if block 中(在变量赋值之前):

reader             writer state
----------------- ------ --------
stops before x=0 x=1, y=2
x=1 x=1, y=2
x=0 x=0, y=2
y=0 x=0, y=0
y=2 x=0, y=2
reader will throw

关于java - 关于重新排序 : Why this code throws RuntimeException despite using the volatile?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51301372/

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