Java AQS
java.util.concurrent.locks.AbstractQueuedSynchronizer
鎶借薄绫伙紝绠€绉?AQS 锛屾槸涓€涓敤浜庢瀯寤洪攣鍜屽悓姝ュ鍣ㄧ殑鍚屾鍣ㄣ€備簨瀹炰笂concurrent
鍖呭唴璁稿绫婚兘鏄熀浜?AQS 鏋勫缓銆備緥濡?ReentrantLock锛孲emaphore锛孋ountDownLatch锛孯eentrantReadWriteLock锛岀瓑銆侫QS 瑙e喅浜嗗湪瀹炵幇鍚屾瀹瑰櫒鏃惰璁$殑澶ч噺缁嗚妭闂銆?/p>
AQS 浣跨敤涓€涓?FIFO 鐨勯槦鍒楄〃绀烘帓闃熺瓑寰呴攣鐨勭嚎绋嬶紝闃熷垪澶磋妭鐐圭О浣溾€滃摠鍏佃妭鐐光€濇垨鑰呪€滃搼鑺傜偣鈥濓紝瀹冧笉涓庝换浣曠嚎绋嬪叧鑱斻€傚叾浠栫殑鑺傜偣涓庣瓑寰呯嚎绋嬪叧鑱旓紝姣忎釜鑺傜偣缁存姢涓€涓瓑寰呯姸鎬?waitStatus
銆?/p>
- Java骞跺彂涔婣QS璇﹁В
浠€涔堟槸 Java Lock 鎺ュ彛锛?/h2>
java.util.concurrent.locks.Lock
鎺ュ彛锛屾瘮 synchronized
鎻愪緵鏇村叿鎷撳睍琛岀殑閿佹搷浣溿€傚畠鍏佽鏇寸伒娲荤殑缁撴瀯锛屽彲浠ュ叿鏈夊畬鍏ㄤ笉鍚岀殑鎬ц川锛屽苟涓斿彲浠ユ敮鎸佸涓浉鍏崇被鐨勬潯浠跺璞°€傚畠鐨勪紭鍔挎湁锛?/p>
- 鍙互浣块攣鏇村叕骞炽€?/li>
- 鍙互浣跨嚎绋嬪湪绛夊緟閿佺殑鏃跺€欏搷搴斾腑鏂€?/li>
- 鍙互璁╃嚎绋嬪皾璇曡幏鍙栭攣锛屽苟鍦ㄦ棤娉曡幏鍙栭攣鐨勬椂鍊欑珛鍗宠繑鍥炴垨鑰呯瓑寰呬竴娈垫椂闂淬€?/li>
- 鍙互鍦ㄤ笉鍚岀殑鑼冨洿锛屼互涓嶅悓鐨勯『搴忚幏鍙栧拰閲婃斁閿併€?/li>
浠€涔堟槸鍙噸鍏ラ攣锛圧eentrantLock锛夛紵
synchronized
銆丷eentrantLock 閮芥槸鍙噸鍏ョ殑閿侊紝鍙噸鍏ラ攣鐩稿鏉ヨ绠€鍖栦簡骞跺彂缂栫▼鐨勫紑鍙戙€?/p>
鍏充簬 ReentrantLock 绫伙紝璇︾粏鐨勬簮鐮佽В鏋愶紝鍙互鐪嬬湅 銆娿€愭纾?Java 骞跺彂銆戔€斺€?J.U.C 涔嬮噸鍏ラ攣锛歊eentrantLock銆?銆?/p>
绠€鍗曟潵璇达紝ReenTrantLock 鐨勫疄鐜版槸涓€绉嶈嚜鏃嬮攣锛岄€氳繃寰幆璋冪敤 CAS 鎿嶄綔鏉ュ疄鐜板姞閿併€傚畠鐨勬€ц兘姣旇緝濂戒篃鏄洜涓洪伩鍏嶄簡浣跨嚎绋嬭繘鍏ュ唴鏍告€佺殑闃诲鐘舵€併€傛兂灏藉姙娉曢伩鍏嶇嚎绋嬭繘鍏ュ唴鏍哥殑闃诲鐘舵€佹槸鎴戜滑鍘诲垎鏋愬拰鐞嗚В閿佽璁$殑鍏抽敭閽ュ寵銆?/p>
馃 synchronized 鍜?ReentrantLock 寮傚悓锛?/strong>
- 鐩稿悓鐐?
- 閮藉疄鐜颁簡澶氱嚎绋嬪悓姝ュ拰鍐呭瓨鍙鎬ц涔夈€?/li>
- 閮芥槸鍙噸鍏ラ攣銆?/li>
- 涓嶅悓鐐?
-
鍚屾瀹炵幇鏈哄埗涓嶅悓
-
synchronized
閫氳繃 Java 瀵硅薄澶撮攣鏍囪鍜?Monitor 瀵硅薄瀹炵幇鍚屾銆?/li> - ReentrantLock 閫氳繃CAS銆丄QS锛圓bstractQueuedSynchronizer锛夊拰 LockSupport锛堢敤浜庨樆濉炲拰瑙i櫎闃诲锛夊疄鐜板悓姝ャ€?/li>
-
-
鍙鎬у疄鐜版満鍒朵笉鍚?/p>
-
synchronized
渚濊禆 JVM 鍐呭瓨妯″瀷淇濊瘉鍖呭惈鍏变韩鍙橀噺鐨勫绾跨▼鍐呭瓨鍙鎬с€?/li> - ReentrantLock 閫氳繃 ASQ 鐨?
volatile state
淇濊瘉鍖呭惈鍏变韩鍙橀噺鐨勫绾跨▼鍐呭瓨鍙鎬с€?/li>
-
-
浣跨敤鏂瑰紡涓嶅悓
-
synchronized
鍙互淇グ瀹炰緥鏂规硶锛堥攣浣忓疄渚嬪璞★級銆侀潤鎬佹柟娉曪紙閿佷綇绫诲璞★級銆佷唬鐮佸潡锛堟樉绀烘寚瀹氶攣瀵硅薄锛夈€?/li> - ReentrantLock 鏄剧ず璋冪敤 tryLock 鍜?lock 鏂规硶锛岄渶瑕佸湪
finally
鍧椾腑閲婃斁閿併€?/li>
-
-
鍔熻兘涓板瘜绋嬪害涓嶅悓
-
synchronized
涓嶅彲璁剧疆绛夊緟鏃堕棿銆佷笉鍙涓柇锛坕nterrupted锛夈€?/li> - ReentrantLock 鎻愪緵鏈夐檺鏃堕棿绛夊€欓攣锛堣缃繃鏈熸椂闂达級銆佸彲涓柇閿侊紙lockInterruptibly锛夈€乧ondition锛堟彁渚?await銆乧ondition锛堟彁渚?await銆乻ignal 绛夋柟娉曪級绛変赴瀵屽姛鑳?/li>
-
-
閿佺被鍨嬩笉鍚?/p>
-
synchronized
鍙敮鎸侀潪鍏钩閿併€?/li> - ReentrantLock 鎻愪緵鍏钩閿佸拰闈炲叕骞抽攣瀹炵幇銆傚綋鐒讹紝鍦ㄥぇ閮ㄥ垎鎯呭喌涓嬶紝闈炲叕骞抽攣鏄珮鏁堢殑閫夋嫨銆?/li>
-
-
鍦?
synchronized
浼樺寲浠ュ墠锛屽畠鐨勬€ц兘鏄瘮 ReenTrantLock 宸緢澶氱殑锛屼絾鏄嚜浠?synchronized
寮曞叆浜嗗亸鍚戦攣锛岃交閲忕骇閿侊紙鑷棆閿侊級鍚庯紝涓よ€呯殑鎬ц兘灏卞樊涓嶅浜嗭紝鍦ㄤ袱绉嶆柟娉曢兘鍙敤鐨勬儏鍐典笅锛屽畼鏂圭敋鑷冲缓璁娇鐢?synchronized
銆?/p>骞朵笖锛屽疄闄呬唬鐮佸疄鎴樹腑锛屽彲鑳界殑浼樺寲鍦烘櫙鏄紝閫氳繃璇诲啓鍒嗙锛岃繘涓€姝ユ€ц兘鐨勬彁鍗囷紝鎵€浠ヤ娇鐢?ReentrantReadWriteLock 銆傪煒?/p>
ReadWriteLock 鏄粈涔堬紵
ReadWriteLock 锛岃鍐欓攣鏄紝鐢ㄦ潵鎻愬崌骞跺彂绋嬪簭鎬ц兘鐨勯攣鍒嗙鎶€鏈殑 Lock 瀹炵幇绫汇€傚彲浠ョ敤浜?鈥滃璇诲皯鍐欌€?鐨勫満鏅紝璇诲啓閿佹敮鎸佸涓鎿嶄綔骞跺彂鎵ц锛屽啓鎿嶄綔鍙兘鐢变竴涓嚎绋嬫潵鎿嶄綔銆?/p>
ReadWriteLock 瀵瑰悜鏁版嵁缁撴瀯鐩稿涓嶉绻佸湴鍐欏叆锛屼絾鏄湁澶氫釜浠诲姟瑕佺粡甯歌鍙栬繖涓暟鎹粨鏋勭殑杩欑被鎯呭喌杩涜浜嗕紭鍖栥€俁eadWriteLock 浣垮緱浣犲彲浠ュ悓鏃舵湁澶氫釜璇诲彇鑰咃紝鍙瀹冧滑閮戒笉璇曞浘鍐欏叆鍗冲彲銆傚鏋滃啓閿佸凡缁忚鍏朵粬浠诲姟鎸佹湁锛岄偅涔堜换浣曡鍙栬€呴兘涓嶈兘璁块棶锛岀洿鑷宠繖涓啓閿佽閲婃斁涓烘銆?/p>
ReadWriteLock 瀵圭▼搴忔€ц兘鐨勬彁楂樹富瑕佸彈鍒朵簬濡備笅鍑犱釜鍥犵礌锛?/p>
- 鏁版嵁琚鍙栫殑棰戠巼涓庤淇敼鐨勯鐜囩浉姣旇緝鐨勭粨鏋溿€?/li>
- 璇诲彇鍜屽啓鍏ョ殑鏃堕棿
- 鏈夊灏戠嚎绋嬬珵浜?/li>
- 鏄惁鍦ㄥ澶勭悊鏈哄櫒涓婅繍琛?/li>
ReadWriteLock 鐨勬簮鐮佽В鏋愶紝鍙互鐪嬬湅 銆娿€愭纾?Java 骞跺彂銆戔€斺€?J.U.C 涔嬭鍐欓攣锛歊eentrantReadWriteLock銆?銆?/p>
Condition 鏄粈涔堬紵
鍦ㄦ病鏈?Lock 涔嬪墠锛屾垜浠娇鐢?synchronized
鏉ユ帶鍒跺悓姝ワ紝閰嶅悎 Object 鐨?#wait()
銆?code>#notify() 绛変竴绯诲垪鏂规硶鍙互瀹炵幇绛夊緟 / 閫氱煡妯″紡銆傚湪 Java SE 5 鍚庯紝Java 鎻愪緵浜?Lock 鎺ュ彛锛岀浉瀵逛簬 synchronized
鑰岃█锛孡ock 鎻愪緵浜嗘潯浠?Condition 锛屽绾跨▼鐨勭瓑寰呫€佸敜閱掓搷浣滄洿鍔犺缁嗗拰鐏垫椿銆備笅鍥炬槸 Condition 涓?Object 鐨勭洃瑙嗗櫒鏂规硶鐨勫姣旓紙鎽樿嚜銆奐ava骞跺彂缂栫▼鐨勮壓鏈€嬶級锛?/p>
- Condition 鐨勬簮鐮侊紝鍙互鐪嬬湅 銆娿€愭纾?Java 骞跺彂銆戔€斺€?J.U.C 涔?Condition銆?銆?/li>
馃 鐢ㄤ笁涓嚎绋嬫寜椤哄簭寰幆鎵撳嵃 abc 涓変釜瀛楁瘝锛屾瘮濡?abcabcabc 锛?/strong>
- 浣跨敤 Lock + Condition 鏉ュ疄鐜般€傚叿浣撲唬鐮侊紝鍙傜湅 銆婄敤涓変釜绾跨▼鎸夐『搴忓惊鐜墦鍗?abc 涓変釜瀛楁瘝锛屾瘮濡?abcabcabc銆?銆?/li>
LockSupport 鏄粈涔堬紵
LockSupport 鏄?JDK 涓瘮杈冨簳灞傜殑绫伙紝鐢ㄦ潵鍒涘缓閿佸拰鍏朵粬鍚屾宸ュ叿绫荤殑鍩烘湰绾跨▼闃诲銆?/p>
Java 閿佸拰鍚屾鍣ㄦ鏋剁殑鏍稿績 AQS(AbstractQueuedSynchronizer)锛屽氨鏄€氳繃璋冪敤
LockSupport#park()
鍜?LockSupport#unpark()
鏂规硶锛屾潵瀹炵幇绾跨▼鐨勯樆濉炲拰鍞ら啋鐨勩€?/p>LockSupport 寰堢被浼间簬浜屽厓淇″彿閲?鍙湁 1 涓鍙瘉鍙緵浣跨敤)锛屽鏋滆繖涓鍙繕娌℃湁琚崰鐢紝褰撳墠绾跨▼鑾峰彇璁稿彲骞剁户缁墽琛岋紱濡傛灉璁稿彲宸茬粡琚崰鐢紝褰撳墠绾跨▼闃诲锛岀瓑寰呰幏鍙栬鍙€?/p>
銆奐ava 骞跺彂缂栫▼ 鈥斺€?LockSupport銆?甯﹂儴鍒嗘簮鐮佽В鏋愩€?/p>