Wednesday, March 27, 2013

AtomicFieldUpdater vs. Atomic

Java 1.5 introduced new family of classes (Atomic*FieldUpdater) for atomic updates of object fields with properties similar to Atomic* set of classes and it seems like there is slight confusion about the purpose of these. And that confusion is understood, the reason for their existance is not very obvious. First of all they are no way faster than Atomics, if you look at source, you see that there are lots of access control checks. Then, they are not handy - developer has to write more code, understand new API, etc.

So why would you bother? There are two main use cases when Atomic*FieldUpdater can be considered an an option:

  • There is a field which is mostly read and rarely changed. In that case, volatile field can be used for read access and Atomic*FieldUpdater for ocasional updates. Thought, that optimization is arguable, because there is a good chance that in latest JVMs Atomic*.get() is intrinsic and should not be slower than volatile.
  • Atomics have much higher overhead on memory usage than primitives. In cases when memory is critical Atomic can be replaced with volatile primitive with Atomic*FieldUpdater.

References:
http://concurrency.markmail.org/message/ns4c5376otat2p54?q=FieldUpdater
http://concurrency.markmail.org/message/mpoy74yhuwgi52fa?q=FieldUpdater

1 comment:

Blogger said...

Do you realize there's a 12 word sentence you can communicate to your man... that will induce deep emotions of love and instinctual appeal to you buried inside his chest?

Because deep inside these 12 words is a "secret signal" that triggers a man's impulse to love, adore and protect you with all his heart...

12 Words That Fuel A Man's Love Response

This impulse is so hardwired into a man's genetics that it will make him try better than ever before to love and admire you.

Matter-of-fact, triggering this dominant impulse is absolutely binding to getting the best possible relationship with your man that the instance you send your man one of these "Secret Signals"...

...You will soon notice him expose his mind and soul for you in such a way he never experienced before and he will perceive you as the only woman in the universe who has ever truly understood him.