I have read about ABA problem
in computer science(in concurrent environment)
Also I have read that this issue is not actual for languages with GC.
Now I am thinking about java atomics and I want to know if this problem prevented but I think this problem can occur.
lets research AtomicInteger
for example and java 6 implementation
each method looks approximately like this:
private volatile int value;
public int incrementAndGet(){
while(true){
int old = value; //1
int newValue = value+1;
if(compareAndSet(old, newValue)){ //2
return newValue;
}
}
}
looks like between //1
and //2
other threads can execute increment and decrement and this check will be added but accoring my inderstanding it is wrong and it is exhibition of ABA problem.
Or for example between //1
and //2
occured Integer.MAX_VALUE increments or ecrements, value overflowed but old value equally new value
Lets research scenario(Thred 1 and Thread 2 make increment, but Thread 3 - decrement):
Thread 1 calls get and gets the value 1.
Thread 1 calculates next to be 2.
Thread 2 calls get and gets the value 1.
Thread 2 calculates next to be 2.
Thread 2 invoke compareAndSet and get success
Thread 3(decrement thread) calls get and gets the value 2.
Thread 3 invoke compareAndSet and get success
Thread 1 invoke compareAndSet and get success
P.S.
from: http://www.ibm.com/developerworks/library/j-jtp11234/
The ABA problem Because CAS basically asks "is the value of V still A" before changing V, it is possible for a CAS-based algorithm to be confused by the value changing from A to B and back to A between the time V was first read and the time the CAS on V is performed. In such a case, the CAS operation would succeed, but in some situations the result might not be what is desired. (Note that the counter and mutex examples from Listing 1 and Listing 2 are immune to this problem, but not all algorithms are.) This problem is called the ABA problem, and is generally dealt with by associating a tag, or version number, with each value to be CASed, and atomically updating both the value and the tag. The AtomicStampedReference class provides support for this approach.
Please, share your thoughts.