Java中unsafe操作實(shí)例總結(jié)
Unsafe是Java無(wú)鎖操作的基石,在無(wú)鎖并發(fā)類中都少不了它們的身影,比如ConcurrentHashMap, ConcurrentLinkedQueue, 都是由Unsafe類來(lái)實(shí)現(xiàn)的。相對(duì)于與Java中的鎖,它基本無(wú)開(kāi)銷,會(huì)原地等待。本文主要介紹下Unsafe中的主要操作。
1 compareAndSwap
/** * 比較obj的offset處內(nèi)存位置中的值和期望的值,如果相同則更新。此更新是不可中斷的。 * * @param obj 需要更新的對(duì)象 * @param offset obj中整型field的偏移量 * @param expect 希望field中存在的值 * @param update 如果期望值expect與field的當(dāng)前值相同,設(shè)置filed的值為這個(gè)新值 * @return 如果field的值被更改返回true */ public native boolean compareAndSwapInt(Object obj, long offset, int expect, int update);
這個(gè)就是著名的CAS操作了,分為三步來(lái)做
- 獲取obj對(duì)象中為offset的偏移值,這里假設(shè)為realVal
- 比較realVal和expect
- 如果相同,將該值更新為update,否則不更新
CAS家族還包括有,compareAndSwapObject(), compareAndSwapLong(), compareAndSwapInt()等等
用AtomicInteger中一個(gè)經(jīng)典的例子來(lái)說(shuō)明:
public final int getAndAdd(int delta) { return unsafe.getAndAddInt(this, valueOffset, delta); } //unsafe.getAndAddInt public final int getAndAddInt(Object var1, long var2, int var4) { int var5; do { /**獲取原始值*/ var5 = this.getIntVolatile(var1, var2); /**確認(rèn)原始值沒(méi)有被其它線程修改時(shí),再執(zhí)行更新var5+var4操作*/ } while(!this.compareAndSwapInt(var1, var2, var5, var5 + var4)); return var5; }
2 putOrder
/*** * Sets the value of the integer field at the specified offset in the * supplied object to the given value. This is an ordered or lazy * version of <code>putIntVolatile(Object,long,int)</code>, which * doesn't guarantee the immediate visibility of the change to other * threads. It is only really useful where the integer field is * <code>volatile</code>, and is thus expected to change unexpectedly. * * @param obj the object containing the field to modify. * @param offset the offset of the integer field within <code>obj</code>. * @param value the new value of the field. * @see #putIntVolatile(Object,long,int) */ public native void putOrderedInt(Object obj, long offset, int value);
將obj對(duì)象的偏移量為offset的位置修改為value,因?yàn)镴ava中沒(méi)有內(nèi)存操作,而Unsafe的這個(gè)操作正好補(bǔ)充了內(nèi)存操作的不足。也可以用于數(shù)組操作,比如ConcurrentHashMap中就大量用到了該操作
Segment<K,V> s0 = new Segment<K,V>(loadFactor, (int)(cap * loadFactor), (HashEntry<K,V>[])new HashEntry[cap]); Segment<K,V>[] ss = (Segment<K,V>[])new Segment[ssize]; // 往數(shù)組下標(biāo)為0的位置,寫入s0: ss[0]=s0 UNSAFE.putOrderedObject(ss, SBASE, s0); // ordered write of segments[0]
需要注意的是obj需要設(shè)置為Volatile,否則對(duì)于其它線程會(huì)不可見(jiàn)
3 putXxxVolatile
/*** * Sets the value of the integer field at the specified offset in the * supplied object to the given value, with volatile store semantics. * * @param obj the object containing the field to modify. * @param offset the offset of the integer field within <code>obj</code>. * @param value the new value of the field. */ public native void putIntVolatile(Object obj, long offset, int value);
感覺(jué)和putOrderInt一樣,因?yàn)楸仨氃O(shè)置為Volatile,否則有什么用呢?
以上就是本次給大家分享的知識(shí)點(diǎn)的全部?jī)?nèi)容,感謝大家對(duì)腳本之家的支持。
相關(guān)文章
Java中I/O流讀取數(shù)據(jù)不完整的問(wèn)題解決
本文主要介紹了ava中I/O流讀取數(shù)據(jù)不完整的問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05MyBatis Plus之實(shí)現(xiàn)動(dòng)態(tài)排序方式
這篇文章主要介紹了MyBatis Plus之實(shí)現(xiàn)動(dòng)態(tài)排序方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02java實(shí)現(xiàn)拉鉤網(wǎng)上的FizzBuzzWhizz問(wèn)題示例
這篇文章主要介紹了java實(shí)現(xiàn)拉鉤網(wǎng)上的FizzBuzzWhizz問(wèn)題示例,需要的朋友可以參考下2014-05-05springboot多環(huán)境(dev、test、prod)配置詳解
這篇文章主要介紹了springboot多環(huán)境(dev、test、prod)配置詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-04-04SpringBoot靜態(tài)資源的訪問(wèn)方法詳細(xì)介紹
最近在做SpringBoot項(xiàng)目的時(shí)候遇到了“白頁(yè)”問(wèn)題,通過(guò)查資料對(duì)SpringBoot訪問(wèn)靜態(tài)資源做了總結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-09-09實(shí)例講解Java設(shè)計(jì)模式編程中如何運(yùn)用代理模式
這篇文章主要介紹了Java設(shè)計(jì)模式編程中如何運(yùn)用代理模式,文中舉了普通代理和強(qiáng)制代理的例子作為代理模式的擴(kuò)展內(nèi)容,需要的朋友可以參考下2016-02-02Java Calendar類常用示例_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
從JDK1.1版本開(kāi)始,在處理日期和時(shí)間時(shí),系統(tǒng)推薦使用Calendar類進(jìn)行實(shí)現(xiàn)。接下來(lái)通過(guò)實(shí)例代碼給大家詳細(xì)介紹Java Calendar類相關(guān)知識(shí),需要的朋友參考下吧2017-04-04Java 實(shí)戰(zhàn)項(xiàng)目錘煉之小區(qū)物業(yè)管理系統(tǒng)的實(shí)現(xiàn)流程
讀萬(wàn)卷書不如行萬(wàn)里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SSM+jsp+mysql+maven實(shí)現(xiàn)一個(gè)小區(qū)物業(yè)管理系統(tǒng),大家可以在過(guò)程中查缺補(bǔ)漏,提升水平2021-11-11