What is reordering in java ?
When you write a program you expect instructions will be executed in same order as written in the program . i.e. execution order will remain same as the order of instructions written in the program.
But that is not the case
Compiler is free to execute the instructions in order it desire . For Example :
Public class Order {
int a=8;
int b=6;
}
Here compiler might execute b=6 prior to executing a=8 . Thus the values for a and be might get flushed as b=6 and a=0 (default value) and a thread reading values at that point of time might read value as b=6 and a=0.
Compiler is absolutely free to conspire and reorder the execution flow .
Why does compiler do this?
Compiler do this to achieve overall optimization . Here a and b are independent of each other So compiler might think it hardly matters of a=8 executed first or b=6 executed first . But for overall optimization executing b-6 and delaying execution of a=8 provides better performance.
Based on latest java memory model , compiler follow reordering more seriously for better performance and optimization .
There are techniques available in Java to restrict this ordering. Synchronization is one of the finest example of applying that.
When you write a program you expect instructions will be executed in same order as written in the program . i.e. execution order will remain same as the order of instructions written in the program.
But that is not the case
Compiler is free to execute the instructions in order it desire . For Example :
Public class Order {
int a=8;
int b=6;
}
Here compiler might execute b=6 prior to executing a=8 . Thus the values for a and be might get flushed as b=6 and a=0 (default value) and a thread reading values at that point of time might read value as b=6 and a=0.
Compiler is absolutely free to conspire and reorder the execution flow .
Why does compiler do this?
Compiler do this to achieve overall optimization . Here a and b are independent of each other So compiler might think it hardly matters of a=8 executed first or b=6 executed first . But for overall optimization executing b-6 and delaying execution of a=8 provides better performance.
Based on latest java memory model , compiler follow reordering more seriously for better performance and optimization .
There are techniques available in Java to restrict this ordering. Synchronization is one of the finest example of applying that.
No comments:
Post a Comment