Java指令重排序。正常情况下,以下代码无论线程t1与线程t2谁先执行,a,b同时为0应该都不是不可能的。a,b同时为0,只有一种情况,那就是代码指令被CPU重排序。两个线程的代码执行顺序是这样的。
1、a=y,
2、b=x,
3、y=1,
4、x=1
/**
* @author lucas
* @program java-test
* @description 测试类
* @createDate 2020-12-29 11:31:23
* @slogan 别人笑我太疯癫,我笑他人看不穿; 长风破浪会有时,直挂云帆济沧海。
**/
public class Main {
static int a,b,x,y;
public static void main(String[] args) throws InterruptedException {
Set<String> stringSet = new HashSet<>();
while (true){
a=0;
b=0;
x=0;
y=0;
Thread t1 = new Thread() {
@Override
public void run() {
x = 1; // 1
a = y; // 4
}
};
Thread t2 = new Thread() {
@Override
public void run() {
y=1; //2
b=x; //3
}
};
t1.start();
t2.start();
t1.join();
t2.join();
stringSet.add("a:"+a+",b:"+b);
System.out.println(stringSet);
if(a == 0 && b ==0){
break;
}
}
}
}