Java涓瑿AS搴曞眰瀹炵幇鍘熺悊鍒嗘瀽
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java涓瑿AS搴曞眰瀹炵幇鍘熺悊鍒嗘瀽相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/time' title='time'>time 鎵ц var hotspot style 鏃犳硶 鑰屼笖 for 鏍规嵁
CAS(鏃犻攣浼樺寲銆佽嚜鏃嬮攣)鍘熺悊鍒嗘瀽
涓€銆丆AS锛坈ompareAndSwap锛夌殑姒傚康
CAS锛?span style="color: #0000ff;">鍏ㄧОCompare And Swap锛堟瘮杈冧笌浜ゆ崲锛?/strong>锛?strong>瑙e喅澶氱嚎绋嬪苟琛屾儏鍐典笅浣跨敤閿侀€犳垚鎬ц兘鎹熻€楃殑涓€绉嶆満鍒?/strong>銆?/p>
CAS锛圴, A, B锛?/strong>锛?span style="color: #0000ff;">V涓哄唴瀛樺湴鍧€銆丄涓洪鏈熷師鍊硷紝B涓烘柊鍊?/strong>銆傚鏋滃唴瀛樺湴鍧€鐨勫€间笌棰勬湡鍘熷€肩浉鍖归厤锛岄偅涔堝皢璇ヤ綅缃€兼洿鏂颁负鏂板€笺€傚惁鍒欙紝璇存槑宸茬粡琚叾浠栫嚎绋嬫洿鏂帮紝澶勭悊鍣ㄤ笉鍋氫换浣曟搷浣滐紱鏃犺鍝鎯呭喌锛屽畠閮戒細鍦?CAS 鎸囦护涔嬪墠杩斿洖璇ヤ綅缃殑鍊笺€傝€屾垜浠彲浠ヤ娇鐢ㄨ嚜鏃嬮攣锛屽惊鐜疌AS锛岄噸鏂拌鍙栬鍙橀噺鍐嶅皾璇曞啀娆′慨鏀硅鍙橀噺锛屼篃鍙互鏀惧純鎿嶄綔銆?/p>
涓轰粈涔堥渶瑕丆AS鏈哄埗鍛紵鎴戜滑鍏堜粠涓€涓敊璇幇璞¤皥璧枫€傛垜浠粡甯镐娇鐢╲olatile鍏抽敭瀛椾慨楗版煇涓€涓彉閲忥紝琛ㄦ槑杩欎釜鍙橀噺鏄叏灞€鍏变韩鐨勪竴涓彉閲忥紝鍚屾椂鍏锋湁浜嗗彲瑙佹€у拰鏈夊簭鎬с€備絾鏄嵈娌℃湁鍘熷瓙鎬с€傛瘮濡傝涓€涓父瑙佺殑鎿嶄綔a++銆傝繖涓搷浣滃叾瀹炲彲浠ョ粏鍒嗘垚涓変釜姝ラ锛?/p>
锛?锛変粠鍐呭瓨涓鍙朼 锛?锛夊a杩涜鍔?鎿嶄綔 锛?锛夊皢a鐨勫€奸噸鏂板啓鍏ュ唴瀛樹腑 鍦ㄥ崟绾跨▼鐘舵€佷笅杩欎釜鎿嶄綔娌℃湁涓€鐐归棶棰橈紝浣嗘槸鍦ㄥ绾跨▼涓氨浼氬嚭鐜板悇绉嶅悇鏍风殑闂浜嗐€傚洜涓哄彲鑳戒竴涓嚎绋嬪a杩涜浜嗗姞1鎿嶄綔锛岃繕娌℃潵寰楀強鍐欏叆鍐呭瓨锛屽叾浠栫殑绾跨▼灏辫鍙栦簡鏃у€笺€傞€犳垚浜嗙嚎绋嬬殑涓嶅畨鍏ㄧ幇璞°€?/p>
Volatile鍏抽敭瀛楀彲浠ヤ繚璇佺嚎绋嬮棿瀵逛簬鍏变韩鍙橀噺鐨勫彲瑙佹€у彲鏈夊簭鎬э紝鍙互闃叉CPU鐨勬寚浠ら噸鎺掑簭(DCL鍗曚緥)锛屼絾鏄棤娉曚繚璇佹搷浣滅殑鍘熷瓙鎬э紝鎵€浠dk1.5涔嬪悗寮曞叆CAS鍒╃敤CPU鍘熻淇濊瘉绾跨▼鎿嶄綔鐨勯櫌瀛愭€с€?/span> CAS鎿嶄綔鐢卞鐞嗗櫒鎻愪緵鏀寔锛屾槸涓€绉嶅師璇€傚師璇槸鎿嶄綔绯荤粺鎴栬绠楁満缃戠粶鐢ㄨ鑼冪暣銆傛槸鐢辫嫢骞叉潯鎸囦护缁勬垚鐨勶紝鐢ㄤ簬瀹屾垚涓€瀹氬姛鑳界殑涓€涓繃绋嬶紝鍏锋湁涓嶅彲鍒嗗壊鎬э紝鍗冲師璇殑鎵ц蹇呴』鏄繛缁殑锛屽湪鎵ц杩囩▼涓笉鍏佽琚腑鏂€傚 Intel 澶勭悊鍣紝姣旇緝骞朵氦鎹㈤€氳繃鎸囦护鐨?cmpxchg 绯诲垪瀹炵幇銆?/span> CAS鐨勫疄鐜颁富瑕佸湪JUC涓殑atomic鍖咃紝鎴戜滑浠tomicInteger绫讳负渚嬶細 閫氳繃浠g爜杩芥函锛屽彲浠ョ湅鍑篔AVA涓殑CAS鎿嶄綔閮芥槸閫氳繃sun鍖呬笅Unsafe绫诲疄鐜帮紝鑰孶nsafe绫讳腑鐨勬柟娉曢兘鏄痭ative鏂规硶锛岀敱JVM鏈湴瀹炵幇锛屾墍浠ユ渶缁堢殑瀹炵幇鏄熀浜嶤銆丆++鍦ㄦ搷浣滅郴缁熶箣涓婃搷浣?/strong> Unsafe绫伙紝鍦╯un.misc鍖呬笅锛屼笉灞炰簬Java鏍囧噯銆俇nsafe绫绘彁渚涗竴绯诲垪澧炲姞Java璇█鑳藉姏鐨勬搷浣滐紝濡傚唴瀛樼鐞嗐€佹搷浣滅被/瀵硅薄/鍙橀噺銆佸绾跨▼鍚屾绛?/p>
鍙互鐪嬪埌璋冪敤浜?ldquo;Atomic::cmpxchg”鏂规硶锛?ldquo;Atomic::cmpxchg”鏂规硶鍦╨inux_x86鍜寃indows_x86鐨勫疄鐜板涓?/strong> cas鏄竴绉嶄箰瑙傞攣锛岃€屼笖鏄竴绉嶉潪闃诲鐨勮交閲忕骇鐨勪箰瑙傞攣锛屼粈涔堟槸闈為樆濉炲紡鐨勫憿锛熷叾瀹炲氨鏄竴涓嚎绋嬫兂瑕佽幏寰楅攣锛屽鏂逛細缁欎竴涓洖搴旇〃绀鸿繖涓攣鑳戒笉鑳借幏寰椼€傚湪璧勬簮绔炰簤涓嶆縺鐑堢殑鎯呭喌涓嬫€ц兘楂橈紝鐩告瘮synchronized閲嶉噺閿侊紝synchronized浼氳繘琛屾瘮杈冨鏉傜殑鍔犻攣锛岃В閿佸拰鍞ら啋鎿嶄綔銆?/p>
1锛夊惊鐜椂闂撮暱寮€閿€澶э紝鍗犵敤CPU璧勬簮 2锛夊彧鑳戒繚璇佷竴涓叡浜彉閲忕殑鍘熷瓙鎿嶄綔 3锛堿BA闂 1锛夋坊鍔犵増鏈彿 2锛?strong>AtomicStampedReference java骞跺彂鍖呬负浜嗚В鍐宠繖涓棶棰橈紝鎻愪緵浜嗕竴涓甫鏈夋爣璁扮殑鍘熷瓙寮曠敤绫?ldquo;AtomicStampedReference”锛屽畠鍙互閫氳繃鎺у埗鍙橀噺鍊肩殑鐗堟湰鏉ヤ繚璇丆AS鐨勬纭€с€傚洜姝わ紝鍦ㄤ娇鐢–AS鍓嶈鑰冭檻娓呮“ABA”闂鏄惁浼氬奖鍝嶇▼搴忓苟鍙戠殑姝g‘鎬э紝濡傛灉闇€瑕佽В鍐矨BA闂锛屾敼鐢ㄤ紶缁熺殑浜掓枼鍚屾鍙兘浼氭瘮鍘熷瓙绫绘洿楂樻晥銆?/strong> 5.1 绾跨▼鏁拌緝灏戙€佺瓑寰呮椂闂寸煭鍙互閲囩敤鑷棆閿佽繘琛孋AS灏濊瘯鎷块攣锛岃緝浜巗ynchronized楂樻晥 5.2 绾跨▼鏁拌緝澶с€佺瓑寰呮椂闂撮暱锛屼笉寤鸿浣跨敤鑷棆閿侊紝鍗犵敤CPU杈冮珮 以上是关于Java涓瑿AS搴曞眰瀹炵幇鍘熺悊鍒嗘瀽的主要内容,如果未能解决你的问题,请参考以下文章 鏁版嵁鍒嗘瀽濡備綍鍔╁姏淇濋櫓琛屼笟瀹炵幇鐪熸鐨勨€滄寜闇€鈥濇湇鍔★紵浜屻€丆AS锛坈ompareAndSwap锛夌殑浜х敓
涓夈€丆AS锛坈ompareAndSwap锛夌殑鍘熺悊鎺㈢┒
//var1涓篊AS鎿嶄綔鐨勫璞★紝offset涓簐ar1鏌愪釜灞炴€х殑鍦板潃鍋忕Щ鍊硷紝expected涓烘湡鏈涘€硷紝var2涓鸿璁剧疆鐨勫€硷紝鍒╃敤JNI鏉ュ畬鎴怌PU鎸囦护鐨勬搷浣?/pre>
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);
public native Object getObjectVolatile(Object var1, long var2);
public native void putObjectVolatile(Object var1, long var2, Object var4);Hotspot婧愮爜涓叧浜巙nsafe鐨勫疄鐜?br />hotspotsrcsharevmprimsunsafe.cpp
UNSAFE_ENTRY(jboolean, Unsafe_CompareAndSwapInt(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jint e, jint x))
UnsafeWrapper("Unsafe_CompareAndSwapInt");
oop p = JNIHandles::resolve(obj);鏍规嵁鍋忕Щ閲?璁$畻value鐨勫湴鍧€銆傝繖閲岀殑offset灏辨槸 AtomaicInteger涓殑valueOffset
jint* addr = (jint *) index_oop_from_field_offset_long(p, offset);
return (jint)(Atomic::cmpxchg(x, addr, e)) == e;
UNSAFE_END
hotspotsrcsharevm
untimeatomic.cpp
unsigned Atomic::cmpxchg(unsigned int exchange_value,
volatile unsigned int* dest, unsigned int compare_value) {
assert(sizeof(unsigned int) == sizeof(jint), "more work to do");
return (unsigned int)Atomic::cmpxchg((jint)exchange_value, (volatile jint*)dest,
(jint)compare_value);
}
鏍规嵁鎿嶄綔绯荤粺绫诲瀷璋冪敤涓嶅悓骞冲彴涓嬬殑閲嶈浇鍑芥暟锛岃繖涓湪棰勭紪璇戞湡闂寸紪璇戝櫒浼氬喅瀹氳皟鐢ㄥ摢涓钩鍙颁笅鐨勯噸杞?/strong>linux_x86搴曞眰瀹炵幇
hotspotsrcos_cpulinux_x86vmatomic_linux_x86.inline.hpp
inline jint Atomic::cmpxchg (jint exchange_value, volatile jint* dest, jint compare_value) {
int mp = os::is_MP();
__asm__ volatile (LOCK_IF_MP(%4) "cmpxchgl %1,(%3)"
: "=a" (exchange_value)
: "r" (exchange_value), "a" (compare_value), "r" (dest), "r" (mp)
: "cc", "memory");
return exchange_value;
}windows_x86搴曞眰瀹炵幇
hotspotsrcos_cpuwindows_x86vmatomic_linux_x86.inline.hpp
inline jint Atomic::cmpxchg (jint exchange_value, volatile jint* dest, jint compare_value) {
// alternative for InterlockedCompareExchange
int mp = os::is_MP();
__asm {
mov edx, dest
mov ecx, exchange_value
mov eax, compare_value
LOCK_IF_MP(mp)
cmpxchg dword ptr [edx], ecx
}
}
鎬荤粨锛氭牴鎹祫鏂欐煡璇紝鍏跺疄CAS搴曞眰瀹炵幇鏍规嵁涓嶅悓鐨勬搷浣滅郴缁熶細鏈変笉鍚岄噸杞斤紝CAS鐨勫疄鐜扮涓嶅紑澶勭悊鍣ㄧ殑鏀寔銆?鏍稿績浠g爜灏辨槸涓€鏉″甫lock 鍓嶇紑鐨?cmpxchg 鎸囦护锛屽嵆lock cmpxchg dword ptr [edx], ecx
Atomic::cmpxchg鏂规硶瑙f瀽锛?mp鏄?ldquo;os::is_MP()”鐨勮繑鍥炵粨鏋滐紝“os::is_MP()”鏄竴涓唴鑱斿嚱鏁帮紝鐢ㄦ潵鍒ゆ柇褰撳墠绯荤粺鏄惁涓哄澶勭悊鍣ㄣ€?濡傛灉褰撳墠绯荤粺鏄澶勭悊鍣紝璇ュ嚱鏁拌繑鍥?銆?鍚﹀垯锛岃繑鍥?銆?LOCK_IF_MP(mp)浼氭牴鎹甿p鐨勫€兼潵鍐冲畾鏄惁涓篶mpxchg鎸囦护娣诲姞lock鍓嶇紑銆?濡傛灉閫氳繃mp鍒ゆ柇褰撳墠绯荤粺鏄澶勭悊鍣紙鍗砿p鍊间负1锛夛紝鍒欎负cmpxchg鎸囦护娣诲姞lock鍓嶇紑銆?鍚﹀垯锛屼笉鍔爈ock鍓嶇紑銆?杩欐槸涓€绉嶄紭鍖栨墜娈碉紝璁や负鍗曞鐞嗗櫒鐨勭幆澧冩病鏈夊繀瑕佹坊鍔爈ock鍓嶇紑锛屽彧鏈夊湪澶氭牳鎯呭喌涓嬫墠浼氭坊鍔爈ock鍓嶇紑锛屽洜涓簂ock浼氬鑷存€ц兘涓嬮檷銆俢mpxchg鏄眹缂栨寚浠わ紝浣滅敤鏄瘮杈冨苟浜ゆ崲鎿嶄綔鏁般€?/span>
鍥涖€丆AS鏈哄埗鐨勪紭缂虹偣
4.1 浼樼偣
4.2 缂虹偣
4.3 瑙e喅ABA闂
浜斻€丆AS浣跨敤鐨勬椂鏈?/h2>