20, reference atom: AtomicReference
ABA Atomic AtomicInteger class to talk about? Atomic update references know?
CAS cause problems with the ABA!
CAS algorithm is the premise: Remove the data in memory of a moment, compare and exchange! It is possible within this time difference data has been changed!
The following example:
package com.interview.concurrent.cas;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @author yangxj
* @description 描述:CAS带来的ABA问题
* @date 2020/2/25 17:10
*/
public class CasAbaQuestionDemo {
public static void main(String[] args) throws InterruptedException {
AtomicInteger atomicInteger = new AtomicInteger(5);
/**
* @description:A线程已经将5修改为其他数
* @author yangxj
* @date 2020/2/25 17:23
*/
new Thread(() -> {
atomicInteger.compareAndSet(5,200);
},"A").start();
/**
* @description:C线程是小偷,偷偷的改动数据,然后又改回原来的5
* @author yangxj
* @date 2020/2/25 17:23
*/
new Thread(() -> {
atomicInteger.compareAndSet(200,210);
atomicInteger.compareAndSet(210,5);
},"C").start();
/**
* @description:B线程希望将5修改为1024,
* 按照原理,期望值已经被A修改为200,B线程应该要修改失败,
* 但是出现了小偷C线程,它将期望值又改成了5,导致B线程能成功的修改,还发现不了C的存在
* @author yangxj
* @date 2020/2/25 17:21
*/
new Thread(() -> {
atomicInteger.compareAndSet(5,1024);
},"B").start();
//确保上面的三个线程都执行完
TimeUnit.SECONDS.sleep(5);
System.out.println(atomicInteger.get()); //1024
}
}
Despite the success of CAS operation! But this does not mean that the process is not a problem!
Solve this problem, we can use optimistic locking (add a version number) to solve, we can be resolved by reference to atomic time-stamped! Then explain for atomic reference.
Atomic reference AtomicReference:
With a version number, time stamp of atomic references, similar optimistic locking!
The following example:
package com.interview.concurrent.cas;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicStampedReference;
/**
* @author yangxj
* @description 描述
* AtomicReference 原子引用
* AtomicStampedReference 加了时间戳 类似于乐观锁! 通过版本号
* @date 2020/2/25 17:43
*/
public class CasAbaAtomicStampedReference {
public static void main(String[] args) throws InterruptedException {
AtomicStampedReference<Integer> atomicStampedReference = new AtomicStampedReference<>(5, 1);
/**
* @description:A线程已经将5修改为其他数
* @author yangxj
* @date 2020/2/25 17:23
*/
new Thread(() -> {
//获得版本号
System.out.println("A stamp 01=>"+atomicStampedReference.getStamp());
atomicStampedReference.compareAndSet(5,200,atomicStampedReference.getStamp(),atomicStampedReference.getStamp() + 1);
},"A").start();
/**
* @description:C线程是小偷,偷偷的改动数据,然后又改回原来的5
* @author yangxj
* @date 2020/2/25 17:23
*/
new Thread(() -> {
// 保证上面的线程先执行完毕!
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
//获得版本号
System.out.println("C stamp 01=>"+atomicStampedReference.getStamp());
atomicStampedReference.compareAndSet(200,210,atomicStampedReference.getStamp(),atomicStampedReference.getStamp() + 1);
System.out.println("C stamp 02=>"+atomicStampedReference.getStamp());
atomicStampedReference.compareAndSet(210,5,atomicStampedReference.getStamp(),atomicStampedReference.getStamp() + 1);
System.out.println("C stamp 03=>"+atomicStampedReference.getStamp());
},"C").start();
/**
* @description:B线程希望将5修改为1024,
* 期望值已经被A修改为200,由于添加了时间戳,B线程修改失败
* @author yangxj
* @date 2020/2/25 17:21
*/
new Thread(() -> {
// 保证上面的线程先执行完毕!
try {
TimeUnit.SECONDS.sleep(7);
} catch (InterruptedException e) {
e.printStackTrace();
}
//获得版本号
System.out.println("B stamp 01=>"+atomicStampedReference.getStamp());
boolean isSuccess = atomicStampedReference.compareAndSet(5, 1024, atomicStampedReference.getStamp(), atomicStampedReference.getStamp() + 1);
System.out.println("线程B执行成功了吗?" + isSuccess);
System.out.println("B 最新的stamp:"+atomicStampedReference.getStamp());
System.out.println("B 当前的最新值:"+atomicStampedReference.getReference());
},"B").start();
}
}
Thread B will fail to modify, run the effect is as follows:
ABA solve the problem: AtomicStampedReference