深入了解Java atomic原子類的使用方法和原理
在講atomic原子類之前先看一個小例子:
public class UseAtomic {
public static void main(String[] args) {
AtomicInteger atomicInteger=new AtomicInteger();
for(int i=0;i<10;i++){
Thread t=new Thread(new AtomicTest(atomicInteger));
t.start();
try {
t.join(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(atomicInteger.get());
}
}
class AtomicTest implements Runnable{
AtomicInteger atomicInteger;
public AtomicTest(AtomicInteger atomicInteger){
this.atomicInteger=atomicInteger;
}
@Override
public void run() {
atomicInteger.addAndGet(1);
atomicInteger.addAndGet(2);
atomicInteger.addAndGet(3);
atomicInteger.addAndGet(4);
}
}
最終的輸出結(jié)果為100,可見這個程序是線程安全的。如果把AtomicInteger換成變量i的話,那最終結(jié)果就不確定了。
打開AtomicInteger的源碼可以看到:
// setup to use Unsafe.compareAndSwapInt for updates private static final Unsafe unsafe = Unsafe.getUnsafe(); private volatile int value;
volatile關(guān)鍵字用來保證內(nèi)存的可見性(但不能保證線程安全性),線程讀的時候直接去主內(nèi)存讀,寫操作完成的時候立即把數(shù)據(jù)刷新到主內(nèi)存當(dāng)中。
CAS簡要
/**
* Atomically sets the value to the given updated value
* if the current value {@code ==} the expected value.
*
* @param expect the expected value
* @param update the new value
* @return {@code true} if successful. False return indicates that
* the actual value was not equal to the expected value.
*/
public final boolean compareAndSet(int expect, int update) {
return unsafe.compareAndSwapInt(this, valueOffset, expect, update);
}
從注釋就可以看出:當(dāng)線程寫數(shù)據(jù)的時候,先對內(nèi)存中要操作的數(shù)據(jù)保留一份舊值,真正寫的時候,比較當(dāng)前的值是否和舊值相同,如果相同,則進行寫操作。如果不同,說明在此期間值已經(jīng)被修改過,則重新嘗試。
compareAndSet使用Unsafe調(diào)用native本地方法CAS(CompareAndSet)遞增數(shù)值。
CAS利用CPU調(diào)用底層指令實現(xiàn)。
兩種方式:總線加鎖或者緩存加鎖保證原子性。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于Spring Cloud 本地屬性覆蓋的問題
這篇文章主要介紹了關(guān)于Spring Cloud 本地屬性覆蓋的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
SpringBoot利用Validation包實現(xiàn)高效參數(shù)校驗
如果不進行校驗就直接使用這些數(shù)據(jù),可能會導(dǎo)致各種問題,那么SpringBoot如何利用Validation包實現(xiàn)高效參數(shù)校驗?zāi)?下面讓我們一起來探討這個重要的話題吧2025-04-04
SpringBoot應(yīng)用快速部署到K8S的詳細教程
這篇文章主要介紹了SpringBoot應(yīng)用快速部署到K8S的詳細教程,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12
Java編程實現(xiàn)調(diào)用com操作Word方法實例代碼
這篇文章主要介紹了Java編程實現(xiàn)調(diào)用com操作Word方法實例代碼,代碼注釋很詳細,在這里分給大家,需要的朋友可以參考下。2017-09-09
Java實現(xiàn)網(wǎng)絡(luò)文件下載以及下載到指定目錄
在Spring框架中,StreamUtils和FileCopyUtils兩個工具類提供了方便的文件下載功能,它們都屬于org.springframework.util包,可以通過簡單的方法調(diào)用實現(xiàn)文件流的復(fù)制和下載,這些工具類支持多種參數(shù)傳遞,涵蓋了文件下載的多種場景2024-09-09
詳解SpringBoot開發(fā)案例之整合Dubbo分布式服務(wù)
這篇文章主要介紹了詳解SpringBoot開發(fā)案例之整合Dubbo分布式服務(wù),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-10-10

