Is Java reordering affecting System.currentTimeMillis ()?

According to the Java memory model, instructions can be reordered as long as the execution is properly formed .

So wondering, is it possible that the following codes produce the following output?

[codes] [in the same topic]

long a = System.currentTimeMillis(); long b = System.currentTimeMillis(); long c = System.currentTimeMillis(); 

[exit]

 a == 10, b == 20, c == 15 

If this is not possible, then what does the JVM / implementation do to prevent this?

+15
java order java-memory-model
Dec 25 '14 at 3:42
source share
2 answers

Please read this question reordering instructions and occurs before the relationship in java .

I believe that if you are not in another thread, the result of any execution will always correspond to the order in your code. In this situation, since it is impossible to process it out of order, it should be good, even if your fields are visible to another thread.

+5
Dec 25 '14 at 4:01
source share

Because of the user's system call, compilers do not have to reorder them in a single thread. If this was not true, we could even experience reordering effects in System.out.println (independent values); I assume that access to the system / OS clock creates some correlation between these operations (always for the current thread), so theoretically there is some kind of relationship between them. The JVM probably addresses this issue and never reorders user system calls.

+1
Dec 25 '14 at 8:50
source share