The only reason I can think of is that the person who wrote that code understood that only the array is volatile but not its content and added a = a
to enforce visibility of the writes performed on the array items.
It doesn't seem to change anything when I take it out.
The fact that it still works on your machine with your JVM does not mean that it would work on another machine with a different JVM. In theory, removing the a = a;
statement could lead to a never-ending while loop.
Side note
I would have thought that the Java Memory Model (JMM) could allow a JVM to ignore that statement, the same way that a synchronized(new Object())
can be ignored.
It seems however that it is not the case*:
You may have noticed that I did provide a way of getting a volatile write above with just arrays: by writing out a self-reference.
*Jeremy Manson is one of the authors of the JMM.