Quantcast
Channel: AtomicInteger vs synchronized getters/setters - Stack Overflow
Viewing all articles
Browse latest Browse all 3

AtomicInteger vs synchronized getters/setters

$
0
0

Is this class thread-safe?

Is it possible to see inconsistent values? Lets say initially a's value is 80. Thread 1 calls setA(100) and enters the function but did not yet call a.set(100) and Thread 2 concurrently calls getA(). Is it possible for Thread 2 to see 80?

public class A {    private AtomicInteger a;     public int getA() {        return a.get()    }    public void setA(int newVal){        a.set(newVal);    }   }

I know that synchronizing it will guarantee thread 2 sees 100, but not sure with AtomicInteger.


Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images