instagram

Atomic and Non-Atomic Operations in Java

This article is all about atomic operations in java.

Atomic operations are take place in one step. Like read and write operation of variable. atomic operations cannot be interrupted and they are thread safe, In java read and write operations are atomic for all variables which are less or equal to 32 bit.

In some environments read and write operations for long and double are not atomic because they are using 64 bit, so in 32 bit OS, Read and Write will be done in two steps.

For example :

// It is atomic operation, It is take place in one step.
int i = 0; 

//It is not atomic operation. It is take please in three steps. 
i++;
/*
   1. Read value of i.
   2. Increment by 1.
   3. Write new value again in i.
*/
Share