Ever used Java’s “volatile” field modifier? Here’s a good explanation of what it’s good for:
The takeaway for me was that for performance reasons, each thread has a local cached copy of the variables stored “main” memory. If you don’t use volatile, a given thread will look at its local cached copy instead of looking in main memory. So, if another thread happened to change the variable, the given thread would never see the change. I didn’t know that threads behaved this way.
To complicate things, it looks like thread behavior for volatile and synchronized is JVM dependent:
and flag dependent (using the -server vs. the -client flag yielded different results on the Sun JVM).
Keep this in mind when you are working on a multi-threaded application.