A blog about programming topics, mostly JVM, Linux kernel, and x86 related things.

Tuesday, December 16, 2008

Memory Models, Multiprocessors, and Java

In my experience, there are still a lot of Java programmers out there that are not aware of the implications of weak memory models when programming on multiprocessor machines. It's not at all uncommon to hear arguments such as "I don't need locking here because I'm just reading the value" because programmers are not aware of the reasons behind the "new" Java memory model specification that appeared in Java 5.

Of course, the simplest way to avoid these problems is to always use Java locking primitives (e.g. the synchronized keyword) whenever you're writing multi-threaded programs. After all, most of the Java programs that are being written today don't require hand optimization of concurrency or other low-level programming tricks. However, if you do need to optimize at that level or you're not quite convinced there is a problem, Bartosz Milewsk has written two excellent blog posts on the subject which are a must read for anyone serious about multi-threaded programming with Java.

And just in case you're interested in diving into the topic a bit more, there's a Google Tech Talk by Richard L. Hudson available on YouTube that discusses the x86 memory model.