Java Concurrency: Atomic Variables

In today’s blog, we will be discussing and understanding the use of atomic variables with regards to concurrency in Java. But before understanding atomic variables, let’s understand what do we mean by atomic or atomicity.

Atomicity

Atomic operations are those operations that ALWAYS execute together. Either all of them execute together, or none of them executes. If an operation is atomic, then it cannot be partly complete, either it will be complete, or not start at all, but will not be incomplete.

Consider the following example –

In the above example, we are writing value 1 to an int i. This is an atomic operation, since this will either happen altogether, assigning 1 to i, or won’t happen at all. This property of an operation, or a set of operations, being atomic is known as atomicity.

Now, consider the following example –

At first glance, this might seem like an atomic operation, but it is actually not. This one liner actually consists of 3 operations.

  • Read operation, where the value of i is read.
  • Modify operation, where a new value is being calculated (i + 1).
  • Write operation, where the new value is written to the variable i.

Now, since these operations are separate, these set of operations are currently not atomic, since it is possible for them to partially execute. This might be a little hard to understand in a single-threaded environment, but just imagine running this piece of code on multiple threads. If we call this method 2 times on a single thread, it will result in i being equal to 2. But imagine calling it on 2 threads. The result should ideally remain same, but if we call this method on 2 threads at the same time, then thread A and B will both read the value at the same time and update the value at the same time as well, resulting i to be equal to 1. This is happening because the set of operations here are not atomic. Let’s see how we can make these set of operations atomic.

Atomic Variables

Atomic variables come to the rescue in these types of situations. Atomic variables allow us to perform atomic operations on the variables.

Consider the following example –

The above example shows how an AtomicInt can be used to update the value atomically. What incrementAndGet() does is atomically increment the value by 1, and then returns the updated value. If the program is now run in a multi-threaded environment, supposing with 2 threads, then the end result will always be i being equal to 2. This is because no matter which thread gets to the incrementAndGet() method first, since it is an atomic operation, the thread will update the value of i to 1, and only then another thread will be able to access or update it, which will make the value of i to 2, thus giving us the correct result.

The most commonly used atomic classes are – AtomicInt, AtomicLong, AtomicBoolean and AtomicReference. All of these provide atomic operations for the respective classes. AtomicReference can be used for just any type of object.

Conclusion

When programming in a multi-threaded environment, we need to avoid situations in which concurrent execution of a set of operations may lead to incorrect or unexpected behaviour. So, we need to make these set of operations atomic. For operations on a single variable, we can achieve this by using the atomic variable classes, which offers us atomic operations on the variables, thus achieving correct behaviour in a multi-threaded environment.

I hope this blog was helpful to you, and you learnt something from it. I will be writing more blogs regarding Java concurrency as well. Till then, happy blogging!


knoldus-advt-sticker

Written by 

Akshansh Jain is a Software Consultant having more than 1 year of experience. He is familiar with Java but also has knowledge of various other programming languages such as scala, HTML and C++. He is also familiar with different Web Technologies and Android programming. He is a passionate programmer and always eager to learn new technologies & apply them in respective projects.

Leave a Reply

Knoldus Pune Careers - Hiring Freshers

Get a head start on your career at Knoldus. Join us!