This was a talk given by the London JUG leaders. They told us how the JVM is getting closer to the harware for better performance, using specialised registers for instance.
In this talk, I learned that the JMM (Java Memory Model) is the best remedy against insomnia. They also showed a small puzzle, to which even Heinz Kabutz, sitting in the front row, did not know the answer. By the way, I was sitting just behind him, and I could see that he was writing the demo program for his talk - a real specialist in multitasking!
Back to the puzzle, here is the code (at least what I remember of it):
publicclass Test {privateint i;privatevolatile j;publicvoid setup() {
i = 1;
j = 1;
}publicvoid print() {
System.out.println("i=" + i + " j=" + j);
}
}
The question is what happens if one thread calls the setup method, then another thread calls the print method. According to the JMM, j will always be 1, while i will be either 0 or 1. In practice, since they are in the same cache line, they will be both 1.