Monday, October 29, 2012

What is Atomic Operation in java?

Atomic means each action take place in one step without interruption or we can say that operation is performed as a single unit of work without the possibility of interference from other operations.
An Atomic operation cannot stop in the middle, either it happened completely or doesn't happen at all.

Java language specification guarantees that
  • Reading or writing of a variable/reference is atomic unless the variable is of type long or double.
  • Read and write are atomic for all variable declared volatile including long and double variables.

Atomic action can be used without fear of thread interference.
Using simple atomic variable access is more efficient than accessing the same variable through synchronized code. But it require more  care and attention from programmer to avoid memory consistency .
Operation i++;

This operation is not atomic as it happens in 3 steps
  1. Reading the current value of i
  2. Increment the current value of i
  3. Writing the new value of i

Since java 1.5, java language provides atomic variable e.g. AtomicInteger or AtomicLong which provides methods like
getAndDecrement(),getAndIncrement() and getAndSet() in java.util.concurrent.atomic package which are all atomic.


Counter class is designed so that invocation of increment () add 1 to the variable c and decrement () subtract 1 from c.
If Counter object is reference from multiple threads, interference b/w threads may prevent from happening as expected.

Suppose 2 thread are accessing the Counter object
  1. Thread A: Retrieve c.
  2. Thread B: Retrieve c.
  3. Thread A: Increment retrieved value; result is 1.
  4. Thread B: Decrement retrieved value; result is -1.
  5. Thread A: Store result in c; c is now 1.
  6. Thread B: Store result in c; c is now -1.

Thread A result is lost and overwritten by Thread B.
Under different circumstances, it might be Thread B result is lost and overwritten by Thread B, or there could be no error.

If you know anyone who has started learning Java, why not help them out! Just share this post with them. 
Thanks for studying today!...


  1. Explanation is clear and easy to understand.

  2. I am technology Enthusiast. Your blog is really awesome, attractive and impressive. I like the way you think. it is very useful for Java SE & Java EE Learners. Your article adds best knowledge to our Java Online Training in India. or learn thru Java Online Training in India Students. or learn thru JavaScript Online Training in India. Appreciating the persistence you put into your blog and detailed information you provide. Kindly keep blogging.

  3. The information which you have provided is very good. It is very useful who is looking for Java online training

  4. All are saying the same thing repeatedly, but in your blog I had a chance to get some useful and unique information,
    I love your writing style very much, I would like to suggest your blog in my dude circle, so keep on updates.
    Java training in Btm layout
    Java training in Jaya nagar
    Java training in Electronic city
    Java training in Chennai
    Java training in USA