Does the atomic classes like AtomicInteger answered both the race condition and visibility in multi threaded environment

I want to know how atomic classes like AtomicInteger  can be used instead of synchronization in java. How it answered both the race condition and visibility in multi threaded environment.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AtomicInteger is roughly equivalent to serialized access to resource.
DevidcoffAuthor Commented:
I would like it in details like comparing synchronization and   AtomicInteger.
Logically AtomicInteger functions as if you had written:

public class MyAtomicInteger() {
   private int m_value ;

   public synchronized setValue(int newValue) {
        m_Value = newValue ;

   public synchronized int getValue() {
       return m_Value ;

However, AtomicInteger is potentially faster, because it can use specific operations on modern CPUs that allow for "test-and-set" which means they only change the value of a memory location when it has the value they expect (so another thread cannot change it in the middle of the operation).

You shouldn't however need to dive into that.  They key is that they are logically equal to synchronize but may be faster.

What do synchronize (and AtomicInteger) both promise:
1) That 2 threads cannot modify the same shared location at the same time - which avoids some of the race conditions you are talking about.  This is based on the idea that only once thread can be executing inside a piece of synchronized code at the same time.
2) That 2 threads will both "see" the newly modified value after it has been changed.  This is to do with how cached memory values become visible between threads and again both synchronized and AtomicInteger guarantee that threads can't keep data in local caches, they instead have to publish it so that other threads can see it (which is what you want).

Hope that helps,


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Think covered this pretty fully
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.