2. Atomic -- atomicstampedreference solves the ABA Problem

Source: Internet
Author: User
Atomicstampedreference solves the ABA Problem

There is a classic ABA problem in the lock-free operation using CAS:

Thread 1 is going to use CAs to replace the value of a with B. Before that, thread 2 replaces the value of a with C and C with, when thread 1 executes cas, it finds that the variable value is still a, so CAS is successful. However, the scene is actually different from the original scene. Although CAS is successful, there may be hidden problems, for example, the following example:


There is a stack implemented using a one-way linked list with the stack at the top of a. At this time, thread T1 already knows that a. Next is B, and then hopes to replace the stack with B with CAS:

Head. compareandset (A, B );

Before T1 executes the preceding command, thread T2 intervene to take a and B out of the stack, and then pushd, C, and A. The stack structure is as follows, while object B is in the Free State at this time:


At this time, it is the turn of thread T1 to execute the CAS operation. The detection finds that the top of the stack is still a, so CAS is successful, and the top of the stack is changed to B, but in fact B. Next is null, so the situation changes:


There is only one element in the stack, and the linked list composed of C and D no longer exists in the stack. C and D are lost without reason.

The above is the hidden danger caused by the ABA problem. The implementation of various optimistic locks usually uses the version stamp to mark records or objects to avoid problems caused by concurrent operations. In Java, atomicstampedreference <E> can also be used to mark the version Stamp of an object by packaging the tuples of [E, integer] To avoid ABA problems, for example, the following code uses atomicinteger and atomicstampedreference to update the atomic integer variable whose initial value is 100. atomicinteger performs the CAS operation successfully, the atomicstampedreference with the version stamp fails to execute CAS for the ABA problem:

public class Test {private static AtomicInteger atomicInt = new AtomicInteger(100);private static AtomicStampedReference atomicStampedRef = new AtomicStampedReference(100, 0);public static void main(String[] args) throws InterruptedException {Thread intT1 = new Thread(new Runnable() {@Overridepublic void run() {atomicInt.compareAndSet(100, 101);atomicInt.compareAndSet(101, 100);}});Thread intT2 = new Thread(new Runnable() {@Overridepublic void run() {try {TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {}boolean c3 = atomicInt.compareAndSet(100, 101);System.out.println(c3); // true}});intT1.start();intT2.start();intT1.join();intT2.join();Thread refT1 = new Thread(new Runnable() {@Overridepublic void run() {try {TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {}atomicStampedRef.compareAndSet(100, 101,atomicStampedRef.getStamp(),atomicStampedRef.getStamp() + 1);atomicStampedRef.compareAndSet(101, 100,atomicStampedRef.getStamp(),atomicStampedRef.getStamp() + 1);}});Thread refT2 = new Thread(new Runnable() {@Overridepublic void run() {int stamp = atomicStampedRef.getStamp();try {TimeUnit.SECONDS.sleep(2);} catch (InterruptedException e) {}boolean c3 = atomicStampedRef.compareAndSet(100, 101, stamp,stamp + 1);System.out.println(c3); // false}});refT1.start();refT2.start();}}




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.