modelo de memoria y reordenación de java

I am seeing this in the java specs:

If x and y are actions of the same thread and x comes before y in program order, then x happens before y.

y también esto

código original
1 hilo
r2 = A;
B = 1;

valid compiler transformation(compilers are allowed to reorder the instructions in either thread, when this does not affect the execution of that thread in isolation)
1 hilo
B = 1;
r2 = A;

I am confused with those two things.
if an action x comes before an action y then x should happen before y. if we consider r2=A for x and B=1 for y, r2=A should happen before B=1. How can there be any reordering, how come B=1 is executed before r2=A if x happens before y is true?.

preguntado el 03 de mayo de 12 a las 22:05

1 Respuestas

Sección 17.4.5 de la JLS específicamente brings this out:

Cabe señalar que la presencia de una relación de suceso anterior entre dos acciones no implica necesariamente que tengan que tener lugar en ese orden en una implementación. Si la reordenación produce resultados consistentes con una ejecución legal, no es ilegal.

If it helps, replace "happens-before" with "wurfles" everywhere in the spec, so that your intuición about what it means doesn't come into play. You're expecting guarantees which aren't present in the spec - due to the naming, I suspect.

contestado el 03 de mayo de 12 a las 22:05

Based on what the JLS says : The JIT decides that a sucede antes is not needed if there is no dependency between two events (if they are independent) right?. This paragraph is pointing to JIT optimizations correct? - La mente perdida

@TheLostMind: Well, either JIT optimizations or just the JIT not la eliminación de CPU optimizations - jon skeet

En ese caso, finalmente, pasa antes doesn't exist. I smell the hand of análisis de escape in this one :P - La mente perdida

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.