No it doesn't.
Suppose two threads T1
and T2
and retryCount
contains actually the 2
value.
Suppose T1
executes if(retryCount.get() < MAX_COUNT){
(evaluated to true) but doesn't reach retryCount.getAndIncrement();
.
T1
is paused. T2
is resumed.
T2
executes if(retryCount.get() < MAX_COUNT){
that is still evaluated to true.
So you are sure that retryCount
will be valued to 4
.
You need explicit synchronization and in this case the AtomicInteger
may not be required :
synchronized(lock){
if(retryCount.get() < MAX_COUNT){
// some other code
retryCount.getAndIncrement();
}else{
// reset count & some other code
retryCount.set(0);
}
}