CAS操作(原子操作,底層使用處理器的CAS指令),內部通過樂觀鎖(舊值和新值比較或版本號比較)實現,
缺點:
ABA問題:值改成其他值後又改回來了,從而不知道是否改過(只控制結果,不能夠控制過程)。解決方案添加版本號(默認舊值和新值進行比較),JDK 提供AtomicMarkableReference,AtomicStampedReference。
AtomicMarkableReference<V> ,可以控制ABA問題,保證是否修改過
AtomicStampedReference<V> 帶時間版本戳的原子操作類。解決ABA問題,保證到修改了幾次。
CPU開銷問題:JDK執行器對於一個CAS指令內部是個死循環,最後更新成功則退出。
只能保證管理一個共享變量:不能控制多個變量。解決方案 多個變量封裝到一個類中,使用AtomicReference。
- 1、單值操作 AtomicInteger AtomicBoolean AtomicLong
public static void main(String[] args) {
AtomicInteger atomicInteger=new AtomicInteger(3);
int andIncrement = atomicInteger.getAndIncrement();
int i = atomicInteger.incrementAndGet();
//加指定值
int i1 = atomicInteger.addAndGet(3);
//比較期望值,是則更新成功
boolean b = atomicInteger.compareAndSet(3, 5);
System.out.println("andIncrement = " + andIncrement+",i="+i+",i1="+i1+",b="+b+",atomicInteger="+atomicInteger);
//andIncrement = 3,i=5,i1=8,b=false,atomicInteger=8
}
- 2、數組操作 AtomicIntegerArray,AtomicLongArray,AtomicDoubleArray
Atomic中的數組和外面數組不是一個對象;
對數組的操作只能是修改值,不能夠向數組中添加新值(索引超出異常);
public static void main(String[] args) throws InterruptedException {
int[] array=new int[]{1,2,3};
//外面的array和內部array不是同一個,是通過克隆的
AtomicIntegerArray atomicIntegerArray=new AtomicIntegerArray(array);
atomicIntegerArray.incrementAndGet(0);
atomicIntegerArray.addAndGet(2,5);
atomicIntegerArray.set(2,4);
System.out.println(atomicIntegerArray);
//[2, 2, 4]
}
- 3、AtomicReference 引用類型CAS使用,AtomicStampedReference 版本號控制和AtomicMarkableReference 標誌控制
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicReference;
/**
* @ClassName ReferenceTest
* @Description 多個變量需要同時Atomic控制時,封裝到一個對象類中 AtomicReference解決。一個對象更新成另一個對象
**/
public class ReferenceTest {
public static void main(String[] args) throws InterruptedException {
MiddleStatus status=new MiddleStatus(10,5);
AtomicReference<MiddleStatus> ato=new AtomicReference<>(status);
MiddleStatus expect=new MiddleStatus(12,6);
MiddleStatus newStatus=new MiddleStatus(20,10);
//保證原子操作,更新之前會比較 expect
ato.compareAndSet(expect,newStatus);
System.out.println("status = " + ato.get()); //更新失敗,MiddleStatus{esfCount=10, zfCount=5}
ato.compareAndSet(status,newStatus);
System.out.println("status = " + ato.get());//更新成功,MiddleStatus{esfCount=20, zfCount=10}
}
private static class MiddleStatus{
private int esfCount;
private int zfCount;
public MiddleStatus(int esf,int zf){
this.esfCount=esf;
this.zfCount=zf;
}
@Override
public String toString() {
return "MiddleStatus{" +
"esfCount=" + esfCount +
", zfCount=" + zfCount +
'}';
}
}
}
import java.util.concurrent.atomic.AtomicMarkableReference;
import java.util.concurrent.atomic.AtomicStampedReference;
/**
* @ClassName StampTest
* @Description 通過時間戳控制原子操作,保證是否有修改,並制定修改了多少次
**/
public class StampTest {
private static class StampStatus{
public int sCount;
public StampStatus(int c){
this.sCount=c;
}
}
public static void main(String[] args) {
StampStatus s1=new StampStatus(2);
//通過Val和初始時間戳創建StampReference
int initialStap=2;
//AtomicStampedReference 可通過Stamp知道已經修改幾次
AtomicStampedReference<StampStatus> ato=new AtomicStampedReference<>(s1,initialStap);
ato.compareAndSet(new StampStatus(3),new StampStatus(6),3,ato.getStamp()+1);
System.out.println(ato.getReference().sCount);//期望值和期望引用不正確,更新失敗
//AtomicMarkableReference 可通過Mark知道是否更新過
AtomicMarkableReference<StampStatus> ato2=new AtomicMarkableReference<>(s1,false);
ato2.compareAndSet(new StampStatus(3),new StampStatus(6),false,true);
System.out.println(ato2.isMarked());
System.out.println(ato2.getReference().sCount);//期望標誌和期望引用不對,修改失敗
}
}