Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

What is atomic operation? What are atomic classes in Java Concurrency API?

user-image
Question added by Mohd shahnawaz khan , Associate Project , Cognizant Technology Solution
Date Posted: 2014/02/17
Aasif Faheem Ansari
by Aasif Faheem Ansari , Sr. Principal Consultant , Accelalpha

Atomic operations are performed in a single unit of task without interference from other operations. Atomic operations are necessity in multi-threaded environment to avoid data inconsistency.

int++ is not an atomic operation. So by the time one threads read it’s value and increment it by one, other thread has read the older value leading to wrong result.

To solve this issue, we will have to make sure that increment operation on count is atomic, we can do that using Synchronization but Java5 java.util.concurrent.atomic provides wrapper classes for int and long that can be used to achieve this atomically without usage of Synchronization. Go to this article to learn more about Atomic operations are performed in a single unit of task without interference from other operations. Atomic operations are necessity in multi-threaded environment to avoid data inconsistency.

 

int++ is not an atomic operation. So by the time one threads read it’s value and increment it by one, other thread has read the older value leading to wrong result.

 

To solve this issue, we will have to make sure that increment operation on count is atomic, we can do that using Synchronization but Java5 java.util.concurrent.atomic provides wrapper classes for int and long that can be used to achieve this atomically without usage of Synchronization. Go to this article to learn more about atomic concurrent classes..

More Questions Like This

Do you need help in adding the right keywords to your CV? Let our CV writing experts help you.