Java并發(fā)之CAS原理詳解
開端
在學(xué)習(xí)源碼之前我們先從一個需求開始
需求
我們開發(fā)一個網(wǎng)站,需要對訪問量進(jìn)行統(tǒng)計(jì),用戶每發(fā)送一次請求,訪問量+1.如何實(shí)現(xiàn)?我們模擬有100個人同時訪問,并且每個人對咱們的網(wǎng)站發(fā)起10次請求,最后總訪問次數(shù)應(yīng)該是1000次
1.代碼
package day03;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
/**
* Description
* User:
* Date:
* Time:
*/
public class Demo {
//總訪問量
static int count = 0;
//模擬訪問的方法
public static void request() throws InterruptedException {
//模擬耗時5毫秒
TimeUnit.MILLISECONDS.sleep(5);
count++;
}
public static void main(String[] args) throws InterruptedException {
long startTime = System.currentTimeMillis();
int threadSize=100;
CountDownLatch countDownLatch = new CountDownLatch(threadSize);
for (int i=0;i<threadSize;i++){
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
//每個用戶訪問10次網(wǎng)站
try {
for (int j=0;j<10;j++) {
request();
}
}catch (InterruptedException e) {
e.printStackTrace();
}finally {
countDownLatch.countDown();
}
}
});
thread.start();
}
//怎么保證100個線程執(zhí)行之后,執(zhí)行后面的代碼
countDownLatch.await();
long endTime = System.currentTimeMillis();
System.out.println(Thread.currentThread().getName()+"耗時:"+(endTime-startTime)+",count:"+count);
}
}
我們多輸出幾次結(jié)果
main耗時:66,count:950
main耗時:67,count:928
發(fā)現(xiàn)每一次count都不相同,和我們期待的1000相差一點(diǎn),這里就牽扯到了并發(fā)問題,我們的count++在底層實(shí)際上由3步操作組成
- 獲取count,各個線程寫入自己的工作內(nèi)存
- count執(zhí)行+1操作
- 將+1后的值寫回主存中
這并不是一個線程安全的過程,如果有A、B兩個線程同時執(zhí)行count++,同時執(zhí)行到第一步,得到的count是一樣的,三步操作完成后,count只加1,導(dǎo)致count結(jié)果不正確
那么怎么解決這個問題呢?
我們可以考慮使用synchronized關(guān)鍵字和ReentrantLock對資源加鎖,保證并發(fā)的正確性,多線程的情況下,可以保證被鎖住的資源被串行訪問
1.1修改后的代碼
package day03;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
/**
* Description
* User:
* Date:
* Time:
*/
public class Demo02 {
//總訪問量
static int count = 0;
//模擬訪問的方法
public static synchronized void request() throws InterruptedException {
//模擬耗時5毫秒
TimeUnit.MILLISECONDS.sleep(5);
count++;
}
public static void main(String[] args) throws InterruptedException {
long startTime = System.currentTimeMillis();
int threadSize=100;
CountDownLatch countDownLatch = new CountDownLatch(threadSize);
for (int i=0;i<threadSize;i++){
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
//每個用戶訪問10次網(wǎng)站
try {
for (int j=0;j<10;j++) {
request();
}
}catch (InterruptedException e) {
e.printStackTrace();
}finally {
countDownLatch.countDown();
}
}
});
thread.start();
}
//怎么保證100個線程執(zhí)行之后,執(zhí)行后面的代碼
countDownLatch.await();
long endTime = System.currentTimeMillis();
System.out.println(Thread.currentThread().getName()+"耗時:"+(endTime-startTime)+",count:"+count);
}
}
執(zhí)行結(jié)果
main耗時:5630,count:1000
可以看到,由于sychronized鎖住了整個方法,雖然結(jié)果正確,但因?yàn)榫€程執(zhí)行方法均為串行執(zhí)行,導(dǎo)致運(yùn)行效率大大下降
那么我們?nèi)绾尾拍苁钩绦驁?zhí)行無誤時,效率還不會降低呢?
縮小鎖的范圍,升級上述3步中第三步的實(shí)現(xiàn)
- 獲取鎖
- 獲取count最新的值,記作LV
- 判斷LV是否等于A,如果相等,則將B的值賦值給count,并返回true,否則返回false
- 釋放鎖
1.2代碼改進(jìn):CAS模仿
package day03;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
/**
* Description
* User:
* Date:
* Time:
*/
public class Demo03 {
//總訪問量
volatile static int count = 0;
//模擬訪問的方法
public static void request() throws InterruptedException {
//模擬耗時5毫秒
TimeUnit.MILLISECONDS.sleep(5);
// count++;
int expectCount;
while (!compareAndSwap(expectCount=getCount(),expectCount+1)){}
}
/**
* @param expectCount 期待的值,比如最剛開始count=3
* @param newCount 新值 count+1之后的值,4
* @return
*/
public static synchronized boolean compareAndSwap(int expectCount,int newCount){
if (getCount()==expectCount){
count = newCount;
return true;
}
return false;
}
public static int getCount(){return count;}
public static void main(String[] args) throws InterruptedException {
long startTime = System.currentTimeMillis();
int threadSize=100;
CountDownLatch countDownLatch = new CountDownLatch(threadSize);
for (int i=0;i<threadSize;i++){
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
//每個用戶訪問10次網(wǎng)站
try {
for (int j=0;j<10;j++) {
request();
}
}catch (InterruptedException e) {
e.printStackTrace();
}finally {
countDownLatch.countDown();
}
}
});
thread.start();
}
//怎么保證100個線程執(zhí)行之后,執(zhí)行后面的代碼
countDownLatch.await();
long endTime = System.currentTimeMillis();
System.out.println(Thread.currentThread().getName()+"耗時:"+(endTime-startTime)+",count:"+count);
}
}
main耗時:67,count:1000
2.CAS分析
CAS全稱“CompareAndSwap”,中文翻譯過來為“比較并替換”
定義:
- CAS操作包含三個操作數(shù)——
內(nèi)存位置(V)、期望值(A)和新值(B)。如果內(nèi)存位置的值和期望值匹配,那么處理器會自動將該位置值更新為新值。否則處理器不作任何操作。無論哪種情況,它都會在CAS指令之前返回該位置的值。 - CAS在一些特殊情況下僅返回CAS是否成功,而不提取當(dāng)前值,CAS有效的說明了我認(rèn)為位置V應(yīng)該包含值A(chǔ),如果包含該值,將B放到這個位置,否則不要更改該位置的值,只告訴我這個位置現(xiàn)在的值即可
2.1Java對CAS的支持
java中提供了對CAS操作的支持,具體在sun.misc.unsafe類中,聲明如下
public final native boolean compareAndSwapObject(Object var1, long var2, Object var4, Object var5); public final native boolean compareAndSwapInt(Object var1, long var2, int var4, int var5); public final native boolean compareAndSwapLong(Object var1, long var2, long var4, long var6);
- 參數(shù)var1:表示要操作的對象
- 參數(shù)var2:表示要操作屬性地址的偏移量
- 參數(shù)var4:表示需要修改數(shù)據(jù)的期望的值
- 參數(shù)var5:表示需要修改的新值
2.2CAS實(shí)現(xiàn)原理是什么?
CAS通過調(diào)用JNI的代碼實(shí)現(xiàn),JNI:java native interface,允許java調(diào)用其他語言。而compareAndSwapxxx系列的方法就是借助C語言來調(diào)用cpu底層指令實(shí)現(xiàn)的
以常用的Intel x86平臺為例,最終映射到cpu的指令為"cmpxchg",這是一個原子指令,cpu執(zhí)行此命令時,實(shí)現(xiàn)比較并替換的操作
現(xiàn)代計(jì)算機(jī)動不動就上百核心,cmpxchg怎么保證多核心下的線程安全?
系統(tǒng)底層在進(jìn)行CAS操作的時候,會判斷當(dāng)前系統(tǒng)是否為多核心系統(tǒng),如果是就給“總線”加鎖,只有一個線程會對總線加鎖成功,加鎖之后執(zhí)行CAS操作,也就是說CAS的原子性是平臺級別的
2.3CAS存在的問題
2.3.1什么是ABA問題?
CAS需要在操作值的時候檢查下值有沒有發(fā)生變化,如果沒有發(fā)生變化則更新,但是如果一個值原來是A,在CAS方法執(zhí)行之前,被其他線程修改為B,然后又修改回了A,那么CAS方法執(zhí)行檢查的時候會發(fā)現(xiàn)它的值沒有發(fā)生變化,但是實(shí)際卻不是原來的A了,這就是CAS的ABA問題

