I am just trying to understand... Will I achieve the same result using the below two approaches? I mostly see the first approach only. Is there anything wrong with the second approach? We can use AtomicInteger to achieve the same but just wanted to know. Can someone please clarify?
Approach#1
public class Counter {
private int counter = 0;
public int getCount() {
synchronized(this) {
return counter;
}
}
public void increment() {
synchronized(this) {
counter++;
}
}
}
Approach#2
public class Counter {
private volatile int counter = 0;
public int getCount() {
return counter;
}
public synchronized void increment() {
counter++;
}
}
Michael's comment is correct. Both approaches are fine.
In terms of performance, it is difficult to say which one is better. It might even depend on the architecture (x86 vs ARM).
The second one might be better for gets, but worse for increments (because both the lock is taken, and a write memory barrier is issued, unless JIT can optimize that away...)