Today I read the answers to the questions about the java interview, and I read this question: Question. Consider the following piece of Java code that initializes two variables, both of which are not volatile, and the two threads T1 and T2 change these values as follows: both are not synchronized
int x = 0;
boolean bExit = false;
Thread 1 (not synchronized)
x = 1;
bExit = true;
Thread 2 (not synchronized)
if (bExit == true)
System.out.println("x=" + x);
Now tell us, is it possible for Thread 2 to print "x = 0"?
So the answer is yes. The explanation is "because without any instruction for the compiler, for example, synchronized or volatile, bExit = true may appear before x = 1 in the reordering of the compiler." Before that, I do not know that the compiler can execute one line before another line after it.
? , - - , , , ( )? ( , , ). - - ?