Java multi-threading & Safe Publication [closed]

Proportionally, it’s probably fair to say that very few programmers sufficiently understand synchronization and concurrency. Who knows how many server applications there are out there right now managing financial transactions, medical records, police records, telephony etc etc that are full of synchronization bugs and essentially work by accident, or very very occasionally fail (never heard of anybody get a phantom phone call added to their telephone bill?) for reasons that are never really looked into or gotten to the bottom of.

Object publication is a particular problem because it’s often overlooked, and it’s a place where it’s quite reasonable for compilers to make optimisations that could result in unexpected behaviour if you don’t know about it: in the JIT-compiled code, storing a pointer, then incrementing it and storing the data is a very reasonable thing to do. You might think it’s “evil”, but at a low level, it’s really how you’d expect the JVM spec to be. (Incidentally, I’ve heard of real-life programs running in JRockit suffering from this problem– it’s not purely theoretical.)

If you know that your application has synchronization bugs but isn’t misbehaving in your current JVM on your current hardware, then (a) congratulations; and (b), now is the time to start “walking calmly towards the fire exit”, fixing your code and educating your programmers before you need to upgrade too many components.

Leave a Comment