关于ABA问题,已经在上篇文章中,谈论了,有不清楚的同学,可以去阅读下,https://blog.csdn.net/wodetongnian/article/details/104044865,这里不再重复了啊,
CAS解决ABA方案是版本号,JDK1.5提供了AtomicStampedReference来解决。AtomicStampedReference通过包装类[E,Integer]的元组来对对象标记版本戳stamp,从而避免ABA问题。
AtomicStampedReference的compareAndSet()方法定义如下:
/**
* Atomically sets the value of both the reference and stamp
* to the given update values if the
* current reference is {@code ==} to the expected reference
* and the current stamp is equal to the expected stamp.
*
* @param expectedReference the expected value of the reference
* @param newReference the new value for the reference
* @param expectedStamp the expected value of the stamp
* @param newStamp the new value for the stamp
* @return {@code true} if successful
*/
public boolean compareAndSet(V expectedReference,
V newReference,
int expectedStamp,
int newStamp) {
Pair<V> current = pair;
return
expectedReference == current.reference &&
expectedStamp == current.stamp &&
((newReference == current.reference &&
newStamp == current.stamp) ||
casPair(current, Pair.of(newReference, newStamp)));
}
CAS有四个参数,分别是预期引用,新引用,预期时间戳,新时间戳。当预期引用==当前引用,预期时间戳==当前时间戳,新引用==当前引用,新时间戳==当前时间戳时,返回true。否则,通过Pair对象生成新的对象与当前pair对象替换。Pair为AtomicStampedReference的内部类,主要用来记录引用和时间戳,代码如下:
public class AtomicStampedReference<V> {
private static class Pair<T> {
final T reference;
final int stamp;
private Pair(T reference, int stamp) {
this.reference = reference;
this.stamp = stamp;
}
static <T> Pair<T> of(T reference, int stamp) {
return new Pair<T>(reference, stamp);
}
}
private volatile Pair<V> pair;
}
Pair记录着对象的引用和版本戳,版本戳为int型。同时Pair是一个不可变对象,其所有属性全部定义为final,对外提供一个of方法,该方法返回一个Pair对象。Pair对象定义为volatile,保证多线程环境下的可见性。
通过一个例子,可以看到AtomicStampedReference相比较AtomicInteger的优势,定义两个线程,线程1负责将100-->110-->100,线程2执行100-->120,查看两者的区别。
package com.concurrent.program;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicStampedReference;
public class AtomicStampedReferenceAndAtomicIntegerTest {
private static AtomicInteger atomicInteger = new AtomicInteger(100);
private static AtomicStampedReference atomicStampedReference = new AtomicStampedReference(100, 1);
public static void main(String[] args) throws InterruptedException {
//AtomicInteger
Thread at1 = new Thread(new Runnable() {
@Override
public void run() {
atomicInteger.compareAndSet(100, 110);
atomicInteger.compareAndSet(110, 100);
}
});
Thread at2 = new Thread(new Runnable() {
@Override
public void run() {
try {
TimeUnit.SECONDS.sleep(2);//at1执行完
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("AtomicInteger:" + atomicInteger.compareAndSet(100, 120));
}
});
at1.start();
at2.start();
at1.join();
at2.join();
Thread tsf1 = new Thread(new Runnable() {
@Override
public void run() {
try {
//让tsf2先获取stamp,导致预期时间戳不一致
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
atomicStampedReference.compareAndSet(100, 110, atomicStampedReference.getStamp(),
atomicStampedReference.getStamp() + 1);
atomicStampedReference.compareAndSet(110, 100, atomicStampedReference.getStamp(),
atomicStampedReference.getStamp() + 1);
}
});
Thread tsf2 = new Thread(new Runnable() {
@Override
public void run() {
int stamp = atomicStampedReference.getStamp();
try {
TimeUnit.SECONDS.sleep(2);//线程tsf1执行完
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("AtomicStampedReference:" + atomicStampedReference.compareAndSet(100, 120, stamp, stamp + 1));
}
});
tsf1.start();
tsf2.start();
}
}
执行结果如下图:
运行解决重新呈现了AtomicInteger调用compareAndSet()方法ABA问题,以及AtomicStampedReference调用compareAndSet()方法解决了ABA问题。