闈㈣瘯瀹橈細鑱婅亰 Java 鐨勫嚑鎶?JVM 绾ч攣鍚?/h1> Posted 寮€鍙戣€呮妧鏈墠绾?/a>
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了闈㈣瘯瀹橈細鑱婅亰 Java 鐨勫嚑鎶?JVM 绾ч攣鍚?/h1>
相关的知识,希望对你有一定的参考价值。
鐐瑰嚮鈥?/span>寮€鍙戣€呮妧鏈墠绾?/span>鈥濓紝閫夋嫨鈥滄槦鏍囸煍濃€?/span>
鍦ㄧ湅|鏄熸爣|鐣欒█, 鐪熺埍
浣滆€?| 澶忔澃
绠€浠?/span>
鍦ㄨ绠楁満琛屼笟鏈変竴涓畾寰嬪彨"鎽╁皵瀹氬緥"锛屽湪姝ゅ畾寰嬩笅锛岃绠楁満鐨勬€ц兘绐侀鐚涜繘锛岃€屼笖浠锋牸涔熼殢涔嬭秺鏉ヨ秺渚垮疁锛宑pu浠庡崟鏍稿埌浜嗗鏍革紝缂撳瓨鎬ц兘涔熷緱鍒颁簡寰堝ぇ鎻愬崌锛屽挨鍏舵槸澶氭牳cpu鎶€鏈殑鍒版潵锛岃绠楁満鍚屼竴鏃跺埢鍙互澶勭悊澶氫釜浠诲姟銆傚湪纭欢灞傞潰鐨勫彂灞曞甫鏉ョ殑鏁堢巼鏋佸ぇ鎻愬崌涓紝杞欢灞傞潰鐨勫绾跨▼缂栫▼宸茬粡鎴愪负蹇呯劧瓒嬪娍锛岀劧鑰屽绾跨▼缂栫▼灏变細寮曞叆鏁版嵁瀹夊叏鎬ч棶棰橈紝鏈夌煕蹇呮湁鐩撅紝浜庢槸鍙戞槑浜嗏€滈攣鈥濇潵瑙e喅绾跨▼瀹夊叏闂銆傚湪杩欑瘒鏂囩珷涓紝鎬荤粨浜唈ava涓嚑鎶婄粡鍏哥殑JVM绾у埆鐨勯攣銆?/span>
synchronized
synchronized鍏抽敭瀛楁槸涓€鎶婄粡鍏哥殑閿侊紝涔熸槸鎴戜滑骞虫椂鐢ㄥ緱鏈€澶氱殑銆傚湪jdk1.6涔嬪墠锛宻yncronized鏄竴鎶婇噸閲忕骇鐨勯攣锛屼笉杩囬殢鐫€jdk鐨勫崌绾э紝涔熷湪瀵瑰畠杩涜涓嶆柇鐨勪紭鍖栵紝濡備粖瀹冨彉寰椾笉閭d箞閲嶄簡锛岀敋鑷冲湪鏌愪簺鍦烘櫙涓嬶紝瀹冪殑鎬ц兘鍙嶈€屼紭浜庤交閲忕骇閿併€傚湪鍔犱簡syncronized鍏抽敭瀛楃殑鏂规硶銆佷唬鐮佸潡涓紝涓€娆″彧鍏佽涓€涓嚎绋嬭繘鍏ョ壒瀹氫唬鐮佹锛屼粠鑰岄伩鍏嶅绾跨▼鍚屾椂淇敼鍚屼竴鏁版嵁銆?/span>
synchronized閿佹湁濡備笅鍑犱釜鐗圭偣锛?/span>
a銆佹湁閿佸崌绾ц繃绋?/span>
鍦╦dk1.5(鍚?涔嬪墠锛宻ynchronized鐨勫簳灞傚疄鐜版槸閲嶉噺绾х殑锛屾墍浠ヤ箣鍓嶄竴鑷寸О鍛煎畠涓?閲嶉噺绾ч攣"锛屽湪jdk1.5涔嬪悗锛屽synchronized杩涜浜嗗悇绉嶄紭鍖栵紝瀹冨彉寰椾笉閭d箞閲嶄簡锛屽疄鐜板師鐞嗗氨鏄攣鍗囩骇鐨勮繃绋嬨€?/span>
鎴戜滑鍏堣亰鑱?.5涔嬪悗鐨剆ynchronized瀹炵幇鍘熺悊鏄€庢牱鐨勶紝璇村埌synchronized鍔犻攣鍘熺悊锛屽氨涓嶅緱涓嶅厛璇磈ava瀵硅薄鍦ㄥ唴瀛樹腑鐨勫竷灞€锛宩ava瀵硅薄鍐呭瓨甯冨眬濡備笅:
濡備笂鍥炬墍绀猴紝鍦ㄥ垱寤轰竴涓璞″悗锛屽湪JVM铏氭嫙鏈?HotSpot)涓紝瀵硅薄鍦↗ava鍐呭瓨涓殑瀛樺偍甯冨眬 鍙垎涓轰笁鍧?
(1)瀵硅薄澶村尯鍩?/span>
姝ゅ瀛樺偍鐨勪俊鎭寘鎷袱閮ㄥ垎锛?/span>
瀵硅薄鑷韩鐨勮繍琛屾椂鏁版嵁(MarkWord)
瀛樺偍hashCode銆丟C鍒嗕唬骞撮緞銆侀攣绫诲瀷鏍囪銆佸亸鍚戦攣绾跨▼ID銆丆AS閿佹寚鍚戠嚎绋婰ockRecord鐨勬寚閽堢瓑锛宻ynconized閿佺殑鏈哄埗涓庤繖涓儴鍒?markwork)瀵嗗垏鐩稿叧锛岀敤markword涓渶浣庣殑涓変綅浠h〃閿佺殑鐘舵€侊紝鍏朵腑涓€浣嶆槸鍋忓悜閿佷綅锛屽彟澶栦袱浣嶆槸鏅€氶攣浣?/span>
瀵硅薄绫诲瀷鎸囬拡(Class Pointer)
瀵硅薄鎸囧悜瀹冪殑绫诲厓鏁版嵁鐨勬寚閽堛€丣VM灏辨槸閫氳繃瀹冩潵纭畾鏄摢涓狢lass鐨勫疄渚?/span>
(2)瀹炰緥鏁版嵁鍖哄煙
姝ゅ瀛樺偍鐨勬槸瀵硅薄鐪熸鏈夋晥鐨勪俊鎭紝姣斿瀵硅薄涓墍鏈夊瓧娈电殑鍐呭
(3)瀵归綈濉厖鍖哄煙
褰撶嚎绋嬭繘鍏ュ埌synchronized澶勫皾璇曡幏鍙栬閿佹椂锛宻ynchronized閿佸崌绾ф祦绋嬪涓嬶細
濡備笂鍥炬墍绀猴紝synchronized閿佸崌绾х殑椤哄簭涓猴細鍋忓悜閿?>杞婚噺绾ч攣->閲嶉噺绾ч攣锛屾瘡涓€姝ヨЕ鍙戦攣鍗囩骇鐨勬儏鍐靛涓嬶細
鍋忓悜閿?/span>
鍦↗DK1.8涓紝鍏跺疄榛樿鏄交閲忕骇閿侊紝浣嗗鏋滆瀹氫簡
-XX:BiasedLockingStartupDelay = 0
閭e湪瀵逛竴涓狾bject鍋歴yncronized鐨勬椂鍊欙紝浼氱珛鍗充笂涓€鎶婂亸鍚戦攣銆?/span>褰撳浜庡亸鍚戦攣鐘舵€佹椂锛宮arkwork浼氳褰曞綋鍓嶇嚎绋婭D
鍗囩骇鍒拌交閲忕骇閿?/strong>
褰撲笅涓€涓嚎绋嬪弬涓庡埌鍋忓悜閿佺珵浜夋椂锛屼細鍏堝垽鏂璵arkword涓繚瀛樼殑绾跨▼ID鏄惁涓庤繖涓嚎绋婭D鐩哥瓑锛屽鏋滀笉鐩哥瓑锛屼細绔嬪嵆鎾ら攢鍋忓悜閿侊紝鍗囩骇涓鸿交閲忕骇閿併€傛瘡涓嚎绋嬪湪鑷繁鐨勭嚎绋嬫爤涓敓鎴愪竴涓狶ockRecord(LR)锛岀劧鍚庢瘡涓嚎绋嬮€氳繃CAS(鑷棆)鐨勬搷浣滃皢閿佸璞″ご涓殑markwork璁剧疆涓烘寚鍚戣嚜宸辩殑LR鐨勬寚閽堬紝鍝釜绾跨▼璁剧疆鎴愬姛锛屽氨鎰忓懗鐫€鑾峰緱閿併€?/span>
鍏充簬synchronized涓鏃舵墽琛岀殑CAS鎿嶄綔鏄€氳繃native鐨勮皟鐢℉otSpot涓璪ytecodeInterpreter.cpp鏂囦欢C++浠g爜瀹炵幇鐨勶紝鏈夊叴瓒g殑鍙互缁х画娣辨寲銆?/span>
鍗囩骇鍒伴噸閲忕骇閿?/strong>
濡傛灉閿佺珵浜夊姞鍓?濡傜嚎绋嬭嚜鏃嬫鏁版垨鑰呰嚜鏃嬬殑绾跨▼鏁拌秴杩囨煇闃堝€硷紝JDK1.6涔嬪悗锛岀敱JVM鑷繁鎺у埗鏀硅鍒?锛屽氨浼氬崌绾т负閲嶉噺绾ч攣銆傛鏃跺氨浼氬悜鎿嶄綔绯荤粺鐢宠璧勬簮锛岀嚎绋嬫寕璧凤紝杩涘叆鍒版搷浣滅郴缁熷唴鏍告€佺殑绛夊緟闃熷垪涓紝绛夊緟鎿嶄綔绯荤粺璋冨害锛岀劧鍚庢槧灏勫洖鐢ㄦ埛鎬併€傚湪閲嶉噺绾ч攣涓紝鐢变簬闇€瑕佸仛鍐呮牳鎬佸埌鐢ㄦ埛鎬佺殑杞崲锛岃€岃繖涓繃绋嬩腑闇€瑕佹秷鑰楄緝澶氭椂闂达紝涔熷氨鏄?閲?鐨勫師鍥犱箣涓€銆?/span>
b銆佸彲閲嶅叆
synchronized鎷ユ湁寮哄埗鍘熷瓙鎬х殑鍐呴儴閿佹満鍒讹紝鏄竴鎶婂彲閲嶅叆閿併€傚洜姝わ紝鍦ㄤ竴涓嚎绋嬩娇鐢╯ynchronized鏂规硶鏃惰皟鐢ㄨ瀵硅薄鍙︿竴涓猻ynchronized鏂规硶锛屽嵆涓€涓嚎绋嬪緱鍒颁竴涓璞¢攣鍚庡啀娆¤姹傝瀵硅薄閿侊紝鏄案杩滃彲浠ユ嬁鍒伴攣鐨勩€傚湪Java涓嚎绋嬭幏寰楀璞¢攣鐨勬搷浣滄槸浠ョ嚎绋嬩负鍗曚綅鐨勶紝鑰屼笉鏄互璋冪敤涓哄崟浣嶇殑銆?/span>
synchronized閿佺殑瀵硅薄澶寸殑markwork涓細璁板綍璇ラ攣鐨勭嚎绋嬫寔鏈夎€呭拰璁℃暟鍣紝褰撲竴涓嚎绋嬭姹傛垚鍔熷悗锛孞VM浼氳涓嬫寔鏈夐攣鐨勭嚎绋嬶紝骞跺皢璁℃暟鍣ㄨ涓?銆傛鏃跺叾浠栫嚎绋嬭姹傝閿侊紝鍒欏繀椤荤瓑寰呫€傝€岃鎸佹湁閿佺殑绾跨▼濡傛灉鍐嶆璇锋眰杩欎釜閿侊紝灏卞彲浠ュ啀娆℃嬁鍒拌繖涓攣锛屽悓鏃惰鏁板櫒浼氶€掑銆?/span>
褰撶嚎绋嬮€€鍑轰竴涓猻ynchronized鏂规硶/鍧楁椂锛岃鏁板櫒浼氶€掑噺锛屽鏋滆鏁板櫒涓?鍒欓噴鏀捐閿侀攣銆?/span>
c銆佹偛瑙傞攣(浜掓枼閿併€佹帓浠栭攣)
synchronized鏄竴鎶婃偛瑙傞攣(鐙崰閿?锛屽綋鍓嶇嚎绋嬪鏋滆幏鍙栧埌閿侊紝浼氬鑷村叾瀹冩墍鏈夐渶瑕侀攣璇ョ殑绾跨▼绛夊緟锛屼竴鐩寸瓑寰呮寔鏈夐攣鐨勭嚎绋嬮噴鏀鹃攣鎵嶇户缁繘琛岄攣鐨勪簤鎶?/span>
ReentrantLock
ReentrantLock浠庡瓧闈㈠彲浠ョ湅鍑烘槸涓€鎶婂彲閲嶅叆閿侊紝杩欑偣鍜宻ynchronized涓€鏍凤紝浣嗗疄鐜板師鐞嗕篃涓巗yncronized鏈夊緢澶у樊鍒€?/span>
瀹冩槸鍩轰簬缁忓吀鐨凙QS(AbstractQueueSyncronized)瀹炵幇鐨?AQS鏄熀浜巚olitale鍜孋AS瀹炵幇鐨勩€?/span>鍏朵腑AQS涓淮鎶や竴涓獀alitale绫诲瀷鐨勫彉閲弒tate鏉ュ仛涓€涓彲閲嶅叆閿佺殑閲嶅叆娆℃暟锛屽姞閿佸拰閲婃斁閿佷篃鏄洿缁曡繖涓彉閲忔潵杩涜鐨勩€?/span>ReentrantLock涔熸彁渚涗簡涓€浜泂ynchronized娌℃湁鐨勭壒鐐癸紝鍥犳姣攕ynchronized濂界敤銆?/span>
AQS妯″瀷濡備笅鍥撅細
ReentrantLock鏈夊涓嬬壒鐐癸細
a銆佸彲閲嶅叆
ReentrantLock鍜宻yncronized鍏抽敭瀛椾竴鏍凤紝閮芥槸鍙噸鍏ラ攣锛屼笉杩囦袱鑰呭疄鐜板師鐞嗙◢鏈夊樊鍒紝RetrantLock鍒╃敤AQS鐨勭殑state鐘舵€佹潵鍒ゆ柇璧勬簮鏄惁宸查攣锛屽悓涓€绾跨▼閲嶅叆鍔犻攣锛宻tate鐨勭姸鎬?1; 鍚屼竴绾跨▼閲嶅叆瑙i攣,state鐘舵€?1(瑙i攣蹇呴』涓哄綋鍓嶇嫭鍗犵嚎绋嬶紝鍚﹀垯寮傚父); 褰搒tate涓?鏃惰В閿佹垚鍔熴€?/span>
b銆侀渶瑕佹墜鍔ㄥ姞閿併€佽В閿?/strong>
synchronized鍏抽敭瀛楁槸鑷姩杩涜鍔犻攣銆佽В閿佺殑锛岃€孯eentrantLock闇€瑕乴ock()鍜寀nlock()鏂规硶閰嶅悎try/finally璇彞鍧楁潵瀹屾垚锛屾潵鎵嬪姩鍔犻攣銆佽В閿併€?/span>
c銆佹敮鎸佽缃攣鐨勮秴鏃舵椂闂?/span>
synchronized鍏抽敭瀛楁棤娉曡缃攣鐨勮秴鏃舵椂闂达紝濡傛灉涓€涓幏寰楅攣鐨勭嚎绋嬪唴閮ㄥ彂鐢熸閿侊紝閭d箞鍏朵粬绾跨▼灏变細涓€鐩磋繘鍏ラ樆濉炵姸鎬侊紝鑰孯eentrantLock鎻愪緵tryLock鏂规硶锛屽厑璁歌缃嚎绋嬭幏鍙栭攣鐨勮秴鏃舵椂闂达紝濡傛灉瓒呮椂锛屽垯璺宠繃锛屼笉杩涜浠讳綍鎿嶄綔锛岄伩鍏嶆閿佺殑鍙戠敓銆?/span>
d銆佹敮鎸佸叕骞?闈炲叕骞抽攣
synchronized鍏抽敭瀛楁槸涓€绉嶉潪鍏钩閿侊紝鍏堟姠鍒伴攣鐨勭嚎绋嬪厛鎵ц銆傝€孯eentrantLock鐨勬瀯閫犳柟娉曚腑鍏佽璁剧疆true/false鏉ュ疄鐜板叕骞炽€侀潪鍏钩閿侊紝濡傛灉璁剧疆涓簍rue锛屽垯绾跨▼鑾峰彇閿佽閬靛惊"鍏堟潵鍚庡埌"鐨勮鍒欙紝姣忔閮戒細鏋勯€犱竴涓嚎绋婲ode锛岀劧鍚庡埌鍙屽悜閾捐〃鐨?灏惧反"鍚庨潰鎺掗槦锛岀瓑寰呭墠闈㈢殑Node閲婃斁閿佽祫婧愩€?/span>
e銆佸彲涓柇閿?/span>
ReentrantLock涓殑lockInterruptibly()鏂规硶浣垮緱绾跨▼鍙互鍦ㄨ闃诲鏃跺搷搴斾腑鏂紝姣斿涓€涓嚎绋媡1閫氳繃lockInterruptibly()鏂规硶鑾峰彇鍒颁竴涓彲閲嶅叆閿侊紝骞舵墽琛屼竴涓暱鏃堕棿鐨勪换鍔★紝鍙︿竴涓嚎绋嬮€氳繃interrupt()鏂规硶灏卞彲浠ョ珛鍒绘墦鏂璽1绾跨▼鐨勬墽琛岋紝鏉ヨ幏鍙杢1鎸佹湁鐨勯偅涓彲閲嶅叆閿併€?/span>
鑰岄€氳繃ReentrantLock鐨刲ock()鏂规硶鎴栬€匰ynchronized鎸佹湁閿佺殑绾跨▼鏄笉浼氬搷搴斿叾浠栫嚎绋嬬殑interrupt()鏂规硶鐨勶紝鐩村埌璇ユ柟娉曚富鍔ㄩ噴鏀鹃攣涔嬪悗鎵嶄細鍝嶅簲interrupt()鏂规硶銆?/span>
ReentrantReadWriteLock
ReentrantReadWriteLock(璇诲啓閿?鍏跺疄鏄袱鎶婇攣锛?/span>
涓€鎶婃槸WriteLock(鍐欓攣)
涓€鎶婃槸璇婚攣锛孯eadLock
璇诲啓閿佺殑瑙勫垯鏄細璇昏涓嶄簰鏂ャ€佽鍐欎簰鏂ャ€佸啓鍐欎簰鏂ャ€傚湪涓€浜涘疄闄呯殑鍦烘櫙涓紝璇绘搷浣滅殑棰戠巼杩滆繙楂樹簬鍐欐搷浣滐紝濡傛灉鐩存帴鐢ㄤ竴鑸殑閿佽繘琛屽苟鍙戞帶鍒剁殑璇濓紝灏变細璇昏浜掓枼銆佽鍐欎簰鏂ャ€佸啓鍐欎簰鏂ワ紝鏁堢巼浣庝笅锛岃鍐欓攣鐨勪骇鐢熷氨鏄负浜嗕紭鍖栬繖绉嶅満鏅殑鎿嶄綔鏁堢巼銆備竴鑸儏鍐典笅鐙崰閿佺殑鏁堢巼浣庢潵婧愪簬楂樺苟鍙戜笅瀵逛复鐣屽尯鐨勬縺鐑堢珵浜夊鑷寸嚎绋嬩笂涓嬫枃鍒囨崲銆傚洜姝ゅ綋骞跺彂涓嶆槸寰堥珮鐨勬儏鍐典笅锛岃鍐欓攣鐢变簬闇€瑕侀澶栫淮鎶よ閿佺殑鐘舵€侊紝鍙兘杩樹笉濡傜嫭鍗犻攣鐨勬晥鐜囬珮銆傚洜姝ら渶瑕佹牴鎹疄闄呮儏鍐甸€夋嫨浣跨敤銆?/span>
ReentrantReadWriteLock鐨勫師鐞嗕篃鏄熀浜嶢QS杩涜瀹炵幇鐨勩€?/span>
涓嶳eentrantLock鐨勫樊鍒湪浜嶳eentrantReadWriteLock閿佹嫢鏈夊叡浜攣銆佹帓浠栭攣灞炴€с€傝鍐欓攣涓殑鍔犻攣銆侀噴鏀鹃攣涔熸槸鍩轰簬Sync(缁ф壙浜嶢QS)锛屽苟涓斾富瑕佷娇鐢ˋQS涓殑state鍜宯ode涓殑waitState鍙橀噺杩涜瀹炵幇鐨勩€?/span>
瀹炵幇璇诲啓閿佷笌瀹炵幇鏅€氫簰鏂ラ攣鐨勪富瑕佸尯鍒湪浜庨渶瑕佸垎鍒褰曡閿佺姸鎬佸強鍐欓攣鐘舵€侊紝骞朵笖绛夊緟闃熷垪涓渶瑕佸尯鍒鐞嗕袱绉嶅姞閿佹搷浣溿€俁eentrantReadWriteLock涓皢AQS涓殑int绫诲瀷鐨剆tate鍒嗕负楂?6浣嶄笌绗?6浣嶅垎鍒褰曡閿佸拰鍐欓攣鐨勭姸鎬侊紝濡備笅鍥炬墍绀猴細
a銆乄riteLock(鍐欓攣)鏄偛瑙傞攣(鎺掍粬閿併€佷簰鏂ラ攣)
閫氳繃璁$畻 state&((1<<16)-1)锛屽皢state鐨勯珮16浣嶅叏閮ㄦ姽鍘伙紝鍥犳state鐨勪綆浣嶈褰曠潃鍐欓攣鐨勯噸鍏ヨ鏁?/span>
鑾峰彇鍐欓攣婧愮爜锛?/span>
閲婃斁鍐欓攣婧愮爜锛?/span>
b銆丷eadLock(璇婚攣)鏄叡浜攣(涔愯閿?
閫氳繃璁$畻 state>>>16 杩涜鏃犵鍙疯ˉ0锛屽彸绉?6浣嶏紝鍥犳state鐨勯珮浣嶈褰曠潃鍐欓攣鐨勯噸鍏ヨ鏁般€?/span>
璇婚攣鑾峰彇閿佺殑杩囩▼姣斿啓閿佺◢寰鏉備簺锛?/span>棣栧厛鍒ゆ柇鍐欓攣鏄惁涓?骞朵笖褰撳墠绾跨▼涓嶅崰鏈夌嫭鍗犻攣锛岀洿鎺ヨ繑鍥烇紱鍚?/span>鍒欙紝鍒ゆ柇璇荤嚎绋嬫槸鍚﹂渶瑕佽闃诲骞朵笖璇婚攣鏁伴噺鏄惁灏忎簬鏈€澶у€煎苟涓旀瘮杈冭缃姸鎬佹垚鍔燂細
鑻ュ綋鍓嶆病鏈夎閿侊紝鍒欒缃涓€涓绾跨▼锛?/span>
firstReader
firstReaderHoldCount锛?/span>
鑻ュ綋鍓嶇嚎绋嬬嚎绋嬩负绗竴涓绾跨▼锛屽垯澧炲姞锛?/span>
firstReaderHoldCount
鍚﹀垯锛屽皢璁剧疆褰撳墠绾跨▼瀵瑰簲鐨凥oldCounter瀵硅薄鐨勫€笺€?/span>
鏇存柊鎴愬姛鍚庝細鍦?/span>readHolds(ThreadLocal绫诲瀷鐨?鐨勬湰绾跨▼鍓湰涓褰曞綋鍓嶇嚎绋嬮噸鍏ユ暟锛岃繖鏄负浜嗗疄鐜癹dk1.6涓姞鍏ョ殑getReadHoldCount()鏂规硶鐨勶紝杩欎釜鏂规硶鑳借幏鍙栧綋鍓嶇嚎绋嬮噸鍏ュ叡浜攣鐨勬鏁?state涓褰曠殑鏄涓嚎绋嬬殑鎬婚噸鍏ユ鏁?锛屽姞鍏ヤ簡杩欎釜鏂规硶璁╀唬鐮佸鏉備簡涓嶅皯銆?/span>
浣嗘槸鍏跺師鐞嗚繕鏄緢绠€鍗曠殑锛?/span>
濡傛灉褰撳墠鍙湁涓€涓嚎绋嬬殑璇濓紝杩樹笉闇€瑕佸姩鐢═hreadLocal锛?/span>
鐩存帴寰€firstReaderHoldCount杩欎釜鎴愬憳鍙橀噺閲屽瓨閲嶅叆鏁帮紱
褰撴湁绗簩涓嚎绋嬫潵鐨勬椂鍊欙紝灏辫鍔ㄧ敤ThreadLocal鍙橀噺readHolds浜嗭紝姣忎釜绾跨▼鎷ユ湁鑷繁鐨勫壇鏈紝鐢ㄦ潵淇濆瓨鑷繁鐨勯噸鍏ユ暟銆?/span>
鑾峰彇璇婚攣婧愮爜锛?nbsp;
閲婃斁璇婚攣婧愮爜锛?/span>
閫氳繃鍒嗘瀽鍙互鐪嬪嚭锛?/span>
鍦ㄧ嚎绋嬫寔鏈夎閿佺殑鎯呭喌涓嬶紝璇ョ嚎绋嬩笉鑳藉彇寰楀啓閿?鍥犱负鑾峰彇鍐欓攣鐨勬椂鍊欙紝濡傛灉鍙戠幇褰撳墠鐨勮閿佽鍗犵敤锛屽氨椹笂鑾峰彇澶辫触锛屼笉绠¤閿佹槸涓嶆槸琚綋鍓嶇嚎绋嬫寔鏈?銆?/span>
鍦ㄧ嚎绋嬫寔鏈夊啓閿佺殑鎯呭喌涓嬶紝璇ョ嚎绋嬪彲浠ョ户缁幏鍙栬閿侊紙鑾峰彇璇婚攣鏃跺鏋滃彂鐜板啓閿佽鍗犵敤锛屽彧鏈夊啓閿佹病鏈夎褰撳墠绾跨▼鍗犵敤鐨勬儏鍐垫墠浼氳幏鍙栧け璐ワ級銆?/span>
LongAdder
鍦ㄩ珮骞跺彂鐨勬儏鍐典笅锛屾垜浠涓€涓狪nteger绫诲瀷鐨勬暣鏁扮洿鎺ヨ繘琛宨++鐨勬椂鍊欙紝鏃犳硶淇濊瘉鎿嶄綔鐨勫師瀛愭€э紝浼氬嚭鐜扮嚎绋嬪畨鍏ㄧ殑闂銆備负姝ゆ垜浠細鐢╦uc涓嬬殑AtomicInteger锛屽畠鏄竴涓彁渚涘師瀛愭搷浣滅殑Interger绫伙紝鍐呴儴涔熸槸閫氳繃CAS瀹炵幇绾跨▼瀹夊叏鐨勩€?/span>
浣嗗綋澶ч噺绾跨▼鍚屾椂鍘昏闂椂锛屽氨浼氬洜涓哄ぇ閲忕嚎绋嬫墽琛孋AS鎿嶄綔澶辫触鑰岃繘琛岀┖鏃嬭浆锛屽鑷碈PU璧勬簮娑堣€楄繃澶氾紝鑰屼笖鎵ц鏁堢巼涔熶笉楂樸€侱oug Lea澶х搴旇涔熶笉婊℃剰锛屼簬鏄湪JDK1.8涓CAS杩涜浜嗕紭鍖栵紝鎻愪緵浜哃ongAdder锛屽畠鏄熀浜庝簡CAS鍒嗘閿佺殑鎬濇兂瀹炵幇鐨勩€?/span>
绾跨▼鍘昏鍐欎竴涓狶ongAdder绫诲瀷鐨勫彉閲忔椂锛屾祦绋嬪涓嬶細
LongAdder涔熸槸鍩轰簬Unsafe鎻愪緵鐨凜AS鎿嶄綔+valitale鍘诲疄鐜扮殑銆?/span>鍦↙ongAdder鐨勭埗绫籗triped64涓淮鎶ょ潃涓€涓猙ase鍙橀噺鍜屼竴涓猚ell鏁扮粍锛屽綋澶氫釜绾跨▼鎿嶄綔涓€涓彉閲忕殑鏃跺€欙紝鍏堜細鍦ㄨ繖涓猙ase鍙橀噺涓婅繘琛宑as鎿嶄綔锛屽綋瀹冨彂鐜扮嚎绋嬪澶氱殑鏃跺€欙紝灏变細浣跨敤cell鏁扮粍銆?/span>
姣斿褰揵ase灏嗚鏇存柊鐨勬椂鍊欏彂鐜扮嚎绋嬪澶氾紙涔熷氨鏄皟鐢╟asBase鏂规硶鏇存柊base鍊煎け璐ワ級锛岄偅涔堝畠浼氳嚜鍔ㄤ娇鐢╟ell鏁扮粍锛屾瘡涓€涓嚎绋嬪搴斾簬涓€涓猚ell锛屽湪姣忎竴涓嚎绋嬩腑瀵硅cell杩涜cas鎿嶄綔锛岃繖鏍峰氨鍙互灏嗗崟涓€value鐨勬洿鏂板帇鍔涘垎鎷呭埌澶氫釜value涓幓锛岄檷浣庡崟涓獀alue鐨?鈥滅儹搴︹€濓紝鍚屾椂涔熷噺灏戜簡绾跨▼澶ч噺绾跨▼鐨勭┖杞紝鎻愰珮骞跺彂鏁堢巼锛屽垎鏁e苟鍙戝帇鍔涖€?/span>杩欑鍒嗘閿侀渶瑕侀澶栫淮鎶や竴涓唴瀛樼┖闂碿ells锛屼笉杩囧湪楂樺苟鍙戝満鏅笅锛岃繖鐐规垚鏈嚑涔庡彲浠ュ拷鐣ャ€?/span>
鍒嗘閿佹槸涓€绉嶄紭绉€鐨勪紭鍖栨€濇兂锛宩uc涓彁渚涚殑鐨凜oncurrentHashMap涔熸槸鍩轰簬鍒嗘閿佷繚璇佽鍐欐搷浣滅殑绾跨▼瀹夊叏銆?/span>
鍦ㄨ繖閲岋紝鎴戜负澶у鍑嗗浜嗕竴浠?020骞存渶鏂版渶鍏ㄧ殑銆奐ava闈㈣瘯棰樺強绛旀V3.0銆嬶紝杩欏鐢靛瓙涔︽兜鐩栦簡璇稿鍚庣鎶€鏈爤鐨勯潰璇曢鍜岀瓟妗堬紝鐩镐俊鍙互甯姪澶у鍦ㄦ渶鐭殑鏃堕棿鍐呭涔燡ava鍚庣鐨勫ぇ澶氭暟闈㈣瘯棰橈紝浠庤€屾嬁鍒拌嚜宸卞績浠殑offer銆?/p>
鎴簡寮犲浘锛屽ぇ瀹跺彲浠ヤ粩缁嗘煡鐪嬪乏杈圭殑鑿滃崟鏍忥紝瑕嗙洊鐨勭煡璇嗛潰鐪熺殑寰堝箍锛岃€屼笖璐ㄩ噺閮藉緢涓嶉敊銆?
璧勬枡鑾峰彇鏂规硶
鎵弿涓嬫柟浜岀淮鐮佸姞鎴戝井淇?/span>
鍚庡彴鍥炲鍏抽敭璇嶏細Java鏍稿績鏁寸悊
END
鍘嗗彶鎺ㄨ崘
濂芥枃鐐逛釜鍦ㄧ湅鍚э紒
以上是关于闈㈣瘯瀹橈細鑱婅亰 Java 鐨勫嚑鎶?JVM 绾ч攣鍚?/h1>
Posted 寮€鍙戣€呮妧鏈墠绾?/a>
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了闈㈣瘯瀹橈細鑱婅亰 Java 鐨勫嚑鎶?JVM 绾ч攣鍚?/h1>
鐐瑰嚮鈥?/span>寮€鍙戣€呮妧鏈墠绾?/span>鈥濓紝閫夋嫨鈥滄槦鏍囸煍濃€?/span>
鍦ㄧ湅|鏄熸爣|鐣欒█, 鐪熺埍
浣滆€?| 澶忔澃
绠€浠?/span>
鍦ㄨ绠楁満琛屼笟鏈変竴涓畾寰嬪彨"鎽╁皵瀹氬緥"锛屽湪姝ゅ畾寰嬩笅锛岃绠楁満鐨勬€ц兘绐侀鐚涜繘锛岃€屼笖浠锋牸涔熼殢涔嬭秺鏉ヨ秺渚垮疁锛宑pu浠庡崟鏍稿埌浜嗗鏍革紝缂撳瓨鎬ц兘涔熷緱鍒颁簡寰堝ぇ鎻愬崌锛屽挨鍏舵槸澶氭牳cpu鎶€鏈殑鍒版潵锛岃绠楁満鍚屼竴鏃跺埢鍙互澶勭悊澶氫釜浠诲姟銆傚湪纭欢灞傞潰鐨勫彂灞曞甫鏉ョ殑鏁堢巼鏋佸ぇ鎻愬崌涓紝杞欢灞傞潰鐨勫绾跨▼缂栫▼宸茬粡鎴愪负蹇呯劧瓒嬪娍锛岀劧鑰屽绾跨▼缂栫▼灏变細寮曞叆鏁版嵁瀹夊叏鎬ч棶棰橈紝鏈夌煕蹇呮湁鐩撅紝浜庢槸鍙戞槑浜嗏€滈攣鈥濇潵瑙e喅绾跨▼瀹夊叏闂銆傚湪杩欑瘒鏂囩珷涓紝鎬荤粨浜唈ava涓嚑鎶婄粡鍏哥殑JVM绾у埆鐨勯攣銆?/span>
synchronized
synchronized鍏抽敭瀛楁槸涓€鎶婄粡鍏哥殑閿侊紝涔熸槸鎴戜滑骞虫椂鐢ㄥ緱鏈€澶氱殑銆傚湪jdk1.6涔嬪墠锛宻yncronized鏄竴鎶婇噸閲忕骇鐨勯攣锛屼笉杩囬殢鐫€jdk鐨勫崌绾э紝涔熷湪瀵瑰畠杩涜涓嶆柇鐨勪紭鍖栵紝濡備粖瀹冨彉寰椾笉閭d箞閲嶄簡锛岀敋鑷冲湪鏌愪簺鍦烘櫙涓嬶紝瀹冪殑鎬ц兘鍙嶈€屼紭浜庤交閲忕骇閿併€傚湪鍔犱簡syncronized鍏抽敭瀛楃殑鏂规硶銆佷唬鐮佸潡涓紝涓€娆″彧鍏佽涓€涓嚎绋嬭繘鍏ョ壒瀹氫唬鐮佹锛屼粠鑰岄伩鍏嶅绾跨▼鍚屾椂淇敼鍚屼竴鏁版嵁銆?/span>
synchronized閿佹湁濡備笅鍑犱釜鐗圭偣锛?/span>
a銆佹湁閿佸崌绾ц繃绋?/span>
鍦╦dk1.5(鍚?涔嬪墠锛宻ynchronized鐨勫簳灞傚疄鐜版槸閲嶉噺绾х殑锛屾墍浠ヤ箣鍓嶄竴鑷寸О鍛煎畠涓?閲嶉噺绾ч攣"锛屽湪jdk1.5涔嬪悗锛屽synchronized杩涜浜嗗悇绉嶄紭鍖栵紝瀹冨彉寰椾笉閭d箞閲嶄簡锛屽疄鐜板師鐞嗗氨鏄攣鍗囩骇鐨勮繃绋嬨€?/span>
鎴戜滑鍏堣亰鑱?.5涔嬪悗鐨剆ynchronized瀹炵幇鍘熺悊鏄€庢牱鐨勶紝璇村埌synchronized鍔犻攣鍘熺悊锛屽氨涓嶅緱涓嶅厛璇磈ava瀵硅薄鍦ㄥ唴瀛樹腑鐨勫竷灞€锛宩ava瀵硅薄鍐呭瓨甯冨眬濡備笅:
濡備笂鍥炬墍绀猴紝鍦ㄥ垱寤轰竴涓璞″悗锛屽湪JVM铏氭嫙鏈?HotSpot)涓紝瀵硅薄鍦↗ava鍐呭瓨涓殑瀛樺偍甯冨眬 鍙垎涓轰笁鍧?
(1)瀵硅薄澶村尯鍩?/span>
姝ゅ瀛樺偍鐨勪俊鎭寘鎷袱閮ㄥ垎锛?/span>
瀵硅薄鑷韩鐨勮繍琛屾椂鏁版嵁(MarkWord)
瀛樺偍hashCode銆丟C鍒嗕唬骞撮緞銆侀攣绫诲瀷鏍囪銆佸亸鍚戦攣绾跨▼ID銆丆AS閿佹寚鍚戠嚎绋婰ockRecord鐨勬寚閽堢瓑锛宻ynconized閿佺殑鏈哄埗涓庤繖涓儴鍒?markwork)瀵嗗垏鐩稿叧锛岀敤markword涓渶浣庣殑涓変綅浠h〃閿佺殑鐘舵€侊紝鍏朵腑涓€浣嶆槸鍋忓悜閿佷綅锛屽彟澶栦袱浣嶆槸鏅€氶攣浣?/span>
瀵硅薄绫诲瀷鎸囬拡(Class Pointer)
瀵硅薄鎸囧悜瀹冪殑绫诲厓鏁版嵁鐨勬寚閽堛€丣VM灏辨槸閫氳繃瀹冩潵纭畾鏄摢涓狢lass鐨勫疄渚?/span>
(2)瀹炰緥鏁版嵁鍖哄煙
姝ゅ瀛樺偍鐨勬槸瀵硅薄鐪熸鏈夋晥鐨勪俊鎭紝姣斿瀵硅薄涓墍鏈夊瓧娈电殑鍐呭
(3)瀵归綈濉厖鍖哄煙
褰撶嚎绋嬭繘鍏ュ埌synchronized澶勫皾璇曡幏鍙栬閿佹椂锛宻ynchronized閿佸崌绾ф祦绋嬪涓嬶細
濡備笂鍥炬墍绀猴紝synchronized閿佸崌绾х殑椤哄簭涓猴細鍋忓悜閿?>杞婚噺绾ч攣->閲嶉噺绾ч攣锛屾瘡涓€姝ヨЕ鍙戦攣鍗囩骇鐨勬儏鍐靛涓嬶細
鍋忓悜閿?/span>
鍦↗DK1.8涓紝鍏跺疄榛樿鏄交閲忕骇閿侊紝浣嗗鏋滆瀹氫簡
-XX:BiasedLockingStartupDelay = 0
閭e湪瀵逛竴涓狾bject鍋歴yncronized鐨勬椂鍊欙紝浼氱珛鍗充笂涓€鎶婂亸鍚戦攣銆?/span>褰撳浜庡亸鍚戦攣鐘舵€佹椂锛宮arkwork浼氳褰曞綋鍓嶇嚎绋婭D
鍗囩骇鍒拌交閲忕骇閿?/strong>
褰撲笅涓€涓嚎绋嬪弬涓庡埌鍋忓悜閿佺珵浜夋椂锛屼細鍏堝垽鏂璵arkword涓繚瀛樼殑绾跨▼ID鏄惁涓庤繖涓嚎绋婭D鐩哥瓑锛屽鏋滀笉鐩哥瓑锛屼細绔嬪嵆鎾ら攢鍋忓悜閿侊紝鍗囩骇涓鸿交閲忕骇閿併€傛瘡涓嚎绋嬪湪鑷繁鐨勭嚎绋嬫爤涓敓鎴愪竴涓狶ockRecord(LR)锛岀劧鍚庢瘡涓嚎绋嬮€氳繃CAS(鑷棆)鐨勬搷浣滃皢閿佸璞″ご涓殑markwork璁剧疆涓烘寚鍚戣嚜宸辩殑LR鐨勬寚閽堬紝鍝釜绾跨▼璁剧疆鎴愬姛锛屽氨鎰忓懗鐫€鑾峰緱閿併€?/span>
鍏充簬synchronized涓鏃舵墽琛岀殑CAS鎿嶄綔鏄€氳繃native鐨勮皟鐢℉otSpot涓璪ytecodeInterpreter.cpp鏂囦欢C++浠g爜瀹炵幇鐨勶紝鏈夊叴瓒g殑鍙互缁х画娣辨寲銆?/span>
鍗囩骇鍒伴噸閲忕骇閿?/strong>
濡傛灉閿佺珵浜夊姞鍓?濡傜嚎绋嬭嚜鏃嬫鏁版垨鑰呰嚜鏃嬬殑绾跨▼鏁拌秴杩囨煇闃堝€硷紝JDK1.6涔嬪悗锛岀敱JVM鑷繁鎺у埗鏀硅鍒?锛屽氨浼氬崌绾т负閲嶉噺绾ч攣銆傛鏃跺氨浼氬悜鎿嶄綔绯荤粺鐢宠璧勬簮锛岀嚎绋嬫寕璧凤紝杩涘叆鍒版搷浣滅郴缁熷唴鏍告€佺殑绛夊緟闃熷垪涓紝绛夊緟鎿嶄綔绯荤粺璋冨害锛岀劧鍚庢槧灏勫洖鐢ㄦ埛鎬併€傚湪閲嶉噺绾ч攣涓紝鐢变簬闇€瑕佸仛鍐呮牳鎬佸埌鐢ㄦ埛鎬佺殑杞崲锛岃€岃繖涓繃绋嬩腑闇€瑕佹秷鑰楄緝澶氭椂闂达紝涔熷氨鏄?閲?鐨勫師鍥犱箣涓€銆?/span>
b銆佸彲閲嶅叆
synchronized鎷ユ湁寮哄埗鍘熷瓙鎬х殑鍐呴儴閿佹満鍒讹紝鏄竴鎶婂彲閲嶅叆閿併€傚洜姝わ紝鍦ㄤ竴涓嚎绋嬩娇鐢╯ynchronized鏂规硶鏃惰皟鐢ㄨ瀵硅薄鍙︿竴涓猻ynchronized鏂规硶锛屽嵆涓€涓嚎绋嬪緱鍒颁竴涓璞¢攣鍚庡啀娆¤姹傝瀵硅薄閿侊紝鏄案杩滃彲浠ユ嬁鍒伴攣鐨勩€傚湪Java涓嚎绋嬭幏寰楀璞¢攣鐨勬搷浣滄槸浠ョ嚎绋嬩负鍗曚綅鐨勶紝鑰屼笉鏄互璋冪敤涓哄崟浣嶇殑銆?/span>
synchronized閿佺殑瀵硅薄澶寸殑markwork涓細璁板綍璇ラ攣鐨勭嚎绋嬫寔鏈夎€呭拰璁℃暟鍣紝褰撲竴涓嚎绋嬭姹傛垚鍔熷悗锛孞VM浼氳涓嬫寔鏈夐攣鐨勭嚎绋嬶紝骞跺皢璁℃暟鍣ㄨ涓?銆傛鏃跺叾浠栫嚎绋嬭姹傝閿侊紝鍒欏繀椤荤瓑寰呫€傝€岃鎸佹湁閿佺殑绾跨▼濡傛灉鍐嶆璇锋眰杩欎釜閿侊紝灏卞彲浠ュ啀娆℃嬁鍒拌繖涓攣锛屽悓鏃惰鏁板櫒浼氶€掑銆?/span>
褰撶嚎绋嬮€€鍑轰竴涓猻ynchronized鏂规硶/鍧楁椂锛岃鏁板櫒浼氶€掑噺锛屽鏋滆鏁板櫒涓?鍒欓噴鏀捐閿侀攣銆?/span>
c銆佹偛瑙傞攣(浜掓枼閿併€佹帓浠栭攣)
synchronized鏄竴鎶婃偛瑙傞攣(鐙崰閿?锛屽綋鍓嶇嚎绋嬪鏋滆幏鍙栧埌閿侊紝浼氬鑷村叾瀹冩墍鏈夐渶瑕侀攣璇ョ殑绾跨▼绛夊緟锛屼竴鐩寸瓑寰呮寔鏈夐攣鐨勭嚎绋嬮噴鏀鹃攣鎵嶇户缁繘琛岄攣鐨勪簤鎶?/span>
ReentrantLock
ReentrantLock浠庡瓧闈㈠彲浠ョ湅鍑烘槸涓€鎶婂彲閲嶅叆閿侊紝杩欑偣鍜宻ynchronized涓€鏍凤紝浣嗗疄鐜板師鐞嗕篃涓巗yncronized鏈夊緢澶у樊鍒€?/span>
瀹冩槸鍩轰簬缁忓吀鐨凙QS(AbstractQueueSyncronized)瀹炵幇鐨?AQS鏄熀浜巚olitale鍜孋AS瀹炵幇鐨勩€?/span>鍏朵腑AQS涓淮鎶や竴涓獀alitale绫诲瀷鐨勫彉閲弒tate鏉ュ仛涓€涓彲閲嶅叆閿佺殑閲嶅叆娆℃暟锛屽姞閿佸拰閲婃斁閿佷篃鏄洿缁曡繖涓彉閲忔潵杩涜鐨勩€?/span>ReentrantLock涔熸彁渚涗簡涓€浜泂ynchronized娌℃湁鐨勭壒鐐癸紝鍥犳姣攕ynchronized濂界敤銆?/span>
AQS妯″瀷濡備笅鍥撅細
ReentrantLock鏈夊涓嬬壒鐐癸細
a銆佸彲閲嶅叆
ReentrantLock鍜宻yncronized鍏抽敭瀛椾竴鏍凤紝閮芥槸鍙噸鍏ラ攣锛屼笉杩囦袱鑰呭疄鐜板師鐞嗙◢鏈夊樊鍒紝RetrantLock鍒╃敤AQS鐨勭殑state鐘舵€佹潵鍒ゆ柇璧勬簮鏄惁宸查攣锛屽悓涓€绾跨▼閲嶅叆鍔犻攣锛宻tate鐨勭姸鎬?1; 鍚屼竴绾跨▼閲嶅叆瑙i攣,state鐘舵€?1(瑙i攣蹇呴』涓哄綋鍓嶇嫭鍗犵嚎绋嬶紝鍚﹀垯寮傚父); 褰搒tate涓?鏃惰В閿佹垚鍔熴€?/span>
b銆侀渶瑕佹墜鍔ㄥ姞閿併€佽В閿?/strong>
synchronized鍏抽敭瀛楁槸鑷姩杩涜鍔犻攣銆佽В閿佺殑锛岃€孯eentrantLock闇€瑕乴ock()鍜寀nlock()鏂规硶閰嶅悎try/finally璇彞鍧楁潵瀹屾垚锛屾潵鎵嬪姩鍔犻攣銆佽В閿併€?/span>
c銆佹敮鎸佽缃攣鐨勮秴鏃舵椂闂?/span>
synchronized鍏抽敭瀛楁棤娉曡缃攣鐨勮秴鏃舵椂闂达紝濡傛灉涓€涓幏寰楅攣鐨勭嚎绋嬪唴閮ㄥ彂鐢熸閿侊紝閭d箞鍏朵粬绾跨▼灏变細涓€鐩磋繘鍏ラ樆濉炵姸鎬侊紝鑰孯eentrantLock鎻愪緵tryLock鏂规硶锛屽厑璁歌缃嚎绋嬭幏鍙栭攣鐨勮秴鏃舵椂闂达紝濡傛灉瓒呮椂锛屽垯璺宠繃锛屼笉杩涜浠讳綍鎿嶄綔锛岄伩鍏嶆閿佺殑鍙戠敓銆?/span>
d銆佹敮鎸佸叕骞?闈炲叕骞抽攣
synchronized鍏抽敭瀛楁槸涓€绉嶉潪鍏钩閿侊紝鍏堟姠鍒伴攣鐨勭嚎绋嬪厛鎵ц銆傝€孯eentrantLock鐨勬瀯閫犳柟娉曚腑鍏佽璁剧疆true/false鏉ュ疄鐜板叕骞炽€侀潪鍏钩閿侊紝濡傛灉璁剧疆涓簍rue锛屽垯绾跨▼鑾峰彇閿佽閬靛惊"鍏堟潵鍚庡埌"鐨勮鍒欙紝姣忔閮戒細鏋勯€犱竴涓嚎绋婲ode锛岀劧鍚庡埌鍙屽悜閾捐〃鐨?灏惧反"鍚庨潰鎺掗槦锛岀瓑寰呭墠闈㈢殑Node閲婃斁閿佽祫婧愩€?/span>
e銆佸彲涓柇閿?/span>
ReentrantLock涓殑lockInterruptibly()鏂规硶浣垮緱绾跨▼鍙互鍦ㄨ闃诲鏃跺搷搴斾腑鏂紝姣斿涓€涓嚎绋媡1閫氳繃lockInterruptibly()鏂规硶鑾峰彇鍒颁竴涓彲閲嶅叆閿侊紝骞舵墽琛屼竴涓暱鏃堕棿鐨勪换鍔★紝鍙︿竴涓嚎绋嬮€氳繃interrupt()鏂规硶灏卞彲浠ョ珛鍒绘墦鏂璽1绾跨▼鐨勬墽琛岋紝鏉ヨ幏鍙杢1鎸佹湁鐨勯偅涓彲閲嶅叆閿併€?/span>
鑰岄€氳繃ReentrantLock鐨刲ock()鏂规硶鎴栬€匰ynchronized鎸佹湁閿佺殑绾跨▼鏄笉浼氬搷搴斿叾浠栫嚎绋嬬殑interrupt()鏂规硶鐨勶紝鐩村埌璇ユ柟娉曚富鍔ㄩ噴鏀鹃攣涔嬪悗鎵嶄細鍝嶅簲interrupt()鏂规硶銆?/span>
ReentrantReadWriteLock
ReentrantReadWriteLock(璇诲啓閿?鍏跺疄鏄袱鎶婇攣锛?/span>
涓€鎶婃槸WriteLock(鍐欓攣)
涓€鎶婃槸璇婚攣锛孯eadLock
璇诲啓閿佺殑瑙勫垯鏄細璇昏涓嶄簰鏂ャ€佽鍐欎簰鏂ャ€佸啓鍐欎簰鏂ャ€傚湪涓€浜涘疄闄呯殑鍦烘櫙涓紝璇绘搷浣滅殑棰戠巼杩滆繙楂樹簬鍐欐搷浣滐紝濡傛灉鐩存帴鐢ㄤ竴鑸殑閿佽繘琛屽苟鍙戞帶鍒剁殑璇濓紝灏变細璇昏浜掓枼銆佽鍐欎簰鏂ャ€佸啓鍐欎簰鏂ワ紝鏁堢巼浣庝笅锛岃鍐欓攣鐨勪骇鐢熷氨鏄负浜嗕紭鍖栬繖绉嶅満鏅殑鎿嶄綔鏁堢巼銆備竴鑸儏鍐典笅鐙崰閿佺殑鏁堢巼浣庢潵婧愪簬楂樺苟鍙戜笅瀵逛复鐣屽尯鐨勬縺鐑堢珵浜夊鑷寸嚎绋嬩笂涓嬫枃鍒囨崲銆傚洜姝ゅ綋骞跺彂涓嶆槸寰堥珮鐨勬儏鍐典笅锛岃鍐欓攣鐢变簬闇€瑕侀澶栫淮鎶よ閿佺殑鐘舵€侊紝鍙兘杩樹笉濡傜嫭鍗犻攣鐨勬晥鐜囬珮銆傚洜姝ら渶瑕佹牴鎹疄闄呮儏鍐甸€夋嫨浣跨敤銆?/span>
ReentrantReadWriteLock鐨勫師鐞嗕篃鏄熀浜嶢QS杩涜瀹炵幇鐨勩€?/span>
涓嶳eentrantLock鐨勫樊鍒湪浜嶳eentrantReadWriteLock閿佹嫢鏈夊叡浜攣銆佹帓浠栭攣灞炴€с€傝鍐欓攣涓殑鍔犻攣銆侀噴鏀鹃攣涔熸槸鍩轰簬Sync(缁ф壙浜嶢QS)锛屽苟涓斾富瑕佷娇鐢ˋQS涓殑state鍜宯ode涓殑waitState鍙橀噺杩涜瀹炵幇鐨勩€?/span>
瀹炵幇璇诲啓閿佷笌瀹炵幇鏅€氫簰鏂ラ攣鐨勪富瑕佸尯鍒湪浜庨渶瑕佸垎鍒褰曡閿佺姸鎬佸強鍐欓攣鐘舵€侊紝骞朵笖绛夊緟闃熷垪涓渶瑕佸尯鍒鐞嗕袱绉嶅姞閿佹搷浣溿€俁eentrantReadWriteLock涓皢AQS涓殑int绫诲瀷鐨剆tate鍒嗕负楂?6浣嶄笌绗?6浣嶅垎鍒褰曡閿佸拰鍐欓攣鐨勭姸鎬侊紝濡備笅鍥炬墍绀猴細
a銆乄riteLock(鍐欓攣)鏄偛瑙傞攣(鎺掍粬閿併€佷簰鏂ラ攣)
閫氳繃璁$畻 state&((1<<16)-1)锛屽皢state鐨勯珮16浣嶅叏閮ㄦ姽鍘伙紝鍥犳state鐨勪綆浣嶈褰曠潃鍐欓攣鐨勯噸鍏ヨ鏁?/span>
鑾峰彇鍐欓攣婧愮爜锛?/span>
閲婃斁鍐欓攣婧愮爜锛?/span>
b銆丷eadLock(璇婚攣)鏄叡浜攣(涔愯閿?
閫氳繃璁$畻 state>>>16 杩涜鏃犵鍙疯ˉ0锛屽彸绉?6浣嶏紝鍥犳state鐨勯珮浣嶈褰曠潃鍐欓攣鐨勯噸鍏ヨ鏁般€?/span>
璇婚攣鑾峰彇閿佺殑杩囩▼姣斿啓閿佺◢寰鏉備簺锛?/span>棣栧厛鍒ゆ柇鍐欓攣鏄惁涓?骞朵笖褰撳墠绾跨▼涓嶅崰鏈夌嫭鍗犻攣锛岀洿鎺ヨ繑鍥烇紱鍚?/span>鍒欙紝鍒ゆ柇璇荤嚎绋嬫槸鍚﹂渶瑕佽闃诲骞朵笖璇婚攣鏁伴噺鏄惁灏忎簬鏈€澶у€煎苟涓旀瘮杈冭缃姸鎬佹垚鍔燂細
鑻ュ綋鍓嶆病鏈夎閿侊紝鍒欒缃涓€涓绾跨▼锛?/span>
firstReader
firstReaderHoldCount锛?/span>
鑻ュ綋鍓嶇嚎绋嬬嚎绋嬩负绗竴涓绾跨▼锛屽垯澧炲姞锛?/span>
firstReaderHoldCount
鍚﹀垯锛屽皢璁剧疆褰撳墠绾跨▼瀵瑰簲鐨凥oldCounter瀵硅薄鐨勫€笺€?/span>
鏇存柊鎴愬姛鍚庝細鍦?/span>readHolds(ThreadLocal绫诲瀷鐨?鐨勬湰绾跨▼鍓湰涓褰曞綋鍓嶇嚎绋嬮噸鍏ユ暟锛岃繖鏄负浜嗗疄鐜癹dk1.6涓姞鍏ョ殑getReadHoldCount()鏂规硶鐨勶紝杩欎釜鏂规硶鑳借幏鍙栧綋鍓嶇嚎绋嬮噸鍏ュ叡浜攣鐨勬鏁?state涓褰曠殑鏄涓嚎绋嬬殑鎬婚噸鍏ユ鏁?锛屽姞鍏ヤ簡杩欎釜鏂规硶璁╀唬鐮佸鏉備簡涓嶅皯銆?/span>
浣嗘槸鍏跺師鐞嗚繕鏄緢绠€鍗曠殑锛?/span>
濡傛灉褰撳墠鍙湁涓€涓嚎绋嬬殑璇濓紝杩樹笉闇€瑕佸姩鐢═hreadLocal锛?/span>
鐩存帴寰€firstReaderHoldCount杩欎釜鎴愬憳鍙橀噺閲屽瓨閲嶅叆鏁帮紱
褰撴湁绗簩涓嚎绋嬫潵鐨勬椂鍊欙紝灏辫鍔ㄧ敤ThreadLocal鍙橀噺readHolds浜嗭紝姣忎釜绾跨▼鎷ユ湁鑷繁鐨勫壇鏈紝鐢ㄦ潵淇濆瓨鑷繁鐨勯噸鍏ユ暟銆?/span>
鑾峰彇璇婚攣婧愮爜锛?nbsp;
閲婃斁璇婚攣婧愮爜锛?/span>
閫氳繃鍒嗘瀽鍙互鐪嬪嚭锛?/span>
鍦ㄧ嚎绋嬫寔鏈夎閿佺殑鎯呭喌涓嬶紝璇ョ嚎绋嬩笉鑳藉彇寰楀啓閿?鍥犱负鑾峰彇鍐欓攣鐨勬椂鍊欙紝濡傛灉鍙戠幇褰撳墠鐨勮閿佽鍗犵敤锛屽氨椹笂鑾峰彇澶辫触锛屼笉绠¤閿佹槸涓嶆槸琚綋鍓嶇嚎绋嬫寔鏈?銆?/span>
鍦ㄧ嚎绋嬫寔鏈夊啓閿佺殑鎯呭喌涓嬶紝璇ョ嚎绋嬪彲浠ョ户缁幏鍙栬閿侊紙鑾峰彇璇婚攣鏃跺鏋滃彂鐜板啓閿佽鍗犵敤锛屽彧鏈夊啓閿佹病鏈夎褰撳墠绾跨▼鍗犵敤鐨勬儏鍐垫墠浼氳幏鍙栧け璐ワ級銆?/span>
LongAdder
鍦ㄩ珮骞跺彂鐨勬儏鍐典笅锛屾垜浠涓€涓狪nteger绫诲瀷鐨勬暣鏁扮洿鎺ヨ繘琛宨++鐨勬椂鍊欙紝鏃犳硶淇濊瘉鎿嶄綔鐨勫師瀛愭€э紝浼氬嚭鐜扮嚎绋嬪畨鍏ㄧ殑闂銆備负姝ゆ垜浠細鐢╦uc涓嬬殑AtomicInteger锛屽畠鏄竴涓彁渚涘師瀛愭搷浣滅殑Interger绫伙紝鍐呴儴涔熸槸閫氳繃CAS瀹炵幇绾跨▼瀹夊叏鐨勩€?/span>
浣嗗綋澶ч噺绾跨▼鍚屾椂鍘昏闂椂锛屽氨浼氬洜涓哄ぇ閲忕嚎绋嬫墽琛孋AS鎿嶄綔澶辫触鑰岃繘琛岀┖鏃嬭浆锛屽鑷碈PU璧勬簮娑堣€楄繃澶氾紝鑰屼笖鎵ц鏁堢巼涔熶笉楂樸€侱oug Lea澶х搴旇涔熶笉婊℃剰锛屼簬鏄湪JDK1.8涓CAS杩涜浜嗕紭鍖栵紝鎻愪緵浜哃ongAdder锛屽畠鏄熀浜庝簡CAS鍒嗘閿佺殑鎬濇兂瀹炵幇鐨勩€?/span>
绾跨▼鍘昏鍐欎竴涓狶ongAdder绫诲瀷鐨勫彉閲忔椂锛屾祦绋嬪涓嬶細
LongAdder涔熸槸鍩轰簬Unsafe鎻愪緵鐨凜AS鎿嶄綔+valitale鍘诲疄鐜扮殑銆?/span>鍦↙ongAdder鐨勭埗绫籗triped64涓淮鎶ょ潃涓€涓猙ase鍙橀噺鍜屼竴涓猚ell鏁扮粍锛屽綋澶氫釜绾跨▼鎿嶄綔涓€涓彉閲忕殑鏃跺€欙紝鍏堜細鍦ㄨ繖涓猙ase鍙橀噺涓婅繘琛宑as鎿嶄綔锛屽綋瀹冨彂鐜扮嚎绋嬪澶氱殑鏃跺€欙紝灏变細浣跨敤cell鏁扮粍銆?/span>
姣斿褰揵ase灏嗚鏇存柊鐨勬椂鍊欏彂鐜扮嚎绋嬪澶氾紙涔熷氨鏄皟鐢╟asBase鏂规硶鏇存柊base鍊煎け璐ワ級锛岄偅涔堝畠浼氳嚜鍔ㄤ娇鐢╟ell鏁扮粍锛屾瘡涓€涓嚎绋嬪搴斾簬涓€涓猚ell锛屽湪姣忎竴涓嚎绋嬩腑瀵硅cell杩涜cas鎿嶄綔锛岃繖鏍峰氨鍙互灏嗗崟涓€value鐨勬洿鏂板帇鍔涘垎鎷呭埌澶氫釜value涓幓锛岄檷浣庡崟涓獀alue鐨?鈥滅儹搴︹€濓紝鍚屾椂涔熷噺灏戜簡绾跨▼澶ч噺绾跨▼鐨勭┖杞紝鎻愰珮骞跺彂鏁堢巼锛屽垎鏁e苟鍙戝帇鍔涖€?/span>杩欑鍒嗘閿侀渶瑕侀澶栫淮鎶や竴涓唴瀛樼┖闂碿ells锛屼笉杩囧湪楂樺苟鍙戝満鏅笅锛岃繖鐐规垚鏈嚑涔庡彲浠ュ拷鐣ャ€?/span>
鍒嗘閿佹槸涓€绉嶄紭绉€鐨勪紭鍖栨€濇兂锛宩uc涓彁渚涚殑鐨凜oncurrentHashMap涔熸槸鍩轰簬鍒嗘閿佷繚璇佽鍐欐搷浣滅殑绾跨▼瀹夊叏銆?/span>
鍦ㄨ繖閲岋紝鎴戜负澶у鍑嗗浜嗕竴浠?020骞存渶鏂版渶鍏ㄧ殑銆奐ava闈㈣瘯棰樺強绛旀V3.0銆嬶紝杩欏鐢靛瓙涔︽兜鐩栦簡璇稿鍚庣鎶€鏈爤鐨勯潰璇曢鍜岀瓟妗堬紝鐩镐俊鍙互甯姪澶у鍦ㄦ渶鐭殑鏃堕棿鍐呭涔燡ava鍚庣鐨勫ぇ澶氭暟闈㈣瘯棰橈紝浠庤€屾嬁鍒拌嚜宸卞績浠殑offer銆?/p>
鎵弿涓嬫柟浜岀淮鐮佸姞鎴戝井淇?/span>
鍚庡彴鍥炲鍏抽敭璇嶏細Java鏍稿績鏁寸悊
以上是关于闈㈣瘯瀹橈細鑱婅亰 Java 鐨勫嚑鎶?JVM 绾ч攣鍚?/h1>