could someone explain me about this code please?
public class Counter {
private AtomicInteger value = new AtomicInteger();
public int incrementLongVersion(){//THIS PART2!!!
int oldValue = value.get();
while (!value.compareAndSet(oldValue, oldValue+1)){
oldValue = value.get();
}
return oldValue+1;
}
}
i mean why can't i just change this code
!value.compareAndSet(oldValue, oldValue+1)
to
false
? I know there will be compile error statement never reached but why
!value.compareAndSet(oldValue, oldValue+1)
is not getting error? isn't it always false too right?
Or why can't I just clear that while loop and just return "oldValue+1"?
Thanks and regards.