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.
DevidcoffAsked:
Who is Participating?
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.

gheistCommented:
AtomicInteger is roughly equivalent to serialized access to resource.
0
DevidcoffAuthor Commented:
I would like it in details like comparing synchronization and   AtomicInteger.
0
dpearsonCommented:
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,

Doug
0

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
dpearsonCommented:
Think covered this pretty fully
0
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
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.