Scenario analysis of ABA problem in Java CAS

Source: Internet
Author: User

The problem of CAs operation is mentioned, that is, before CAs a becomes B and a,cas or can be set successfully, what scenario will appear this problem? Looked up some information, found that in the following two cases of ABA problems will occur.

1.A at the beginning of the memory address is x, then invalid, there is assigned B, just the memory address is X, this time through the CAS operation, but set the success

  This situation is unlikely to happen in a language with GC, why? Java example, when performing a CAS operation, a A, a, a, a, a, a, a, the GC cannot release it, then a point of memory will not be freed, B will not be allocated to the same memory address, CAS failed. If the A object has been freed in a GC-free, then B is assigned a memory, CAS succeeds.

2. Thread 1 Prepares the value of the variable to be replaced by a B with CAs, before which thread 2 replaces the value of the variable by a with C, and C with a, and then thread 1 executes the CAS when the value of the variable is still a, so the CAS succeeds. But in fact the scene at this time is different from the original, despite the success of CAs, but there may be hidden problems. For example:

  Existing a one-way linked list implementation of the stack, the top of the stack is a, then thread T1 already know A.next B, and then want to use CAs to replace the top of the stack with B:head.compareandset (A, b); before T1 executes this instruction, thread T2 intervenes, A, B is out of the stack, then pushd, C, A. and Object B is in a free state at this point: at this point, the thread T1 perform the CAS operation, the detection finds that the stack top is still a, so the CAs succeeds, the stack top changes to B, but actually the b.next is null, where there is only a B element in the stack, and the list of C and D is no longer present in the stack, and the C, D lost it .

  The above is due to the problem of ABA problems, the implementation of various optimistic locks are usually used in version to mark the record or object, to avoid the problems caused by concurrency, in Java,atomicstampedreference<e> also achieve this role, It avoids the ABA problem by wrapping the tuple of [E,integer] into the OBJECT tag version stamp stamp, for example, the following code updates the atomic integer variable with an initial value of 100, respectively, using Atomicinteger and atomicstampedreference. , Atomicinteger will successfully perform CAS operations, and atomicstampedreference with version stamp will perform CAS failure for ABA issues

 PackageConcur.lock;ImportJava.util.concurrent.TimeUnit;ImportJava.util.concurrent.atomic.AtomicInteger;Importjava.util.concurrent.atomic.AtomicStampedReference; Public classABA {Private StaticAtomicinteger Atomicint =NewAtomicinteger (100); Private StaticAtomicstampedreference<integer> Atomicstampedref =Newatomicstampedreference<integer> (100, 0);  Public Static voidMain (string[] args)throwsinterruptedexception {Thread intT1=NewThread (NewRunnable () {@Override Public voidrun () {Atomicint.compareandset (100, 101); Atomicint.compareandset (101, 100);                }        }); Thread intT2=NewThread (NewRunnable () {@Override Public voidrun () {Try{TimeUnit.SECONDS.sleep (1); } Catch(interruptedexception e) {e.printstacktrace (); }                BooleanC3 = Atomicint.compareandset (100, 101);        System.out.println (C3); //true            }        });        Intt1.start ();        Intt2.start ();        Intt1.join ();                Intt2.join (); Thread refT1=NewThread (NewRunnable () {@Override Public voidrun () {Try{TimeUnit.SECONDS.sleep (1); } Catch(interruptedexception e) {e.printstacktrace (); } atomicstampedref.compareandset (100, 101, Atomicstampedref.getstamp (), Atomicstampedref.getstamp ()+1); Atomicstampedref.compareandset (101, 100, Atomicstampedref.getstamp (), Atomicstampedref.getstamp ()+1);                }        }); Thread refT2=NewThread (NewRunnable () {@Override Public voidrun () {intStamp =Atomicstampedref.getstamp (); System.out.println ("Before Sleep:stamp =" + stamp);//stamp = 0                Try{TimeUnit.SECONDS.sleep (2); } Catch(interruptedexception e) {e.printstacktrace (); } System.out.println ("After Sleep:stamp =" + Atomicstampedref.getstamp ());//stamp = 1                BooleanC3 = Atomicstampedref.compareandset (101, Stamp, stamp+1);        System.out.println (C3); //false            }        });        Reft1.start ();    Reft2.start (); }}

Scenario analysis of ABA problem in Java CAS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.