重排序
重排序,Reorder,指编译器和Java虚拟机通过改变程序的处理顺序来优化程序。
重排序能够被广泛应用于提高程序的性能。但是在运行单线程程序的时候,我们无法判断是否进行了重排序,因为虽然处理顺序改变了,但是规范上有很多限制可以避免程序出现运行错误。
public class TestReorder {
public static void main(String[] args){
final Something something = new Something();
//写数据线程A ----- 匿名内部类
new Thread(){
public void run(){
something.write();
}
}.start();
//读数据线程B ---- 匿名内部类
new Thread(){
public void run(){
something.read();
}
}.start();
}
}
class Something{
private int x = 0;
private int y = 0;
public void write(){
x = 100;
y = 50;
}
public void read(){
if(x < y){
System.out.println("x < y");
}else {
System.out.println("x > y");
}
}
}
主函数Main方法实例化一个Something类,并启动两个线程:写数据线程A和读数据线程B。在Something类中,write()方法为x赋值100,为y赋值50;read()方法通过比较x、y的大小,并输出结果。考虑,执行程序后,是否会输出“x < y”?
- 在Java内存模型中,由于重排序,可能会输出 “x < y”。
- 2.