2020-08-16 java中如何验证指令重排序
public class Test1 {
private static int x =0, y =0;
private static int a =0, b =0;
public static void main(String[] args)throws InterruptedException {
int i =0;
for (;;){
i++;
x =0; y =0;
a =0; b =0;
Thread t1 =new Thread(new Runnable() {
public void run() {
// 这里这个时间根据自己电脑性能调整
shortWait(20000);
a =1;
x =b;
}
});
Thread t2 =new Thread(new Runnable() {
public void run() {
b =1;
y =a;
}
});
t1.start();
t2.start();
t1.join();
t2.join();
String result ="第" + i +"次 (" +x +"," +y +")";
if(x ==0 &&y ==0) {
System.out.println(result);
break;
}else {
System.out.println(result);
}
}
}
/**
* 等待一段时间,时间单位纳秒
* @param interval
*/
public static void shortWait(long interval){
long start = System.nanoTime();
long end;
do{
end = System.nanoTime();
}while(start + interval >= end);
}
}