According the jdk ,the implementation of the method incrementAndGet() of AtomicInteger class is as follows:
public final int incrementAndGet() {
return unsafe.getAndAddInt(this, valueOffset, 1) + 1;
}
public final int getAndAddInt(Object var1, long var2, int var4) {
int var5;
do {
var5 = this.getIntVolatile(var1, var2);
} while(!this.compareAndSwapInt(var1, var2, var5, var5 + var4));
return var5;
}
What happens if the getIntVolatile(Object var1, long var2) method is replaced by getInt(Object var1, long var2) method?
What I think is that the getIntVolatile(Object var1, long var2) method is unnecessary, as the operated variable is a volatile variableļ¼the getInt(Object var1, long var2) is able to read the latest value.