/**
* 验证指令重排的案例
* 顺序执行下,x和y不可能同时为0,如果出现了同时为0,则说明发生了指令重排。
*/
public class InstructionRearrangement {
static int a = 0, b = 0, x = 0, y = 0;
public static void main(String[] args) throws InterruptedException {
while (true) {
Thread one = new Thread(() -> {
a = 1;
x = b;
});
Thread other = new Thread(() -> {
b = 1;
y = a;
});
one.start();
other.start();
one.join();
other.join();
//没有指令重排,x和y不可能同时为0
if (x == 0 && y == 0) {
System.out.println("x=" + x + ",y=" + y);
}
a = 0;
b = 0;
x = 0;
y = 0;
}
}
}
java指令重排案例
最新推荐文章于 2024-05-28 14:25:37 发布