可以看到上圖中線程A在真正更改A之前,A已經(jīng)被其他線程修改為B然后又修改為A了
程序模擬ABA問題
package day04;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Description
* User:
* Date:
* Time:
*/
public class Test01 {
public static AtomicInteger a = new AtomicInteger();
public static void main(String[] args) {
Thread main = new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"執(zhí)行,a的值為:"+a.get());
try {
int expect = a.get();
int update = expect+1;
//讓出cpu
Thread.sleep(1000);
boolean b = a.compareAndSet(expect, update);
System.out.println(Thread.currentThread().getName()+"CAS執(zhí)行:"+b+",a的值為:"+a.get());
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
},"主線程");
// main.start();
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(20);
a.incrementAndGet();
System.out.println(Thread.currentThread().getName()+"更改a的值為:"+a.get());
a.decrementAndGet();
System.out.println(Thread.currentThread().getName()+"更改a的值為:"+a.get());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"其他線程");
main.start();
thread1.start();
}
}
主線程執(zhí)行,a的值為:0
其他線程更改a的值為:1
其他線程更改a的值為:0
主線程CAS執(zhí)行:true,a的值為:1
可以看到,在執(zhí)行CAS之前,a被其他線程修改為1又修改為0,但是對執(zhí)行CAS并沒有影響,因?yàn)樗緵]有察覺到其他線程對a的修改
2.3.2如何解決ABA問題
解決ABA問題最簡單的方案就是給值加一個修改版本號,每次值變化,都會修改它的版本號,CAS操作時都去對比此版本號
在java中的ABA解決方案(AtomicStampedReference)
AtomicStampedReference主要包含一個對象引用及一個可以自動更新的整數(shù)stamp的pair對象來解決ABA問題
AtomicStampedReference源碼
/**
* 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 &&//期望引用與當(dāng)前引用保持一致
expectedStamp == current.stamp &&//期望引用版本號與當(dāng)前版本號保持一致
((newReference == current.reference &&//新值引用與當(dāng)前引用一致并且新值版本號與當(dāng)前版本號保持一致
newStamp == current.stamp)
||//如果上述版本號不一致,則通過casPair方法新建一個Pair對象,更新值和版本號,進(jìn)行再次比較
casPair(current, Pair.of(newReference, newStamp)));
}
private boolean casPair(Pair<V> cmp, Pair<V> val) {
return UNSAFE.compareAndSwapObject(this, pairOffset, cmp, val);
}
使用AtomicStampedReference解決ABA問題代碼
package day04;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.atomic.AtomicStampedReference;
/**
* Description
* User:
* Date:
* Time:
*/
public class Test02 {
public static AtomicStampedReference<Integer> a = new AtomicStampedReference(new Integer(1),1);
public static void main(String[] args) {
Thread main = new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"執(zhí)行,a的值為:"+a.getReference());
try {
Integer expectReference = a.getReference();
Integer newReference = expectReference+1;
Integer expectStamp = a.getStamp();
Integer newStamp = expectStamp+1;
//讓出cpu
Thread.sleep(1000);
boolean b = a.compareAndSet(expectReference, newReference,expectStamp,newStamp);
System.out.println(Thread.currentThread().getName()+"CAS執(zhí)行:"+b);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
},"主線程");
// main.start();
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(20);
a.compareAndSet(a.getReference(),a.getReference()+1,a.getStamp(),a.getStamp()+1);
System.out.println(Thread.currentThread().getName()+"更改a的值為:"+a.getReference());
a.compareAndSet(a.getReference(),a.getReference()-1,a.getStamp(),a.getStamp()-1);
System.out.println(Thread.currentThread().getName()+"更改a的值為:"+a.getReference());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"其他線程");
main.start();
thread1.start();
}
}
主線程執(zhí)行,a的值為:1
其他線程更改a的值為:2
其他線程更改a的值為:1
主線程CAS執(zhí)行:false
因?yàn)?code>AtomicStampedReference執(zhí)行CAS會去檢查版本號,版本號不一致則不會進(jìn)行CAS,所以ABA問題成功解決
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
Java項(xiàng)目實(shí)現(xiàn)模擬ATM機(jī)
這篇文章主要為大家詳細(xì)介紹了Java項(xiàng)目實(shí)現(xiàn)模擬ATM機(jī),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-05-05
java實(shí)現(xiàn)MapReduce對文件進(jìn)行切分的示例代碼
本文主要介紹了java實(shí)現(xiàn)MapReduce對文件進(jìn)行切分的示例代碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01
SpringSecurity OAuth2單點(diǎn)登錄和登出的實(shí)現(xiàn)
本文主要介紹了SpringSecurity OAuth2單點(diǎn)登錄和登出的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02
java實(shí)現(xiàn)輕量型http代理服務(wù)器示例
這篇文章主要介紹了java實(shí)現(xiàn)輕量型http代理服務(wù)器示例,需要的朋友可以參考下2014-04-04
Java實(shí)現(xiàn)注冊登錄與郵箱發(fā)送賬號驗(yàn)證激活功能
這篇文章主要介紹了Java實(shí)現(xiàn)注冊登錄與郵箱發(fā)送賬號驗(yàn)證激活功能,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-12-12

