深入了解Java atomic原子類的使用方法和原理
在講atomic原子類之前先看一個(gè)小例子:
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,可見這個(gè)程序是線程安全的。如果把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)存的可見性(但不能保證線程安全性),線程讀的時(shí)候直接去主內(nèi)存讀,寫操作完成的時(shí)候立即把數(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ù)的時(shí)候,先對內(nèi)存中要操作的數(shù)據(jù)保留一份舊值,真正寫的時(shí)候,比較當(dāng)前的值是否和舊值相同,如果相同,則進(jìn)行寫操作。如果不同,說明在此期間值已經(jīng)被修改過,則重新嘗試。
compareAndSet使用Unsafe調(diào)用native本地方法CAS(CompareAndSet)遞增數(shù)值。
CAS利用CPU調(diào)用底層指令實(shí)現(xiàn)。
兩種方式:總線加鎖或者緩存加鎖保證原子性。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Java多線程Atomic包操作原子變量與原子類詳解
- Java concurrency之AtomicReference原子類_動力節(jié)點(diǎn)Java學(xué)院整理
- Java concurrency之AtomicLong原子類_動力節(jié)點(diǎn)Java學(xué)院整理
- Java concurrency之AtomicLongFieldUpdater原子類_動力節(jié)點(diǎn)Java學(xué)院整理
- Java concurrency之AtomicLongArray原子類_動力節(jié)點(diǎn)Java學(xué)院整理
- Java的Atomic原子類詳解
相關(guān)文章
總結(jié)Java常用到的六個(gè)加密技術(shù)和代碼
大家要記住現(xiàn)代密碼學(xué)最重要的原則柯克霍夫原則:數(shù)據(jù)的安全基于密鑰而不是算法的保密。也就是說即使密碼系統(tǒng)的任何細(xì)節(jié)已為人悉知,只要密匙未洩漏,它也應(yīng)是安全的。這篇文章給大家介紹了6個(gè)常用的加密技術(shù)和代碼。2016-07-07Java利用FileUtils讀取數(shù)據(jù)和寫入數(shù)據(jù)到文件
這篇文章主要介紹了Java利用FileUtils讀取數(shù)據(jù)和寫入數(shù)據(jù)到文件,下面文章圍繞FileUtils的相關(guān)資料展開怎么讀取數(shù)據(jù)和寫入數(shù)據(jù)到文件的內(nèi)容,具有一定的參考價(jià)值,徐婭奧德小伙伴可以參考一下2021-12-12SpringCloud的Gateway網(wǎng)關(guān)詳解
這篇文章主要介紹了SpringCloud的Gateway網(wǎng)關(guān)詳解,Gateway 是 Spring Cloud 官方推出的一個(gè)基于 Spring 5、Spring Boot 2 和 Project Reactor 的 API 網(wǎng)關(guān)實(shí)現(xiàn),本文將介紹 Spring Cloud Gateway 的基本概念、核心組件以及如何配置和使用它,需要的朋友可以參考下2023-09-09mybatis入門_動力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了mybatis入門的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09eclipse中沒有SERVER的解決辦法(超詳細(xì))
使用eclipse進(jìn)行tomcat配置時(shí),經(jīng)常會發(fā)現(xiàn)一個(gè)重要的問題就是打開eclipse之后沒有了server選項(xiàng),所以本給大家詳細(xì)介紹了eclipse中沒有SERVER的解決辦法,文中有詳細(xì)的圖文講解,需要的朋友可以參考下2023-12-12