Posted 瀛楄妭鏁扮粍
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了相关的知识,希望对你有一定的参考价值。
鐩墠锛屽绾跨▼缂栫▼鍙互璇存槸鍦ㄥぇ閮ㄥ垎骞冲彴鍜屽簲鐢ㄤ笂閮介渶瑕佸疄鐜扮殑涓€涓熀鏈渶姹傘€傛湰绯诲垪鏂囩珷灏辨潵瀵?nbsp;Java 骞冲彴涓嬬殑澶氱嚎绋嬬紪绋嬬煡璇?/strong>杩涜璁茶В锛屼粠姒傚康鍏ラ棬銆?span class="mq-4">搴曞眰瀹炵幇鍒?span class="mq-5">涓婂眰搴旂敤閮戒細娑夊強鍒帮紝棰勮涓€鍏变細鏈変簲绡囨枃绔狅紝甯屾湜瀵逛綘鏈夋墍甯姪 馃槑馃槑
鏈瘒鏂囩珷鏄涓€绡囷紝鍏堟潵浠嬬粛涓?Java 澶氱嚎绋嬬殑鍩虹姒傚康浠ュ強闇€瑕侀潰瀵圭殑鎸戞垬锛屾槸鍚庣画鏂囩珷鐨勬暡闂ㄧ爾
涓€銆佸绾跨▼缂栫▼
鍋囪瀛樺湪涓変釜浜嬩欢锛堜簨浠禔銆佷簨浠禕銆佷簨浠禖锛夐渶瑕佹垜浠畬鎴愶紝姣忎釜浜嬩欢鍧囧寘鍚竴瀹氱殑鍓嶇疆澶勭悊鏃堕棿鍜岀瓑寰呭畬鎴愭椂闂达紝鍗虫瘡涓簨浠跺潎闇€瑕佸厛澶勭悊涓€瀹氭椂闂达紝澶勭悊瀹屾垚鍚庡啀绛夊緟涓€娈垫椂闂达紝绛夊緟杩囧悗璇ヤ簨浠跺氨绠椾綔宸插畬鎴愪簡銆傞偅涔堬紝鎴戜滑灏卞彲浠ラ噰鐢ㄤ笁绉嶄笉鍚岀殑鏂瑰紡鏉ュ畬鎴愯繖涓変釜浜嬩欢锛?/p>
鈥?/span>涓茶銆傛寜鐓ч『搴忎緷娆℃潵澶勭悊涓変釜浜嬩欢锛屽緟鏌愪釜浜嬩欢澶勭悊涓旂瓑寰呯粨鏉熷悗鍐嶅鐞嗕笅涓€涓簨浠躲€傝繖绉嶆柟寮忛渶瑕佹秷鑰椾竴涓汉鍔?/span>鈥?/span>骞跺彂銆傚厛澶勭悊浜嬩欢 A锛屽綋浜嬩欢 A 鐨勫墠缃鐞嗗畬鎴愬悗锛岃浆鑰屾潵澶勭悊浜嬩欢 B锛屽綋浜嬩欢 B 鐨勫墠缃鐞嗗畬鎴愬悗锛岃浆鑰屾潵澶勭悊浜嬩欢 C锛屾渶鍚庡氨鍙绛夊緟涓変釜浜嬩欢缁撴潫鍗冲彲銆傝繖绉嶆柟寮忛渶瑕佹秷鑰椾竴涓汉鍔?/span>鈥?/span>骞惰銆備笁涓簨浠跺垎鍒浆浜ょ敱涓変釜浜鸿繘琛屽悓鏃跺鐞嗐€傝繖绉嶆柟寮忛渶瑕佹秷鑰椾笁涓汉鍔?/span>
浠庣洿瑙備笂鐪嬶紝涓茶鐨勫鐞嗘晥鐜囨渶浣庯紝鑰楁椂鏈€闀匡紝鍦ㄦ瘡娆$瓑寰呬簨浠跺畬鎴愮殑鏃堕棿娈靛唴浜哄姏閮借鐧界櫧娑堣€椾簡銆傚苟琛岀殑澶勭悊鏁堢巼鏈€楂橈紝鑰楁椂鏈€鐭紝鐞嗚涓婃€荤殑鎵€闇€鑰楁椂鍙栧喅浜庣敤鏃舵渶闀跨殑閭d釜浜嬩欢锛屼絾鍏堕渶瑕佺殑浜哄姏鎴愭湰涔熸渶楂樸€傚苟鍙戠殑澶勭悊鏁堢巼鍜岃€楁椂闀跨煭鍧囦粙浜庝覆琛屽拰骞惰涔嬮棿锛岄渶瑕佺殑浜哄姏鎴愭湰鍜屼覆琛屾寔骞筹紙鍧囦綆浜庡苟琛岋級
浠庝互涓婂亣璁剧殑鎯呮櫙鏄犲皠鍒拌蒋浠朵笘鐣?/p>
鈥?/span>骞跺彂灏辨槸鍦ㄤ竴娈垫椂闂村唴浠ヤ氦鏇跨殑鏂瑰紡鏉ュ畬鎴愬涓换鍔★紝浣跨敤澶氫釜绾跨▼鏉ュ垎鍒鐞嗕笉鍚岀殑浠诲姟锛屽嵆浣垮湪鍗曚釜澶勭悊鍣ㄧ殑鎯呭喌涓嬩篃鍙互閫氳繃鏃堕棿鐗囧垏鎹?/strong>鐨勬妧鏈潵瀹炵幇鍦ㄤ竴涓椂闂存鍐呰繍琛屽涓嚎绋嬶紝鍥犳鍗充娇鍙湁涓€涓鐞嗗櫒涔熷彲浠ュ疄鐜板苟鍙?/span>鈥?/span>骞惰灏辨槸浠ュ悓鏃跺鐞嗙殑鏂瑰紡鏉ュ畬鎴愬涓换鍔★紝浣跨敤澶氫釜绾跨▼鏉ュ垎鍒鐞嗕笉鍚岀殑浠诲姟锛岀劧鍚庡皢澶氫釜绾跨▼鍒嗗埆杞氦缁欎笉鍚岀殑澶勭悊鍣ㄨ繘琛岃繍琛岋紝鍥犳骞惰闇€瑕佹湁澶氫釜澶勭悊鍣ㄦ墠鍙互瀹炵幇鈥?/span>鑰屽湪鐜板疄鎯呭喌涓嬶紝绋嬪簭闇€瑕佸悓鏃舵墽琛岀殑绾跨▼鏁伴噺寰€寰€鏄繙澶氫簬澶勭悊鍣ㄧ殑鏁伴噺锛屽苟鍙戞墠鏄垜浠殑涓昏瀹炵幇鐩爣銆傚洜姝わ紝鎴戜滑鍙互杩欎箞鐞嗚В锛?span class="mq-26">瀹炵幇澶氱嚎绋嬬紪绋嬬殑杩囩▼灏辨槸灏嗕换鍔$殑鎵ц鏂瑰紡鐢变覆琛屾敼涓哄苟鍙戠殑杩囩▼锛屽嵆瀹炵幇骞跺彂鍖栵紝浠ユ鏉ュ敖閲忔彁楂樼▼搴忓拰纭欢鐨勮繍琛屾晥鐜?/strong>
閭d娇鐢ㄥ绾跨▼鏈変粈涔堝ソ澶勫憿锛?/p>
鈥?/span>瀵逛簬璁$畻鏈烘潵璇达紝鍏跺鐞嗗櫒鐨勮繍绠楅€熷害鐩告瘮瀛樺偍鍜岄€氫俊瀛愮郴缁熻蹇簡鍑犱釜鏁伴噺绾э紝濡傛灉鍙噰鐢ㄥ崟绾跨▼锛岄偅涔堝綋绾跨▼鍦ㄥ鐞嗙鐩?I/O銆佹暟鎹簱璁块棶绛変换鍔℃椂锛屽鐞嗗櫒灏辫闂茬疆鐫€娌℃湁娲诲共浜嗭紝杩欏氨閫犳垚浜嗗緢澶х殑鎬ц兘娴垂銆傛鏃跺氨鍙互閫氳繃閲囩敤澶氱嚎绋嬫潵浣垮鐞嗗櫒灏介噺澶勪簬杩愯浆鐘舵€侊紝灏介噺鍒╃敤鍒板叾杩愮畻鑳藉姏鈥?/span>姝ゅ锛屽浜庝竴涓湇鍔$锛岃 閲忓叾鎬ц兘楂樹綆濂藉潖鐨勪竴涓噸瑕佹爣鍑嗕箣涓€鏄?span class="mq-33">姣忕浜嬪姟澶勭悊鏁帮紙TPS锛夌殑澶у皬锛屽畠浠h〃鐫€涓€绉掑唴鏈嶅姟绔钩鍧囪兘鍝嶅簲鐨勮姹傛€绘暟銆傛湇鍔$鍙兘浼氬湪鏋佸皬鐨勬椂闂存鍐呮敹鍒板涓姹傦紝鏈嶅姟绔殑 TPS 灏卞拰绋嬪簭鐨勫苟鍙戣兘鍔涳紙鍗冲悓鏃跺鐞嗗椤逛换鍔$殑鑳藉姏锛夋湁鐫€瀵嗗垏鍏宠仈鈥?/span>鍐嶆瘮濡傦紝鍦?Android 搴旂敤寮€鍙戜腑锛岀郴缁熻瀹氫簡鍙湁 main 绾跨▼鎵嶅彲浠ヨ繘琛?UI 缁樺埗鍜屽埛鏂帮紝濡傛灉涓嶅皢鑰楁椂鎿嶄綔锛圛O璇诲啓銆佺綉缁滆姹傜瓑锛夋斁鍒板瓙绾跨▼杩涜澶勭悊锛岄偅涔堢敤鎴峰搴旂敤鐨?UI 鎿嶄綔琛屼负锛堢偣鍑诲睆骞曘€佹粦鍔ㄥ垪琛ㄧ瓑锛夊緢澶у彲鑳藉氨浼氱敱浜庢棤娉曞強鏃惰 main 绾跨▼澶勭悊锛屽鑷村簲鐢ㄤ技涔庤鍗′綇浜嗭紝鏈€缁堢敤鎴峰彲鑳藉氨浼氭斁寮冧娇鐢ㄨ搴旂敤浜?/span>
鎵€浠ワ紝浣跨敤澶氱嚎绋嬬紪绋嬪彲浠ユ渶澶ч檺搴﹀湴鍒╃敤绯荤粺鎻愪緵鐨勫鐞嗚兘鍔涳紝鎻愰珮绋嬪簭鐨勫悶鍚愮巼鍜屽搷搴旀€э紝閬垮厤鎬ц兘娴垂銆備絾浣跨敤澶氱嚎绋嬪氨涓€瀹氬氨鑳芥彁楂樻晥鐜囧悧锛熻繖涓嶄竴瀹?/p>
鈥?/span>閲囩敤澶氱嚎绋嬪悗锛屽悇涓嚎绋嬮棿鍙兘浼氱浉浜掔珵浜夌郴缁熻祫婧愶紝渚嬪澶勭悊鍣ㄦ椂闂寸墖銆佹帓浠栭攣銆佸甫瀹姐€佺‖鐩樿鍐欑瓑锛岃€岃祫婧愬線寰€鏄湁闄愪笖姣忔鍙兘鐢变竴涓嚎绋嬩娇鐢ㄧ殑锛屽苟鍙戠紪绋嬬殑鏈€缁堟晥鐩婂氨寰€寰€鍙楅檺浜庤祫婧愮殑鏈夐檺鍒嗛厤锛屽涓嚎绋嬩簤鐢ㄥ悓涓€涓帓浠栨€ц祫婧愬氨浼氬甫鏉ョ嚎绋嬩笂涓嬫枃鍒囨崲鐢氳嚦姝婚攣绛夐棶棰?/span>鈥?/span>渚嬪锛屽綋閲囩敤澶氫釜绾跨▼鏉ュ垎娈典笅杞芥煇涓綉缁滄枃浠朵互姝ゆ潵甯屾湜鍑忓皯涓嬭浇鑰楁椂銆傜敱浜庡甫瀹藉ぇ灏忔槸鍥哄畾鐨勶紝浣跨敤澶氫釜绾跨▼鍚屾椂杩涜涓嬭浇棣栧厛灏变細鎷変綆姣忎釜绾跨▼鐨勫钩鍧囧彲鐢ㄥ甫瀹藉ぇ灏忥紝姣忎釜绾跨▼涓嬭浇鍒扮殑鍗曚唤璧勬簮涔熼渶瑕侀€氳繃纭洏璇诲啓鍚堝苟鎴愪竴涓畬鏁寸殑鏂囦欢锛屾瘡娈佃祫婧愮殑鍚堝苟闇€瑕侀€氳繃璋冨害绋嬪害鏉ユ寜椤哄簭鍐欏叆锛岀淮鎶よ皟搴﹂『搴忕殑杩囩▼涔熸槸鏈夌潃鎬ц兘鐨勬秷鑰楋紝澶氫釜绾跨▼杩涜 IO 璇诲啓涔熶細鍔犲ぇ鍙戠敓绾跨▼涓婁笅鏂囧垏鎹㈢殑娆℃暟銆傚洜姝わ紝鏌愪簺鎯呭喌涓嬮噰鐢ㄥ绾跨▼鍙兘浼氭樉寰椻€滃苟涓嶉偅涔堝€煎緱鈥濓紝闇€瑕佹垜浠牴鎹疄闄呮儏鍐垫潵琛¢噺浣跨敤
浜屻€佽繘绋嬩笌绾跨▼
鈥?/span>绋嬪簭锛圥rogram锛夋槸瀵规寚浠ゃ€佹暟鎹強鍏剁粍缁囧舰寮忕殑鎻忚堪锛屾槸涓€绉嶉潤鎬佺殑姒傚康鈥?/span>杩涚▼锛圥rocess锛夋槸绋嬪簭鐨勮繍琛屽疄渚嬶紝姣忎釜琚惎鍔ㄧ殑绋嬪簭灏卞搴旇繍琛屼簬鎿嶄綔绯荤粺涓婄殑涓€涓繘绋嬶紝鏄竴绉嶅姩鎬佺殑姒傚康銆傝繘绋嬫槸绋嬪簭鍚戞搷浣滅郴缁熺敵璇疯祫婧愶紙鍐呭瓨绌洪棿銆佹枃浠跺彞鏌勭瓑锛夌殑鍩烘湰鍗曚綅锛屼篃鏄搷浣滅郴缁熻繘琛岃祫婧愯皟搴﹀拰璧勬簮鍒嗛厤鐨勫熀鏈崟浣嶃€傝繍琛屼竴涓?Java 绋嬪簭瀹炶川涓婂氨鏄惎鍔ㄤ簡涓€涓?Java 铏氭嫙鏈鸿繘绋?/span>鈥?/span>绾跨▼锛圱hread锛夋槸杩涚▼涓彲鐙珛鎵ц鐨勬渶灏忓崟浣嶏紝涔熸槸鎿嶄綔绯荤粺鑳藉杩涜杩愮畻璋冨害鐨勬渶灏忓崟浣嶏紝涔熻绉颁负杞婚噺绾ц繘绋嬨€傛瘡涓嚎绋嬫€绘槸鍖呭惈浜庣壒瀹氱殑杩涚▼鍐咃紝涓€涓繘绋嬪彲浠ュ寘鍚涓嚎绋嬩笖鑷冲皯鍖呭惈涓€涓嚎绋嬶紝绾跨▼鏄繘绋嬩腑鐨勫疄闄呰繍琛屽崟浣嶃€傚悓涓€涓繘绋嬩腑鐨勬墍鏈夌嚎绋嬪叡浜杩涚▼涓殑璧勬簮锛堝唴瀛樼┖闂淬€佹枃浠跺彞鏌勭瓑锛?/span>鈥?/span>浠诲姟锛圱ask锛夊嵆绾跨▼鎵€瑕佸畬鎴愮殑閫昏緫璁$畻銆傜嚎绋嬪湪鍒涘缓涔嬪垵鐨勭洰鐨勫氨鏄负浜嗚鍏舵潵鎵ц鐗瑰畾鐨勯€昏緫璁$畻锛屽叾鎵€瑕佸畬鎴愮殑宸ヤ綔灏辩О涓鸿绾跨▼鐨勪换鍔?/span>
澶氱嚎绋嬬紪绋嬫槸涓€绉嶄互绾跨▼涓哄熀鏈娊璞″崟浣嶇殑缂栫▼鑼冨紡锛圥raadigm锛夈€傜幇浠h绠楁満鎿嶄綔绯荤粺鍑犱箮閮芥敮鎸佸浠诲姟澶勭悊锛屽浠诲姟澶勭悊鏈変袱绉嶄笉鍚岀殑绫诲瀷锛?span class="mq-53">鍩轰簬杩涚▼鐨勫浠诲姟澶勭悊鍜?span class="mq-54">鍩轰簬绾跨▼鐨勫浠诲姟澶勭悊
鍩轰簬杩涚▼鐨勫浠诲姟澶勭悊鏄敱鎿嶄綔绯荤粺鏉ュ疄鐜板苟绠$悊鐨勶紝涓€鑸殑绋嬪簭寮€鍙戞帴瑙︿笉鍒拌繖涓眰闈€傝€屽熀浜庣嚎绋嬬殑澶氫换鍔″鐞嗗垯鍙互鐢辩▼搴忓紑鍙戣€呰嚜宸辨潵瀹炵幇骞惰繘琛岀鐞嗐€傚彲浠ヨ锛屽绾跨▼缂栫▼鐨勪竴涓洰鐨勫氨鏄负浜嗗疄鐜?span class="mq-56">鍩轰簬绾跨▼鐨勫浠诲姟澶勭悊
Java 瀵瑰绾跨▼鎻愪緵浜嗗唴缃敮鎸併€侸ava 鏍囧噯绫诲簱涓殑 java.lang.Thread
绫诲氨鏄绾跨▼杩欎釜姒傚康鐨勬娊璞″疄鐜帮紝鎻愪緵浜嗗湪涓嶅悓鐨勭‖浠跺拰鎿嶄綔绯荤粺骞冲彴涓婂绾跨▼鎿嶄綔鐨勭粺涓€澶勭悊锛屽睆钄戒簡涓嶅悓鐨勭‖浠跺拰鎿嶄綔绯荤粺鐨勫樊寮傛€?/p>
Java 鏈韩鏄竴涓绾跨▼鐨勫钩鍙帮紝鍗充娇寮€鍙戣€呮病鏈変富鍔ㄥ垱寤虹嚎绋嬶紝姝ゆ椂杩涚▼鍐呰繕鏄娇鐢ㄥ埌浜嗗涓嚎绋嬶紙渚嬪锛岃繕瀛樺湪 GC 绾跨▼锛夈€傛墍璋撶殑鍗曠嚎绋嬬紪绋嬪線寰€鎸囩殑鏄湪绋嬪簭涓紑鍙戣€呮病鏈変富鍔ㄥ垱寤虹嚎绋?/p>
涓夈€乀hread
Java 鏍囧噯绫诲簱 java.lang.Thread
鏄?Java 骞冲彴瀵圭嚎绋嬭繖涓蹇电殑鎶借薄瀹炵幇锛孴hread 绫绘垨鑰呭叾瀛愮被鐨勪竴涓疄渚嬪氨鏄竴涓嚎绋?/p>
1銆佺嚎绋嬬殑灞炴€?/h4>
灞炴€?/td> | 鍚箟 |
缂栧彿锛圛D锛?/td> | long銆傜敤浜庢爣璇嗕笉鍚岀殑绾跨▼锛屼笉鍚岀殑绾跨▼鎷ユ湁涓嶅悓鐨勭紪鍙凤紝鍦?Java 铏氭嫙鏈虹殑鍗曟鐢熷懡鍛ㄦ湡鍐呭叿鏈夊敮涓€鎬?/td> |
鍚嶇О锛圢ame锛?/td> | String銆傜敤浜庡尯鍒嗕笉鍚岀殑绾跨▼锛屾柟渚垮紑鍙戝拰璋冭瘯鏃跺畾浣嶉棶棰橈紝鍦?Java 铏氭嫙鏈虹殑鍗曟鐢熷懡鍛ㄦ湡鍐呭彲浠ラ噸澶?/td> |
绫诲埆锛圖aemon锛?/td> | boolean銆傚€间负 true 琛ㄧず璇ョ嚎绋嬩负瀹堟姢绾跨▼锛屽惁鍒欎负鐢ㄦ埛绾跨▼ |
浼樺厛绾э紙Priority锛?/td> | int銆侸ava 瀹氫箟浜?1~10 鐨?10 涓紭鍏堢骇锛岄粯璁ゅ€间负 5 |
鎸夌収绾跨▼鏄惁浼氶樆姝?Java 铏氭嫙鏈烘甯稿仠姝紝Java 涓殑绾跨▼鍒嗕负鐢ㄦ埛绾跨▼鍜?span class="mq-84">瀹堟姢绾跨▼銆傜敤鎴风嚎绋嬩細闃绘 Java 铏氭嫙鏈虹殑姝e父鍋滄锛屽嵆涓€涓?Java 铏氭嫙鏈哄彧鏈夊湪鍏舵墍鏈夌敤鎴风嚎绋嬮兘杩愯缁撴潫鐨勬儏鍐典笅鎵嶈兘姝e父鍋滄銆傝€屽畧鎶ょ嚎绋嬩笉浼氬奖鍝?Java 铏氭嫙鏈虹殑姝e父鍋滄锛屽嵆浣垮簲鐢ㄧ▼搴忎腑杩樻湁瀹堟姢绾跨▼鍦ㄨ繍琛屼篃涓嶅奖鍝?Java 铏氭嫙鏈虹殑姝e父鍋滄銆傚洜姝わ紝瀹堟姢绾跨▼閫傚悎鐢ㄤ簬鎵ц涓€浜涢噸瑕佹€т笉鏄緢楂樼殑浠诲姟銆備絾濡傛灉 Java 铏氭嫙鏈烘槸琚己鍒跺仠姝㈡垨鑰呯敱浜庡紓甯歌鍋滄鐨勮瘽锛岀敤鎴风嚎绋嬩篃鏃犳硶闃绘 Java 铏氭嫙鏈虹殑鍋滄
Java 绾跨▼鐨勪紭鍏堢骇灞炴€ф湰璐ㄤ笂鍙槸涓€涓粰绾跨▼璋冨害鍣ㄧ殑鎻愮ず淇℃伅锛屼互渚夸簬绾跨▼璋冨害鍣ㄥ喅瀹氫紭鍏堣皟搴﹀摢浜涚嚎绋嬭繍琛岋紝浼樺厛绾ч珮鐨勭嚎绋嬬悊璁轰笂浼氳幏寰楁洿澶氱殑澶勭悊鍣ㄤ娇鐢ㄦ椂闂达紝浣嗙嚎绋嬭皟搴﹀櫒骞朵笉淇濊瘉涓€瀹氭寜鐓х嚎绋嬩紭鍏堢骇鐨勯珮浣庢潵璋冨害绾跨▼銆傛澶栵紝 JVM 鎵€鍦ㄧ殑鎿嶄綔绯荤粺鍙兘浼氬拷鐣ョ敋鑷充富鍔ㄦ潵淇敼鎴戜滑瀵圭嚎绋嬬殑浼樺厛绾ч厤缃紝涓斿鏋滅嚎绋嬬殑浼樺厛绾ц缃笉褰擄紝鐢氳嚦鏈夊彲鑳藉鑷寸嚎绋嬫案杩滄棤娉曞緱鍒拌繍琛岋紝鍗充骇鐢熺嚎绋嬮ゥ楗?/p>
濡傛灉鍦ㄧ嚎绋?A 涓垱寤轰簡绾跨▼ B锛岄偅涔堢嚎绋?B 灏辩О涓虹嚎绋?A 鐨勫瓙绾跨▼锛岀嚎绋?A 灏辩О涓虹嚎绋?B 鐨勭埗绾跨▼銆傜敱浜?Java 铏氭嫙鏈哄垱寤虹殑 main 绾跨▼锛堜富绾跨▼锛夎礋璐f墽琛?Java 绋嬪簭鐨勫叆鍙f柟娉?nbsp;main()
鏂规硶锛屽洜姝?main 鏂规硶涓垱寤虹殑绾跨▼閮芥槸 main 绾跨▼鐨勫瓙绾跨▼鎴栭棿鎺ュ瓙绾跨▼銆傛澶栵紝涓€涓嚎绋嬫槸鍚︽槸瀹堟姢绾跨▼榛樿涓庡叾鐖剁嚎绋嬬浉鍚岋紝绾跨▼浼樺厛绾х殑榛樿鍊间篃涓庡叾鐖剁嚎绋嬬殑浼樺厛绾х浉鍚屻€傞渶瑕佹敞鎰忕殑鏄紝铏界劧绾跨▼闂村叿鏈夎繖绫荤埗瀛愬叧绯伙紝浣嗘槸瀹冧滑骞朵笉浼氱浉浜掑奖鍝嶅鏂圭殑鐢熷懡鍛ㄦ湡锛屼竴鏂圭嚎绋嬬敓鍛藉懆鏈熺殑缁撴潫锛堜笉绠℃槸姝e父缁撴潫杩樻槸寮傚父鍋滄锛夊苟涓嶅奖鍝嶅彟涓€涓嚎绋嬬户缁繍琛?/p>
2銆佺嚎绋嬬殑鏂规硶
鏂规硶 | 璇存槑 |
static Thread currentThread() | 杩斿洖褰撳墠鐨勬墽琛岀嚎绋嬪璞?/td> |
static void yield() | 浣垮綋鍓嶇嚎绋嬩富鍔ㄦ斁寮冨叾澶勭悊鍣ㄧ殑鍗犵敤锛屼絾涓嶄竴瀹氫細浣垮綋鍓嶇嚎绋嬫殏鍋?/td> |
static void sleep(long) | 浣垮綋鍓嶇嚎绋嬩紤鐪犳寚瀹氭椂闂?/td> |
void start() | 鍚姩绾跨▼ |
void join() | 鑻ョ嚎绋?A 璋冪敤绾跨▼ B 鐨?join() 鏂规硶锛屽垯绾跨▼ A 浼氭殏鍋滆繍琛岋紝鐩村埌绾跨▼ B 杩愯缁撴潫 |
void suspend() | 鏆傚仠绾跨▼锛岃繘鍏ョ潯鐪犵姸鎬侊紙宸插簾寮冿級 |
void resume() | 鍞ら啋绾跨▼锛屽拰 suspend() 鎴愬浣跨敤锛堝凡搴熷純锛?/td> |
void stop() | 鍋滄绾跨▼锛堝凡搴熷純锛?/td> |
Thread.suspend()
鍜?nbsp;Thread.resume()
鏄?Thread 绫绘彁渚涚殑鐢ㄤ簬鏆傚仠鍜屽敜閱掔嚎绋嬬殑鏂规硶锛岀敤浜庡湪鏌愪簺杩愯鏉′欢涓嶆弧瓒崇殑鏃跺€欐殏鍋滄墽琛屼换鍔★紝鍚庣画鍦ㄨ繍琛屾潯浠舵弧瓒崇殑鏃跺€欏敜閱掔嚎绋嬬户缁墽琛屼换鍔★紝浣嗙幇鍦ㄥ潎宸插簾寮?/p>
3銆佺嚎绋嬬殑鐘舵€?/h4>
绾跨▼鍦ㄥ畠鐨勬暣涓敓鍛藉懆鏈熷唴浼氬厛鍚庡浜庝笉鍚岀姸鎬侊紝涔熷彲鑳戒細鍦ㄥ涓姸鎬侀棿鏉ュ洖鍒囨崲銆傚浜庣粰瀹氱殑绾跨▼瀹炰緥锛屽彲浠ヤ娇鐢?nbsp;Thread.getState()
鏂规硶鑾峰彇绾跨▼鐨勭姸鎬侊紝璇ユ柟娉曡繑鍥?nbsp;Thread.State
鏋氫妇绫诲瀷鍊硷紝鐢ㄤ簬鏍囨槑鍦ㄨ皟鐢ㄨ鏂规硶鏃剁嚎绋嬫墍澶勭殑褰撳墠鐘舵€侊紝杩斿洖鍊煎寘鍚互涓嬪嚑绉嶅彲鑳斤細
鍊?/td> | 鐘舵€?/td> |
NEW | 绾跨▼澶勪簬鏂板缓鐘舵€侊紝杩樻病鏈夎皟鐢?start() 鏂规硶 |
RUNNABLE | 绾跨▼瑕佷箞褰撳墠姝e湪鎵ц锛岃涔堝湪鑾峰緱澶勭悊鍣ㄦ椂闂寸墖涔嬪悗灏卞彲浠ユ墽琛?/td> |
BLOCKED | 绾跨▼鍥犱负鍙戣捣浜嗛樆濉炲紡鎿嶄綔锛屾垨鑰呮鍦ㄧ瓑寰呴渶瑕佺殑璧勬簮鏃惰鎸傝捣 |
WAITING | 绾跨▼鍥犱负绛夊緟鏌愪簺鍔ㄤ綔鑰屾寕璧锋墽琛屻€備緥濡傦紝鍥犱负璋冪敤浜?wait() 鎴?join() 鏂规硶鎵€浠ュ浜庤繖绉嶇姸鎬侊紝杩欑鐘舵€佷笅鐨勭嚎绋嬮渶瑕佺敱澶栭儴鍏跺畠绾跨▼鏉ュ敜閱掕嚜韬?/td> |
TIMED_WAITING | 绾跨▼鎸傝捣涓€娈垫寚瀹氱殑鏃堕棿锛屽綋杈惧埌鎸囧畾鐨勬椂闂村埌杈惧悗锛岀嚎绋嬪氨浼氳浆涓?RUNNABLE 鐘舵€併€備緥濡傚綋璋冪敤 sleep(long) 銆亀ait(long) 銆?join(long) 绛夋柟娉曟椂灏变細澶勪簬杩欑鐘舵€?/td> |
TERMINATED | 宸茬粡杩愯缁撴潫鐨勭嚎绋嬪氨澶勪簬姝ょ姸鎬?/td> |
鍙互鐢ㄦ洿鍔犻€氫織鐨勮瑷€鏉ユ弿杩扮嚎绋嬬殑鐢熷懡鍛ㄦ湡锛?/p>
1.鏂板缓鐘舵€?NEW浣跨敤 new
鍏抽敭瀛楀缓绔嬩竴涓嚎绋嬪璞″悗锛岀嚎绋嬪氨澶勪簬鏂板缓鐘舵€侊紝涓€涓凡鍒涘缓浣嗚繕鏈惎鍔ㄧ殑绾跨▼灏卞浜庢鐘舵€併€傜嚎绋嬩細淇濇寔杩欎釜鐘舵€佺洿鍒拌璋冪敤 Thread.start()
鏂规硶2.灏辩华鐘舵€?RUNNABLE褰撶嚎绋嬪璞¤皟鐢ㄤ簡 start()
鏂规硶涔嬪悗锛岀嚎绋嬪氨浼氳繘鍏ュ氨缁姸鎬併€傝鐘舵€佸彲浠ョ湅鎴愪竴涓鍚堢姸鎬侊紝瀹冨寘鍚袱涓瓙鐘舵€侊細READY 鍜?nbsp;RUNNING銆傚墠鑰呰〃绀虹嚎绋嬪浜庡氨缁槦鍒椾腑锛屽綋琚嚎绋嬭皟搴﹀櫒閫変腑璋冨害鍚庡氨鍙互姝e紡杩愯锛屽彉涓?RUNNING 鐘舵€併€傚悗鑰呰〃绀虹嚎绋嬫澶勪簬杩愯涓紝鍏?nbsp;run()
鏂规硶瀵瑰簲鐨勬寚浠ゆ鍦ㄧ敱澶勭悊鍣ㄦ墽琛屻€傚綋鎵ц绾跨▼鐨?nbsp;yiedId()
鏂规硶鏃讹紝鍏剁姸鎬佸彲鑳戒細鐢?RUNNING 鍒囨崲涓?READY3.闃诲鐘舵€?BLOCKED褰撶嚎绋嬪彂璧蜂竴涓樆濉炲紡鐨?IO 鎿嶄綔鎴栬€呮槸鍦ㄧ敵璇蜂竴涓敱鍏跺畠绾跨▼鎸佹湁鐨勭嫭鍗犺祫婧愭椂锛岃绾跨▼灏变細澶勪簬姝ょ姸鎬併€傚鐞?BLOCKED 鐘舵€佺殑绾跨▼涓嶄細鍗犵敤 CPU 璧勬簮銆傚綋鍏剁洰鏍囪涓烘垨鑰呮槸鐩爣璧勬簮琚弧瓒冲悗锛屽氨鍙互鍒囨崲涓?RUNNABLE 鐘舵€?/span>4.鏃犻檺鏈熺瓑寰呯姸鎬?WAITING褰撶嚎绋嬬殑杩愯闇€瑕佹弧瓒虫煇浜涙墽琛屾潯浠惰€屽綋鍓嶅苟涓嶆弧瓒虫椂锛岄€氬父灏变細閫氳繃璁╄绾跨▼涓诲姩璋冪敤 Object.wait()
銆?code class="mq-166">Thread.join() 绛夌被浼兼柟娉曞皢绾跨▼鍒囨崲涓?WAITING 鐘舵€併€傚綋鍓嶇姸鎬佹槸 WAITING 鐨勭嚎绋嬪浜庢殏鍋滆繍琛岀殑鐘舵€侊紝闇€瑕佸閮ㄥ叾瀹冪嚎绋嬮€氳繃 Object.notify()
绛夋柟娉曟潵涓诲姩鍞ら啋璇ョ嚎绋?/span>5.闄愭湡绛夊緟鐘舵€?TIMED_WAITINGTIMED_WAITING 鐘舵€佸拰 WAITING 鐘舵€佺被浼笺€傚尯鍒湪浜?TIMED_WAITING 鐘舵€佸苟闈炴槸绾跨▼鏈韩瀹屽叏鏃犻檺鍒跺湴杩涜绛夊緟锛屽叾绛夊緟琛屼负甯︽湁鎸囧畾鏃堕棿鑼冨洿鐨勯檺鍒讹紝褰撳湪鎸囧畾鏃堕棿鍐呮病鏈夊畬鎴愯绾跨▼鎵€鏈熸湜鐨勭壒瀹氭搷浣滄椂锛岃绾跨▼灏变細杞负 RUNNABLE 鐘舵€併€傚彲浠ラ€氳繃 Object.wait(long)
鏂规硶鏉ヤ娇绾跨▼鍒囨崲涓?TIMED_WAITING 鐘舵€?/span>6.缁堟鐘舵€?TERMINATED褰撲竴涓嚎绋嬪畬鎴愯嚜韬换鍔℃垨鑰呯敱浜庡叾瀹冨師鍥犺杩粓姝㈡椂锛岀嚎绋嬪氨浼氬垏鎹㈠埌缁堟鐘舵€侊紝鑷虫绾跨▼鐨勬暣涓敓鍛藉懆鏈熷氨缁撴潫浜?/span>
鐢变簬涓€涓嚎绋嬪湪鍏舵暣涓敓鍛藉懆鏈熷唴鍙兘琚惎鍔ㄤ竴娆★紝鎵€浠ョ嚎绋嬩篃鍙細澶勪簬涓€娆?nbsp;NEW
鐘舵€佸拰涓€娆?nbsp;TERMINATED
鐘舵€併€傚浜庝竴涓绾跨▼绯荤粺鏉ヨ锛屾渶鐞嗘兂鐨勬儏鍐靛氨鏄墍鏈夊凡鍚姩涓旀湭缁撴潫鐨勭嚎绋嬭兘涓€鐩村浜?RUNNING 鐘舵€侊紝浣嗚繖鏄笉鍙兘瀹炵幇鐨勩€傚湪鐜板疄鍦烘櫙涓嬬嚎绋嬩細鍦ㄥ涓姸鎬侀棿鏉ュ洖鍒囨崲锛屼笖绾跨▼浠?RUNNABLE 鐘舵€佽浆鎹负 BLOCKED銆乄AITING 鍜?TIMED_WAITING 杩欏嚑涓姸鎬佷腑鐨勪换浣曚竴涓椂閮芥剰鍛崇潃鍙戠敓浜嗙嚎绋嬩笂涓嬫枃鍒囨崲
4銆乀hreadGroup
绾跨▼缁勶紙ThreadGroup锛夌敤鏉ヨ〃绀轰竴缁勭浉鍏宠仈鐨勭嚎绋嬶紝瀹冩槸 Thread 绫诲寘鍚殑涓€涓唴閮ㄥ睘鎬э紝鍙互閫氳繃 Thread.getThreadGroup()
鏉ヨ幏鍙栬鍊笺€傜嚎绋嬩笌绾跨▼缁勪箣闂寸殑鍏崇郴绫讳技浜庢枃浠剁郴缁熶腑鏂囦欢涓庢枃浠跺す涔嬮棿鐨勫叧绯伙紝涓€涓嚎绋嬬粍鍙互鍖呭惈澶氫釜绾跨▼浠ュ強鍏跺畠绾跨▼缁?/p>
濡傛灉鍦ㄥ垱寤虹嚎绋嬫椂娌℃湁鏄剧ず鎸囧畾绾跨▼鎵€灞炵殑绾跨▼缁勭殑璇濓紝鍦ㄩ粯璁ゆ儏鍐典笅绾跨▼灏辫褰掔被浜庡叾鐖剁嚎绋嬫墍灞炵殑绾跨▼缁勪笅銆備粠 Thread 绫荤殑浠ヤ笅鏂规硶灏卞彲浠ョ湅鍑烘潵锛?/p>
private void init(ThreadGroup g, Runnable target, String name,
long stackSize, AccessControlContext acc,
boolean inheritThreadLocals) {
路路路路
Thread parent = currentThread();
SecurityManager security = System.getSecurityManager();
if (g == null) {
/* Determine if it's an applet or not */
/* If there is a security manager, ask the security manager
what to do. */
if (security != null) {
//榛樿涔熸槸杩斿洖 Thread.currentThread().getThreadGroup()
g = security.getThreadGroup();
}
/* If the security doesn't have a strong opinion of the matter
use the parent thread group. */
if (g == null) {
g = parent.getThreadGroup();
}
}
路路路路
}
Java 铏氭嫙鏈哄湪鍒涘缓 main 绾跨▼锛堟墍鏈夌嚎绋嬬殑鐖剁嚎绋嬶級鐨勬椂鍊欎細涓哄叾鑷姩鎸囧畾涓€涓嚎绋嬬粍锛屽洜姝や换浣曚竴涓嚎绋嬮兘鏈変竴涓嚎绋嬬粍涓庝箣鐩稿叧鑱斻€備笖涓€涓嚎绋嬬粍鐨勭埗绾跨▼缁勯粯璁ゆ槸鍦ㄥ0鏄庤绾跨▼缁勬椂鎵€鍦ㄧ嚎绋嬬殑绾跨▼缁勶紝浣嗗苟闈炴墍鏈夌嚎绋嬬粍鍧囨湁鐖剁嚎绋嬬粍锛屾渶椤跺眰绾跨▼缁勫氨涓嶅寘鍚埗绾跨▼缁?/p>
/**
* 浣滆€咃細leavesC
* 鏃堕棿锛?/span>2020/8/12 22:10
* 鎻忚堪锛?/span>
* GitHub锛?/span>https://github.com/leavesC
*/
fun main() {
val mainThreadGroup = Thread.currentThread().threadGroup
println(mainThreadGroup)
println("mainThreadGroup.parent: " + mainThreadGroup.parent)
println("mainThreadGroup.parent.parent: " + mainThreadGroup.parent.parent)
val thread = Thread()
println(thread.threadGroup)
val thread2 = Thread(ThreadGroup("otherThreadGroup"), "thread")
println(thread2.threadGroup)
// java.lang.ThreadGroup[name=main,maxpri=10]
// mainThreadGroup.parent: java.lang.ThreadGroup[name=system,maxpri=10]
// mainThreadGroup.parent.parent: null
// java.lang.ThreadGroup[name=main,maxpri=10]
// java.lang.ThreadGroup[name=otherThreadGroup,maxpri=10]
}
ThreadGroup 鏈韩瀛樺湪璁捐缂洪櫡闂锛岀洰鍓嶇殑浣跨敤鍦烘櫙鏈夐檺锛屾棩甯稿紑鍙戜腑鍙互鏃犻渶鐞嗕細
5銆佺嚎绋嬪紓甯告崟鑾?/h4>
鍦ㄥ緢澶氭椂鍊欙紝鎴戜滑浼氶€氳繃鍒涘缓涓€涓嚎绋嬫睜鏉ユ墽琛屼换鍔★紝鑰屽綋鏌愪釜浠诲姟鐢变簬鎶涘嚭寮傚父瀵艰嚧鍏舵墽琛岀嚎绋嬪紓甯哥粓姝㈡椂锛屾垜浠氨闇€瑕佸杩欑寮傚父鎯呭喌杩涜涓婃姤浠ヤ究鍚庣画鍒嗘瀽銆傝瀹炵幇杩欎釜鏁堟灉锛屽氨闇€瑕佽兘澶熸敹鍒扮嚎绋嬭寮傚父缁堟鏃剁殑浜嬩欢閫氱煡锛岃繖灏遍渶瑕佺敤鍒?nbsp;Thread.setUncaughtExceptionHandler(UncaughtExceptionHandler)
鏂规硶
閫氳繃璇ユ柟娉曟垜浠彲浠ュ湪寮傚父鍙戠敓鏃朵笖绾跨▼琚仠姝㈠墠鑾峰彇鍒扮浉搴旂殑 Thread 瀵硅薄鍜?Throwable 瀹炰緥
/**
* 浣滆€咃細leavesC
* 鏃堕棿锛?/span>2020/8/12 22:14
* 鎻忚堪锛?/span>
* GitHub锛?/span>https://github.com/leavesC
*/
fun main() {
val runnable = Runnable {
for (i in 4 downTo 0) {
println(100 % i)
Thread.sleep(100)
}
}
val thread = Thread(runnable, "otherName")
thread.setUncaughtExceptionHandler { t, e ->
println("threadName: " + t.name)
println("exc: $e")
}
thread.start()
}
0
1
4
2
4
0
0
1
0
0
threadName: otherName
exc: java.lang.ArithmeticException: / by zero
ThreadGroup 鏈韩涔熷疄鐜颁簡 UncaughtExceptionHandler 鎺ュ彛锛屾墍浠ュ鏋?Thread 瀵硅薄涓嶅寘鍚叧鑱旂殑 UncaughtExceptionHandler 瀹炰緥鐨勮瘽锛屽垯浼氬皢寮傚父浜ょ敱 ThreadGroup 鏉ヨ繘琛屽鐞?/p>
浠?Thread 绫荤殑浠ヤ笅閫昏緫灏卞彲浠ョ湅鍑烘潵
public class Thread implements Runnable {
private ThreadGroup group;
private volatile UncaughtExceptionHandler uncaughtExceptionHandler;
private void dispatchUncaughtException(Throwable e) {
getUncaughtExceptionHandler().uncaughtException(this, e);
}
public UncaughtExceptionHandler getUncaughtExceptionHandler() {
return uncaughtExceptionHandler != null ?
uncaughtExceptionHandler : group;
}
}
ThreadGroup 榛樿鎯呭喌涓嬩細灏嗗紓甯镐氦鐢卞叾鐖剁嚎绋嬬粍杩涜澶勭悊锛岃€屽浜庝笉鍖呭惈鐖剁嚎绋嬬粍鐨勭嚎绋嬬粍瀵硅薄锛堥《灞傜嚎绋嬬粍锛夛紝鍒欎細灏嗗紓甯镐氦鐢?Thread 绫荤殑 defaultUncaughtExceptionHandler
杩涜澶勭悊銆傛墍浠ワ紝鎴戜滑鍙互閫氳繃 Thread 鐨勯潤鎬佹柟娉?nbsp;setDefaultUncaughtExceptionHandler
鏂规硶鏉ヤ负绋嬪簭璁剧疆涓€涓叏灞€鐨勯粯璁ゅ紓甯稿鐞嗗櫒
public class ThreadGroup implements Thread.UncaughtExceptionHandler {
public void uncaughtException(Thread t, Throwable e) {
if (parent != null) {
//褰撴湁鐖剁嚎绋嬬粍鏃讹紝鍒欏皢寮傚父浜ょ敱鐖剁嚎绋嬬粍鏉ュ鐞?/span>
parent.uncaughtException(t, e);
} else {
//褰撶埗绾跨▼缁勪笉瀛樺湪鏃讹紝鍒欏皾璇曞皢寮傚父浜ょ敱 DefaultUncaughtExceptionHandler 鏉ュ鐞?/span>
Thread.UncaughtExceptionHandler ueh =
Thread.getDefaultUncaughtExceptionHandler();
if (ueh != null) {
ueh.uncaughtException(t, e);
} else if (!(e instanceof ThreadDeath)) {
System.err.print("Exception in thread \""
+ t.getName() + "\" ");
e.printStackTrace(System.err);
}
}
}
}
public class Thread implements Runnable {
private static volatile UncaughtExceptionHandler defaultUncaughtExceptionHandler;
public static UncaughtExceptionHandler getDefaultUncaughtExceptionHandler(){
return defaultUncaughtExceptionHandler;
}
//璁剧疆鍏ㄥ眬榛樿鐨勫紓甯稿鐞嗗櫒
public static void setDefaultUncaughtExceptionHandler(UncaughtExceptionHandler eh) {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkPermission(
new RuntimePermission("setDefaultUncaughtExceptionHandler")
);
}
defaultUncaughtExceptionHandler = eh;
}
}
鎵€浠ワ紝褰撶嚎绋嬬敱浜庡紓甯歌€岀粓姝㈡椂锛孶ncaughtExceptionHandler 瀹炰緥鐨勯€夋嫨浼樺厛绾т粠楂樺埌浣庡垎鍒槸锛?/p>
1.Thread.uncaughtExceptionHandler2.ThreadGroup.uncaughtExceptionHandler3.Thread.defaultUncaughtExceptionHandler
6銆乀hreadFactory
鍦ㄩ」鐩腑鍏堝悗闇€瑕佷娇鐢ㄥ埌澶氫釜绾跨▼鏄竴涓櫘閬嶇殑闇€姹傦紝鑰屽鏋滄瘡娆″潎绠€鍗曠殑閫氳繃 new Thread()
鏉ュ垱寤虹嚎绋嬬殑璇濓紝鍦ㄥ嚭鐜伴棶棰樻椂灏卞緢闅惧畾浣嶉棶棰樻墍鍦ㄣ€傛墍浠?Java 鏍囧噯搴撲篃鎻愪緵浜嗗垱寤虹嚎绋嬬殑宸ュ巶鏂规硶锛屽嵆 ThreadFactory 鎺ュ彛
public interface ThreadFactory {
Thread newThread(Runnable r);
}
ThreadFactory 鎻愪緵浜嗗皢瑕佹墽琛岀殑浠诲姟 Runnable 涓庤鍒涘缓鐨?Thread 鐩稿叧鑱旂殑鏂规硶锛屽嵆鎴戜滑鍙互閫氳繃 ThreadFactory 鏉ユ爣鏄?Thread 瑕佹墽琛岀殑鍏蜂綋浠诲姟銆佷负 Thread 璁剧疆涓€涓湁鍏蜂綋鍚箟鐨勫悕瀛椼€佽缃?Thread 鐨勮繍琛屼紭鍏堢骇绛?/p>
渚嬪锛孍xecutors 鍐呴儴灏卞寘鍚簡涓€涓?nbsp;DefaultThreadFactory
锛岄€氳繃 threadNumber
鑷鐨勬柟寮忎负姣忎竴涓垱寤虹殑绾跨▼璁剧疆浜嗙壒瀹氱殑绾跨▼鍚嶃€佺‘淇濈嚎绋嬫槸鐢ㄦ埛绾跨▼銆佺‘淇濈嚎绋嬬殑浼樺厛绾т负姝e父绾у埆
static class DefaultThreadFactory implements ThreadFactory {
private static final AtomicInteger poolNumber = new AtomicInteger(1);
private final ThreadGroup group;
private final AtomicInteger threadNumber = new AtomicInteger(1);
private final String namePrefix;
DefaultThreadFactory() {
SecurityManager s = System.getSecurityManager();
group = (s != null) ? s.getThreadGroup() :
Thread.currentThread().getThreadGroup();
namePrefix = "pool-" +
poolNumber.getAndIncrement() +
"-thread-";
}
public Thread newThread(Runnable r) {
Thread t = new Thread(group, r,
namePrefix + threadNumber.getAndIncrement(),
0);
if (t.isDaemon())
t.setDaemon(false);
if (t.getPriority() != Thread.NORM_PRIORITY)
t.setPriority(Thread.NORM_PRIORITY);
return t;
}
}
鑰屽浜庢垜浠」鐩嚜宸卞畾涔夌殑绾跨▼姹狅紝浣跨敤 ThreadFactory 鐨勪竴涓瘮杈冩湁鎰忎箟鐨勭敤澶勬槸锛氫负绾跨▼璁剧疆鍏宠仈鐨?UncaughtExceptionHandler锛岃繖鍦ㄦ彁楂樼郴缁熺殑鍋ュ.鎬ф柟闈㈡槸寰堟湁濂藉鐨?/p>
7銆佺嚎绋嬬殑鎵ц鏃舵満
鎴戜滑鍙互绠€鍗曞湴鐞嗚В涓鸿繍琛屼竴涓嚎绋嬪氨鏄姹?Java 铏氭嫙鏈烘潵璋冪敤 Runnable 瀵硅薄鐨?nbsp;run()
鏂规硶锛屼粠鑰屼娇寰楃嚎绋嬬殑浠诲姟澶勭悊閫昏緫寰椾互琚墽琛屻€備絾鎴戜滑閫氳繃 Thread.start()
鍚姩涓€涓嚎绋嬪苟涓嶆剰鍛崇潃绾跨▼灏辫兘澶熼┈涓婅鎵ц锛岀嚎绋嬬殑鍏蜂綋鎵ц鏃舵満鐢?span class="mq-1238">绾跨▼璋冨害鍣?/strong>鏉ュ喅瀹氾紝鎵ц鏃舵満鍏锋湁涓嶇‘瀹氭€э紝鐢氳嚦鍙兘浼氱敱浜庣嚎绋嬫椿鎬ф晠闅滆€屾案杩滄棤娉曡繍琛屻€傛澶栵紝鐢变簬 run()
鏂规硶鏄?nbsp;public
鐨勶紝鎵€浠ュ畠涔熷彲浠ョ敱澶栭儴涓诲姩鏉ヨ皟鐢ㄦ墽琛岋紝浣嗘鏃跺叾浠诲姟灏辨槸鐢卞綋鍓嶇殑杩愯绾跨▼鏉ユ墽琛岋紝杩欏湪澶у鏁版椂鍊欓兘鏄病鏈夊疄闄呮剰涔夌殑
鑰屼笉绠℃槸閫氳繃浠€涔堟柟寮忔潵鍒涘缓绾跨▼锛屽綋绾跨▼鐨?nbsp;run()
鏂规硶鎵ц缁撴潫鏃讹紙涓嶇鏄甯哥粨鏉熻繕鏄敱浜庡紓甯歌€屼腑鏂繍琛岋級锛岀嚎绋嬬殑鐢熷懡鍛ㄦ湡涔熷氨璧板埌鏈熬浜嗭紝鍏跺崰鐢ㄧ殑璧勬簮浼氬湪鍚庣画琚?Java 铏氭嫙鏈哄瀮鍦惧洖鏀躲€傝€屼笖锛岀嚎绋嬫槸涓€娆℃€ц祫婧愶紝鎴戜滑鏃犳硶閫氳繃鍐嶆璋冪敤 start()
鏂规硶鏉ラ噸鏂板惎鍔ㄧ嚎绋嬶紝褰撳娆¤皟鐢ㄨ鏂规硶鏃朵細鎶涘嚭 IllegalThreadStateException
鍥涖€佸绾跨▼瀹夊叏
瀹炵幇澶氱嚎绋嬬紪绋嬩笉鏄畝鍗曞湴澹版槑澶氫釜 Thread 瀵硅薄骞跺惎鍔ㄥ氨鍙互鐨勪簡锛屽湪鐜板疄鍦烘櫙涓紝澶氫釜绾跨▼闂村線寰€鏄渶瑕佸畬鎴愭暟鎹氦鎹㈠拰琛屼负浜や簰绛夊悇绉嶅鏉傛搷浣滅殑锛岃€屼笉鏄畝鍗曞湴鈥滃悇琛屽叾鏄€濄€傜浉姣斿崟绾跨▼锛?span class="mq-1247">浣跨敤澶氱嚎绋嬩細甯︽潵璁稿鍦ㄥ崟绾跨▼涓嬩笉瀛樺湪鎴栬€呮牴鏈笉鐢ㄨ€冭檻鐨勯棶棰?/strong>
1銆佺珵鎬?/h4>
鍏堟潵鐪嬩竴涓畝鍗曠殑渚嬪瓙銆傚亣璁惧瓨鍦ㄤ竴涓晢搴?Shop锛屽叾鍒濆鍟嗗簵鏁伴噺涓洪浂銆傚瓨鍦ㄥ洓鍗佷釜鐢熶骇鑰?Producer 涓哄叾鐢熶骇鍟嗗搧锛屾瘡涓?Producer 浼氬悇鑷负鍟嗗簵鎻愪緵涓€涓晢鍝併€傞偅涔堬紝鐞嗚涓婂綋鎵€鏈?Producer 鐢熶骇瀹屾瘯鍚庯紝Shop 鐨勫晢鍝佹暟閲?goodsCount 搴旇鏄洓鍗?/p>
鍙槸锛岃繍琛屼互涓嬩唬鐮佸悗浣犱細鍙戠幇瀹為檯鏁伴噺澶ф鐜囨槸浼氬皯浜庡洓鍗?/p>
/**
* 浣滆€咃細leavesC
* 鏃堕棿锛?/span>2020/8/3 22:18
* 鎻忚堪锛?/span>
* GitHub锛?/span>https://github.com/leavesC
*/
class Shop(var goodsCount: Int) {
fun produce() {
goodsCount++
}
}
class Producer(private val shop: Shop) : Thread() {
override fun run() {
sleep(100)
if (shop.goodsCount < 40) {
shop.produce()
}
println("over")
}
}
fun main() {
val shop = Shop(0)
val threads = mutableListOf<Thread>()
for (i in 1..40) {
threads.add(Producer(shop))
}
threads.forEach {
it.start()
}
//淇濊瘉鎵€鏈?Producer Thread 閮芥墽琛屽畬姣曚細鍐嶆墽琛屼箣鍚庣殑璇彞
threads.forEach {
it.join()
}
println("shop.goodsCount: " + shop.goodsCount)
}
浠ヤ笂浠g爜灏变娇鐢ㄥ埌浜嗗涓嚎绋嬶紝鍗曚釜 Producer 绾跨▼鐨勮涓洪€昏緫鏄嫭绔嬬殑锛岃€屽涓?Producer 绾跨▼鐨勮涓洪€昏緫瀵逛簬 Shop 鏉ヨ鏄簰鐩镐氦閿欎笖鍏堝悗椤哄簭涓嶇‘瀹氱殑锛岃繖灏辨湁鍙兘瀵艰嚧涓€绉嶆儏鍐碉細涓や釜 Producer 鍚屾椂鍒ゆ柇鍒板綋鍓嶅晢鍝佹暟閲忔槸鍗侊紝鐒跺悗鍚屾椂涓哄晢搴楃敓浜х鍗佷竴浠跺晢鍝侊紙shop.goodsCount++锛夛紝鏈€缁堝氨瀵艰嚧鏌愪釜 Producer 鐢熶骇鐨勭鍗佷竴鍙峰晢鍝佽鍙︿竴涓?Producer 瑕嗙洊浜嗭紝涓や釜 Producer 鍙敓浜т簡涓€浠跺晢鍝侊紝鍗虫暟鎹?span class="mq-1508">鏇存柊鏃犳晥/鏇存柊涓㈠け
shop.goodsCount++
杩欐潯璇彞铏界劧鐪嬭捣鏉ュ儚鏄竴涓笉鍙垎鍓茬殑鎿嶄綔锛堝師瀛愭搷浣滐級锛屼絾瀹冨疄闄呬笂鐩稿綋浜庡涓嬩吉浠g爜鎵€琛ㄧず鐨勪笁涓寚浠ょ殑缁勫悎锛?/p>
load(shop.goodsCount , r1) //鎸囦护1锛屽皢鍙橀噺 shop.goodsCount 鐨勫€间粠鍐呭瓨璇诲埌瀵勫瓨鍣?r1
increment(r1) //鎸囦护2锛屽皢瀵勫瓨鍣?r1 鐨勫€煎姞1
store(shop.goodsCount , r1) //鎸囦护3锛屽皢瀵勫瓨鍣?r1 鐨勫唴瀹瑰啓鍏ュ彉閲?shop.goodsCount 鎵€瀵瑰簲鐨勫唴瀛樼┖闂?/span>
澶氫釜 Producer 绾跨▼鍙兘浼氬悓鏃跺悇鑷墽琛屼笂杩版寚浠ゃ€備緥濡傦紝鍋囪褰撳墠 goodsCount 鏄崄锛孭roducer1 鍜?Producer2 鍚屾椂鎵ц鍒版寚浠?锛屼袱涓嚎绋嬪皢 goodsCount 璇诲埌鍚勮嚜澶勭悊鍣ㄧ殑瀵勫瓨鍣ㄤ笂锛屽嵆姣忎釜绾跨▼浼氬悇鑷嫢鏈変竴浠藉壇鏈暟鎹紝鐒跺悗瀵瑰悇鑷瘎瀛樺櫒鐨勫€艰繘琛岃嚜澧炲姞涓€鐨勬搷浣滐紝褰撴墽琛屽埌鎸囦护涓夋椂锛岀敱浜?goodsCount 鎵€鍦ㄧ殑鍐呭瓨绌洪棿鏄壒瀹氱殑锛屾墍浠ヤ袱涓?Producer 绾跨▼瀵瑰唴瀛樼┖闂翠笂 goodsCount 鐨勫€肩殑鍥炰紶浼氬瓨鍦ㄧ浉浜掕鐩栫殑鎯呭喌銆傚嵆鍘熸湰鏈€缁堢粨鏋滃簲璇ユ槸閫掑鍔犱簩鐨勮涓烘渶缁堝嵈鍙湁閫掑鍔犱竴
浠ヤ笂鏄绾跨▼缂栫▼涓粡甯镐細閬囧埌鐨勪竴涓幇璞★紝鍗崇珵鎬併€傜珵鎬佹槸鎸囪绠楃殑姝g‘鎬т緷璧栦簬鐩稿鏃堕棿椤哄簭鎴栬€呯嚎绋嬬殑浜ら敊銆傜珵鎬佸苟涓嶄竴瀹氬氨浼氬鑷磋绠楃粨鏋滀笉姝g‘锛屽畠鍙槸涓嶆帓闄よ绠楃粨鏋滄椂鑰屾纭椂鑰岄敊璇殑鍙兘
浠庝笂杩颁唬鐮佷腑鍙互鎬荤粨鍑虹珵鎬佺殑涓ょ妯″紡锛?span class="mq-1549">read-modify-write锛堣-鏀?鍐欙級銆?span class="mq-1550">check-then-act锛堟娴嬪悗琛屽姩锛?/strong>
read-modify-write 鐨勬楠ゅ嵆锛氳鍙栦竴涓叡浜彉閲忕殑鍊硷紝鐒跺悗鏍规嵁璇ュ€艰繘琛屼竴浜涜绠椼€佹帴鐫€鏍规嵁璁$畻缁撴灉鏇存柊鍏变韩鍙橀噺鐨勫€笺€備緥濡傦紝涓婅堪浠g爜涓殑 goodsCount++
灏辨槸杩欑妯″紡锛岀浉褰撲簬濡備笅浼唬鐮佹墍琛ㄧず鐨勪笁涓寚浠ょ殑缁勫悎
load(shop.goodsCount , r1) //鎸囦护1锛屽皢鍙橀噺 shop.goodsCount 鐨勫€间粠鍐呭瓨璇诲埌瀵勫瓨鍣?r1
increment(r1) //鎸囦护2锛屽皢瀵勫瓨鍣?r1 鐨勫€煎姞1
store(shop.goodsCount , r1) //鎸囦护3锛屽皢瀵勫瓨鍣?r1 鐨勫唴瀹瑰啓鍏ュ彉閲?shop.goodsCount 鎵€瀵瑰簲鐨勫唴瀛樼┖闂?/span>
绾跨▼ A 鍦ㄦ墽琛屽畬鎸囦护1锛屽紑濮嬫墽琛屾垨鑰呮鍦ㄦ墽琛屾寚浠?鏃讹紝绾跨▼ B 鍙兘宸茬粡鎵ц瀹屼簡鎸囦护3锛岃繖浣垮緱绾跨▼ A 褰撳墠鎸佹湁鐨勫叡浜彉閲?shop.goodsCount 鏄棫鍊硷紝褰撶嚎绋?A 鎵ц瀹屾寚浠?鏃讹紝杩欏氨浣垮緱绾跨▼ B 瀵瑰叡浜彉閲忕殑鏇存柊琚鐩栦簡锛屽嵆閫犳垚浜嗘洿鏂颁涪澶?/p>
check-then-act 鐨勬楠ゅ嵆锛氳鍙栦竴涓叡浜彉閲忕殑鍊硷紝鏍规嵁璇ュ彉閲忕殑鍊煎喅瀹氫笅涓€姝ョ殑鍔ㄤ綔鏄粈涔堛€備緥濡傦紝浠ヤ笅浠g爜灏辨槸杩欑妯″紡
if (shop.goodsCount < 40) { //鎿嶄綔1
shop.produce() //鎿嶄綔2
}
绾跨▼ A 鍦ㄦ墽琛屽畬鎿嶄綔1锛屽紑濮嬫墽琛屾搷浣?涔嬪墠锛岀嚎绋?B 鍙兘宸茬粡鏇存柊浜嗗叡浜彉閲?nbsp;shop.goodsCount
鐨勫€煎鑷?if 璇彞涓殑鏉′欢鍙樹负涓嶆垚绔嬶紝鍙鏃剁嚎绋?A 渚濈劧浼氭墽琛屾搷浣?锛岃繖鏄洜涓虹嚎绋?A 姝ゆ椂骞朵笉鐭ラ亾鍏变韩鍙橀噺宸茬粡琚洿鏂颁笖瀵艰嚧杩愯鏉′欢涓嶆垚绔嬩簡
浠庝笂杩板垎鏋愪腑鎴戜滑鍙互鎬荤粨鍑虹珵鎬佷骇鐢熺殑涓€鑸潯浠躲€傝 Q1 鍜?Q2 鏄苟鍙戣闂叡浜彉閲?V 鐨勪袱涓搷浣滐紝杩欎袱涓搷浣滃苟闈為兘鏄鎿嶄綔銆傚鏋滀竴涓嚎绋嬪湪鎵ц Q1 鏈熼棿鍙﹀涓€涓嚎绋嬪悓鏃舵墽琛?Q2锛岄偅涔堟棤璁?Q2 鏄鎿嶄綔杩樻槸鍐欐搷浣滈兘浼氬鑷寸珵鎬併€備粠杩欎釜瑙掑害鏉ョ湅锛岀珵鎬佸彲浠ョ湅鍋氭槸鐢变簬璁块棶锛堣鍙栥€佹洿鏂帮級鍚屼竴缁勫叡浜彉閲忕殑澶氫釜绾跨▼鎵€鎵ц鐨勬搷浣滆鐩镐簰浜ら敊鑰屽鑷寸殑銆傝€屼笂杩颁唬鐮佷腑閬囧埌鐨?span class="mq-1619">鏇存柊涓㈠け鍜?span class="mq-1620">璇诲埌鑴忔暟鎹?/strong>闂灏辨槸鐢变簬绔炴€佺殑瀛樺湪鑰屽鑷寸殑
闇€瑕佹敞鎰忕殑鏄紝绔炴€佺殑浜х敓鍓嶆彁鏄秹鍙婂埌浜嗗涓嚎绋嬪拰鍏变韩鍙橀噺銆傚鏋滅郴缁熶粎鍖呭惈鍗曚釜绾跨▼锛屾垨鑰呬笉娑夊強鍏变韩鍙橀噺锛岄偅涔堝氨涓嶄細浜х敓绔炴€併€傚浜庡眬閮ㄥ彉閲忥紙鍖呮嫭褰㈠紡鍙傛暟鍜屾柟娉曚綋鍐呭畾涔夌殑鍙橀噺锛夛紝鐢变簬涓嶅悓鐨勭嚎绋嬭闂殑鏄悇鑷殑閭d竴浠藉眬閮ㄥ彉閲忥紝鍥犳灞€閮ㄥ彉閲忕殑浣跨敤涓嶄細瀵艰嚧绔炴€?/p>
2銆佺嚎绋嬪畨鍏ㄦ€?/h4>
濡傛灉涓€涓被鍦ㄥ崟绾跨▼鐜涓嬭兘澶熸甯歌繍琛岋紝骞朵笖鍦ㄥ绾跨▼鐜涓嬩篃涓嶉渶瑕佽€冭檻杩愯鏃剁幆澧冧笅鐨勮皟搴﹀拰浜ゆ浛鎵ц锛屼娇鐢ㄦ柟涔熶笉蹇呬负鍏跺仛澶氫换浣曟搷浣滀篃鑳芥甯歌繍琛岋紝閭d箞鎴戜滑灏辫璇ョ被鏄嚎绋嬪畨鍏ㄧ殑锛屽嵆杩欎釜绫诲叿鏈夌嚎绋嬪畨鍏ㄦ€с€傚弽涔嬶紝濡傛灉涓€涓被鍦ㄥ崟绾跨▼鐜涓嬫甯歌繍琛岃€屽湪澶氱嚎绋嬬幆澧冧笅鏃犳硶姝e父杩愯锛岄偅涔堣繖涓被灏辨槸闈炵嚎绋嬪畨鍏ㄧ殑銆傛墍浠ワ紝鍙湁闈炵嚎绋嬪畨鍏ㄧ殑绫绘墠浼氬鑷寸珵鎬?/p>
濡傛灉涓€涓被涓嶆槸绾跨▼瀹夊叏鐨勶紝鎴戜滑灏辫瀹冨湪澶氱嚎绋嬬幆澧冧笅瀛樺湪澶氱嚎绋嬪畨鍏ㄩ棶棰樸€備互涓婂畾涔変篃閫傜敤浜庡涓嚎绋嬮棿鐨勫叡浜暟鎹?/p>
澶氱嚎绋嬪畨鍏ㄩ棶棰樻鎷潵璇磋〃鐜颁负涓変釜鏂归潰锛?span class="mq-1626">鍘熷瓙鎬с€佸彲瑙佹€с€佹湁搴忔€?/strong>
1銆佸師瀛愭€?/h4>
鍘熷瓙鐨勫瓧闈㈡剰鎬濆嵆涓嶅彲鍒嗗壊銆傚浜庢秹鍙婂叡浜彉閲忚闂殑鎿嶄綔锛岃嫢璇ユ搷浣滀粠鍏舵墽琛岀嚎绋嬩互澶栫殑鍏跺畠浠绘剰绾跨▼鏉ョ湅鏄笉鍙垎鍓茬殑锛岄偅涔堣鎿嶄綔灏辨槸鍘熷瓙鎿嶄綔锛岀浉搴旂殑灏辩О璇ユ搷浣滃叿鏈?span class="mq-1631">鍘熷瓙鎬?/strong>銆傛墍璋撯€滀笉鍙垎鍓测€濓紝鏄寚璁块棶锛堣銆佸啓锛夋煇涓叡浜彉閲忕殑鎿嶄綔浠庡叾鎵ц绾跨▼浠ュ鐨勪换浣曠嚎绋嬫潵鐪嬶紝璇ユ搷浣滆涔堝凡缁忓畬鎴愶紝瑕佷箞灏氭湭鍙戠敓锛屽叾瀹冪嚎绋嬩笉浼氱湅鍒拌鎿嶄綔鎵ц浜嗕竴閮ㄥ垎鐨勪腑闂存晥鏋?/p>
渚嬪锛屽亣璁惧瓨鍦ㄤ竴涓叡浜殑鍏ㄥ眬鍙橀噺 Shop 瀵硅薄锛屽叾瀛樺湪涓€涓?nbsp; 鐞嗚В鍘熷瓙鎿嶄綔杩欎釜姒傚康杩橀渶瑕佹敞鎰忎互涓嬩袱鐐癸細 鈥?/span>鍘熷瓙鎿嶄綔鏄拡瀵瑰叡浜彉閲忕殑鎿嶄綔鑰岃█鐨勶紝浠呮秹鍙婂眬閮ㄥ彉閲忚闂殑鎿嶄綔鏃犳墍璋撴槸鍚︽槸鍘熷瓙鎬э紝鎴栬€呭彲浠ョ洿鎺ュ皢鍏剁湅浣滄垚鍘熷瓙鎿嶄綔鈥?/span>鍘熷瓙鎿嶄綔鏄粠璇ユ搷浣滅殑鎵ц绾跨▼浠ュ鐨勫叾瀹冪嚎绋嬬殑瑙嗚鏉ユ弿杩扮殑锛屼篃灏辨槸璇村畠鍙湪澶氱嚎绋嬬幆澧冧笅鎵嶆湁鎰忎箟锛屾墍浠ュ彲浠ュ皢鍗曠嚎绋嬬幆澧冧笅鐨勬墍鏈夋搷浣滃潎褰撳仛鍘熷瓙鎿嶄綔 鎬荤殑鏉ヨ锛孞ava 涓湁涓ょ鏂瑰紡鏉ユ彁渚涘師瀛愭€э細 鈥?/span>绗竴绉嶆槸浣跨敤閿侊紙Lock锛夈€傞攣鍏锋湁鎺掍粬鎬э紝瀹冭兘澶熶繚闅滃叡浜彉閲忓湪浠绘剰涓€涓椂鍒诲彧鑳藉琚竴涓嚎绋嬭闂€傝繖灏辨帓闄や簡澶氫釜绾跨▼鍦ㄥ悓涓€鏃跺埢璁块棶鍚屼竴涓叡浜彉閲忚€屽鑷村共鎵颁笌鍐茬獊鐨勫彲鑳斤紝浠庤€屾秷闄や簡绔炴€?/span>鈥?/span>绗簩绉嶆槸鍒╃敤澶勭悊鍣ㄦ彁渚涚殑 CAS 鎸囦护銆侰AS 鎸囦护瀹炵幇鍘熷瓙鎬х殑鏂瑰紡涓庨攣鍦ㄦ湰璐ㄤ笂鏄浉鍚岀殑锛屽樊鍒湪浜庨攣閫氬父鏄湪杞欢杩欎竴灞傞潰瀹炵幇鐨勶紝鑰?CAS 鏄洿鎺ュ湪纭欢锛堝鐞嗗櫒鍜屽唴瀛橈級杩欎竴灞傛瀹炵幇鐨勶紝鍙互琚湅鍋氣€滅‖浠堕攣鈥?/span> Java 璇█瑙勮寖瑙勫畾浜嗭細鍦?Java 璇█涓紝64 浣嶄互澶栫殑浠讳綍绫诲瀷鐨勫彉閲忕殑璇诲啓鎿嶄綔閮芥槸鍘熷瓙鎿嶄綔銆傝€屽浜?long 鍜?double 绛?64 浣嶇殑鏁版嵁绫诲瀷鐨勮鍐欐搷浣滃苟涓嶅己鍒惰瀹?Java 铏氭嫙鏈哄繀椤讳繚璇佸叾鍘熷瓙鎬э紝鍙互鐢?Java 铏氭嫙鏈鸿嚜宸遍€夋嫨鏄惁瑕佸疄鐜般€傚洜姝ゅ湪澶氱嚎绋嬪苟鍙戣鍐欏悓涓€ long/double 鍨嬪叡浜彉閲忕殑鎯呭喌涓嬶紝涓€涓嚎绋嬪彲鑳戒細璇诲彇鍒板叾瀹冪嚎绋嬫洿鏂拌鍙橀噺鐨勨€滀腑闂寸粨鏋溾€濄€傝€屼箣鎵€浠ヤ細鏈変腑闂寸粨鏋滐紝鏄洜涓哄浜?64 浣嶇殑瀛樺偍绌洪棿鐨勫啓鎿嶄綔锛岃櫄鎷熸満鍙兘浼氬皢鍏舵媶瑙d负涓や釜姝ラ鏉ュ疄鐜帮紝姣斿鍏堝啓浣?32 浣嶅啀鍐欓珮 32 浣嶏紝浠庤€屽鑷村閮ㄧ嚎绋嬭鍙栧埌涓€涓腑闂寸粨鏋滃€笺€備絾杩欎釜闂涔熶笉闇€瑕佺壒鎰忓叧娉紝鍥犱负鐩墠鍟嗙敤 Java 铏氭嫙鏈哄嚑涔庨兘閫夋嫨灏?64 浣嶆暟鎹殑璇诲啓鎿嶄綔瀹炵幇涓哄師瀛愭搷浣溿€傛澶栵紝Java 璇█瑙勮寖涔熺壒鍒瀹氫簡鐢?volatile 鍏抽敭瀛椾慨楗扮殑 long/double 鍨嬪彉閲忕殑璇诲啓鎿嶄綔鍏锋湁鍘熷瓙鎬с€?/p>
闇€瑕佹敞鎰忕殑鏄紝 volatile 鍏抽敭瀛椾粎鑳戒繚闅滃彉閲忚鍐欐搷浣滅殑鍘熷瓙鎬э紝浣嗗苟涓嶈兘淇濋殰鍏跺畠鎿嶄綔锛堜緥濡?read-modify-write 銆乧heck-then-act锛夌殑鍘熷瓙鎬?/p>
鍦ㄥ绾跨▼鐜涓嬶紝涓€涓嚎绋嬪鏌愪釜鍏变韩鍙橀噺杩涜鏇存柊涔嬪悗锛屽悗缁闂鍙橀噺鐨勫叾瀹冪嚎绋嬪彲鑳芥棤娉曠珛鍗宠鍙栧埌杩欎釜鏇存柊鐨勭粨鏋滐紝鐢氳嚦姘歌繙涔熸棤娉曡鍙栧埌锛岃繖浣撶幇浜嗗绾跨▼瀹夊叏鎬ч棶棰樹腑鐨勪竴涓細鍙鎬с€傚彲瑙佹€ф槸鎸囦竴涓嚎绋嬪鍏变韩鍙橀噺鐨勬洿鏂扮粨鏋滃浜庡叾瀹冭鍙栫浉搴斿叡浜彉閲忕殑绾跨▼鑰岃█鏄惁鍙鐨勯棶棰樸€傚绾跨▼绋嬪簭鍦ㄥ彲瑙佹€ф柟闈㈠瓨鍦ㄩ棶棰樻剰鍛崇潃鏌愪簺绾跨▼璇诲彇鍒颁簡鏃ф暟鎹紝鑰岃繖寰€寰€浼氬鑷存垜浠殑绋嬪簭鍑虹幇鎰忔兂涓嶅埌鐨勯棶棰?/p>
浼氬瓨鍦ㄥ彲瑙佹€ч棶棰樸€備竴鏂归潰鏄敱浜?JIT 缂栬瘧鍣ㄥ彲鑳藉嚭浜庢彁楂樹唬鐮佽繍琛屾晥鐜囪€冭檻鑰岃嚜鍔ㄥ浠g爜杩涜涓€浜涒€滀紭鍖栤€濓紝浣垮緱鍏变韩鍙橀噺鏇存柊澶辨晥銆備竴鏂归潰鏄敱浜庡鐞嗗櫒骞朵笉鏄洿鎺ュ涓诲唴瀛樹腑鐨勫叡浜彉閲忚繘琛岃闂紝鑰屾槸浼氬悇鑷湪鑷繁鐨勯珮閫熺紦瀛樹笂淇濈暀鐫€瀵瑰叡浜彉閲忕殑涓€浠藉壇鏈紝澶勭悊鍣ㄧ洿鎺ヨ闂殑鏄壇鏈暟鎹紝瀵瑰壇鏈暟鎹殑淇敼闇€瑕佸悓姝ュ洖涓诲唴瀛樺悗鎵嶅彲浠ュ鍏跺畠澶勭悊鍣ㄥ彲瑙併€傛墍浠ヤ竴涓鐞嗗櫒瀵瑰叡浜彉閲忕殑鏇存柊缁撴灉骞朵笉涓€瀹氳兘绔嬪嵆鍚屾鍒板叾瀹冨鐞嗗櫒涓婏紝杩欏氨瀵艰嚧浜嗗彲瑙佹€ч棶棰樼殑鍑虹幇 瀵逛簬鍚屼竴涓叡浜彉閲忚€岃█锛屼竴涓嚎绋嬫洿鏂颁簡璇ュ彉閲忕殑鍊间箣鍚庯紝濡傛灉鍏跺畠绾跨▼鑳藉璇诲彇鍒拌繖涓洿鏂板悗鐨勫€硷紝閭d箞杩欎釜鍊煎氨琚О涓鸿鍙橀噺鐨勭浉瀵规柊鍊笺€傚鏋滆鍙栬繖涓叡浜彉閲忕殑绾跨▼鍦ㄨ鍙栧苟浣跨敤璇ュ彉閲忕殑鏃跺€欏叾瀹冪嚎绋嬫棤娉曟洿鏂拌鍙橀噺鐨勫€硷紝閭d箞璇ョ嚎绋嬭鍙栧埌鐨勫€煎氨琚О涓鸿鍙橀噺鐨勬渶鏂板€笺€傚彲瑙佹€х殑淇濋殰浠呬粎鎰忓懗鐫€涓€涓嚎绋嬭兘澶熻鍙栧埌鍏变韩鍙橀噺鐨勭浉瀵规柊鍊硷紝鑰屽苟涓嶆剰鍛崇潃绾跨▼鑳藉璇诲彇鍒扮浉搴斿彉閲忕殑鏈€鏂板€?/p>
鍙鎬ч棶棰樻槸鐢变簬浣跨敤浜嗗绾跨▼鎵€瀵艰嚧鐨勶紝瀹冧笌褰撳墠鏄崟鏍稿鐞嗗櫒杩樻槸澶氭牳澶勭悊鍣ㄦ棤鍏炽€傚湪鍗曟牳澶勭悊鍣ㄤ笅锛屽绾跨▼骞跺彂鏄€氳繃鏃堕棿鐗囧垎閰嶆妧鏈潵瀹炵幇鐨勶紝姝ゆ椂铏界劧澶氫釜绾跨▼閮芥槸杩愯鍦ㄥ悓涓鐞嗗櫒涓婏紝浣嗘槸鐢变簬鍦ㄤ笂涓嬫枃鍒囨崲鐨勬椂鍊欙紝涓€涓嚎绋嬪鍏变韩鍙橀噺鐨勪慨鏀逛細琚綋鍋氬叾涓婁笅鏂囦俊鎭繚瀛樿捣鏉ワ紝杩欎篃浼氬鑷村彟澶栦竴涓嚎绋嬫棤娉曠珛鍗宠鍙栧埌璇ョ嚎绋嬪鍏变韩鍙橀噺鐨勪慨鏀?/p>
鍦ㄨ鏈夊簭鎬т箣鍓嶏紝闇€瑕佸厛浠嬬粛涓?span class="mq-1708">閲嶆帓搴?/strong> 椤哄簭缁撴瀯鏄粨鏋勫寲缂栫▼涓殑涓€绉嶅熀鏈粨鏋勶紝瀹冭〃绀烘垜浠笇鏈涙煇涓搷浣滃繀椤诲厛浜庡彟澶栦竴涓搷浣滃緱浠ユ墽琛岋紝浣嗘槸鍦ㄥ鏍稿鐞嗗櫒鐜涓嬶紝杩欑鎿嶄綔鎵ц椤哄簭鍙兘鏄病鏈変繚闅滅殑銆傜紪璇戝櫒鍜屽鐞嗗櫒鍙互鍦ㄤ繚璇佷笉褰卞搷鍗曠嚎绋嬫墽琛岀粨鏋滅殑鍓嶆彁涓嬶紝瀵规簮浠g爜鐨勬寚浠よ繘琛岄噸鏂版帓搴忔墽琛岋紝澶勭悊鍣ㄥ彲鑳戒笉鏄畬鍏ㄤ緷鐓х▼搴忕殑鐩爣浠g爜鎵€鎸囧畾鐨勯『搴忔潵鎵ц鎸囦护銆傚彟澶栵紝涓€涓鐞嗗櫒涓婃墽琛岀殑澶氫釜鎿嶄綔锛屼粠鍏跺畠澶勭悊鍣ㄧ殑瑙掑害鏉ョ湅鍏堕『搴忓彲鑳戒笌鐩爣浠g爜鎵€鎸囧畾鐨勯『搴忎笉涓€鑷淬€傝繖绉嶇幇璞″氨鍙仛閲嶆帓搴忋€傞噸鎺掑簭鏄鍐呭瓨璁块棶鏈夊叧鐨勬搷浣滐紙璇诲拰鍐欙級鎵€鍋氱殑涓€绉嶄紭鍖栵紝瀹冨彲浠ュ湪涓嶅奖鍝嶅崟绾跨▼绋嬪簭姝g‘鎬х殑鎯呭喌涓嬫彁鍗囩▼搴忕殑鎬ц兘銆備絾鏄紝瀹冨彲鑳藉澶氱嚎绋嬬▼搴忕殑姝g‘鎬т骇鐢熷奖鍝嶏紝鍗冲畠鍙兘瀵艰嚧绾跨▼瀹夊叏闂 閲嶆帓搴忓垎涓轰互涓嬪嚑绉嶏細 鈥?/span>缂栬瘧鍣ㄤ紭鍖栫殑閲嶆帓搴忋€傜紪璇戝櫒鍦ㄤ笉鏀瑰彉鍗曠嚎绋嬬▼搴忚涔夌殑鍓嶆彁涓嬶紝鍙互閲嶆柊瀹夋帓璇彞鐨勬墽琛岄『搴?/span>鈥?/span>鎸囦护绾у苟琛岀殑閲嶆帓搴忋€傜幇浠e鐞嗗櫒閲囩敤浜嗘寚浠ょ骇骞惰鎶€鏈潵灏嗗鏉℃寚浠ら噸鍙犳墽琛屻€傚鏋滀笉瀛樺湪鏁版嵁渚濊禆鎬э紝澶勭悊鍣ㄥ彲浠ユ敼鍙樿鍙ュ搴旀満鍣ㄦ寚浠ょ殑鎵ц椤哄簭鈥?/span>鍐呭瓨绯荤粺鐨勯噸鎺掑簭銆傜敱浜庡鐞嗗櫒浣跨敤缂撳瓨鍜岃锛忓啓缂撳啿鍖猴紝杩欎娇寰楀姞杞藉拰瀛樺偍鎿嶄綔鐪嬩笂鍘诲彲鑳芥槸鍦ㄤ贡搴忔墽琛?/span> 鏈夊簭鎬ф寚鐨勫氨鏄湪浠€涔堟儏鍐典笅涓€涓鐞嗗櫒涓婅繍琛岀殑绾跨▼鎵€鎵ц鐨勫唴瀛樿闂『搴忓湪鍙﹀涓€涓鐞嗗櫒涓婅繍琛岀殑鍏跺畠绾跨▼鐪嬫潵鏄贡搴忕殑銆傛墍璋撲贡搴忥紝鏄寚鍐呭瓨璁块棶鎿嶄綔鐨勯『搴忕湅璧锋潵鏄彂鐢熶簡鍙樺寲 涓婃枃鏈夋彁鍒板浣曞畾涔変竴涓被鏄惁鏄嚎绋嬪畨鍏ㄧ殑锛孞ava 涔熸彁渚涗簡寰堝琚О涓虹嚎绋嬪畨鍏ㄧ殑绫伙紝渚嬪 渚嬪锛屽浜庝互涓嬩唬鐮併€傚嵆浣?nbsp; 鎵€浠ヨ锛岀嚎绋嬪畨鍏ㄧ被鍙兘鍙槸淇濋殰浜嗗叾鑷韩鍗曟鎿嶄綔琛屼负鐨勭嚎绋嬪畨鍏ㄦ€э紝浣垮緱鎴戜滑鍦ㄨ皟鐢ㄧ殑鏃跺€欎笉闇€瑕佽繘琛岄澶栫殑鍚屾淇濋殰銆備絾瀵逛簬浣跨敤鏂圭殑涓€浜涚壒瀹氶『搴忕殑杩炵画璋冪敤锛屽氨杩樻槸闇€瑕佸湪澶栭儴瀹炵幇棰濆鐨勫悓姝ユ墜娈垫潵淇濊瘉璋冪敤鐨勬纭€э紝鍚﹀垯灏辨湁鍙兘鐢ㄧ嚎绋嬪畨鍏ㄧ被鍐欏嚭涓嶅畨鍏ㄧ殑浠g爜 骞跺彂鐨勫疄鐜板拰鏄惁鎷ユ湁澶氫釜澶勭悊鍣ㄦ棤鍏筹紝鍗充娇鍙湁鍗曚釜澶勭悊鍣ㄤ篃鑳藉閫氳繃澶勭悊鍣?span class="mq-2054">鏃堕棿鐗囧垎閰?/strong>鎶€鏈潵瀹炵幇骞跺彂銆傛搷浣滅郴缁熼€氳繃缁欐瘡涓嚎绋嬪垎閰嶄竴灏忔鍗犳湁澶勭悊鍣ㄤ娇鐢ㄦ潈鐨勬椂闂存潵渚涘叾杩愯锛岀劧鍚庡湪姣忎釜绾跨▼鐨勮繍琛屾椂闂寸粨鏉熷悗鍙堝揩閫熷垏鎹㈠埌涓嬩竴涓嚎绋嬫潵杩愯锛屽涓嚎绋嬩互杩欑鏂柇缁画鐨勬柟寮忔潵瀹炵幇骞跺彂骞跺畬鎴愬悇鑷殑浠诲姟銆備竴涓嚎绋嬭鍓ュず澶勭悊鍣ㄧ殑浣跨敤鏉冨苟鏆傚仠杩愯鐨勮繃绋嬪氨琚О涓哄垏鍑猴紝琚嚎绋嬭皟搴﹀櫒閫変腑鏉ュ崰鐢ㄥ鐞嗗櫒骞惰繍琛岀殑杩囩▼灏辫绉颁负鍒囧叆 鎿嶄綔绯荤粺浼氬垎鍑轰竴涓釜鏃堕棿鐗囷紝姣忎釜绾跨▼姣忔杩愯浼氬垎閰嶅埌鑻ュ共涓椂闂寸墖锛屾椂闂寸墖鍐冲畾浜嗕竴涓嚎绋嬪彲浠ヨ繛缁崰鐢ㄥ鐞嗗櫒杩愯鐨勬椂闂撮暱搴︼紝涓€鑸槸鍙湁鍑犲崄姣锛屽崟澶勭悊鍣ㄤ笂鐨勫绾跨▼灏辨槸閫氳繃杩欑鏃堕棿鐗囧垎閰?/strong>鐨勬柟寮忔潵瀹炵幇骞跺彂銆傚綋涓€涓繘绋嬩腑鐨勪竴涓嚎绋嬬敱浜庡叾鏃堕棿鐗囩敤瀹屾垨鑰呯敱浜庡叾鑷韩鐨勫師鍥犺杩垨鑰呬富鍔ㄦ殏鍋滃叾杩愯鏃讹紝鍙﹀涓€涓嚎绋嬶紙褰撳墠杩涚▼涓殑绾跨▼鎴栬€呭叾瀹冭繘绋嬩腑鐨勭嚎绋嬶級灏卞彲浠ヨ绾跨▼璋冨害鍣ㄩ€変腑鏉ュ崰鐢ㄥ鐞嗗櫒骞跺紑濮嬭繍琛屻€傝繖绉嶄竴涓嚎绋嬭鍓ュず澶勭悊鍣ㄧ殑浣跨敤鏉冨苟鏆傚仠杩愯锛屽彟澶栦竴涓嚎绋嬭璧嬩簣澶勭悊鍣ㄧ殑浣跨敤鏉冨苟寮€濮嬭繍琛岀殑杩囩▼灏辩О涓虹嚎绋嬩笂涓嬫枃鍒囨崲 绾跨▼涓婁笅鏂囧垏鎹㈡槸澶勭悊鍣ㄤ釜鏁拌繙灏忎簬绯荤粺鎵€闇€瑕佹敮鎸佺殑骞跺彂绾跨▼鏁?/strong>鐨勭幇瀹炲満鏅笅鐨勫繀鐒朵骇鐗┿€傝繖涔熸剰鍛崇潃鍦ㄧ嚎绋嬪垏鍑哄拰鍒囧叆鐨勬椂鍊欐搷浣滅郴缁熼渶瑕佷繚瀛樺拰鎭㈠鐩稿簲绾跨▼鐨勮繘搴︿俊鎭紝鍗抽渶瑕佷繚瀛樺垏鍏ュ拰鍒囧嚭閭d竴鍒荤浉搴旂嚎绋嬫墍鎵ц鐨勬寚浠よ繘琛屽埌浠€涔堝摢涓€姝ヤ簡銆傝繖涓繘搴︿俊鎭氨琚О涓轰笂涓嬫枃 绾跨▼鐨勭敓鍛藉懆鏈熺姸鎬佸湪 RUNNABLE 鐘舵€佷笌闈?RUNNABLE 鐘舵€佷箣闂村垏鎹㈢殑杩囩▼灏辨槸涓婁笅鏂囧垏鎹㈢殑杩囩▼銆傚綋琚殏鍋滅殑绾跨▼琚搷浣滅郴缁熼€変腑鑾峰緱缁х画杩愯鐨勬満浼氭椂锛屾搷浣滅郴缁熶細鎭㈠涔嬪墠涓鸿绾跨▼淇濆瓨鐨勪笂涓嬫枃锛屼互渚垮叾鍦ㄦ鍩虹涓婄户缁畬鎴愬叾浠诲姟 鎸夌収瀵艰嚧涓婁笅鏂囧垏鎹㈢殑鍥犵礌鏉ュ垝鍒嗭紝鍙互灏嗕笂涓嬫枃鍒囨崲鍒掑垎涓?span class="mq-2061">鑷彂鎬т笂涓嬫枃鍒囨崲鍜?span class="mq-2062">闈炶嚜鍙戞€т笂涓嬫枃鍒囨崲 鈥?/span>鑷彂鎬т笂涓嬫枃鍒囨崲銆傝繖绉嶆儏鍐垫槸绾跨▼鐢变簬鍏惰嚜韬師鍥犲鑷寸殑鍒囧嚭銆備粠 Java 骞冲彴鐨勮搴︽潵鐪嬶紝涓€涓嚎绋嬪湪鍏惰繍琛岃繃绋嬩腑鎵ц浜嗕互涓嬩换浣曚竴涓搷浣滈兘浼氬紩璧疯嚜鍙戞€т笂涓嬫枃鍒囨崲鈥?/span>Thread.sleep()鈥?/span>Object.wait()鈥?/span>Thread.yieid()鈥?/span>Thread.join()鈥?/span>LockSupport.park()鈥?/span>I/O 鎿嶄綔鈥?/span>绛夊緟琚叾瀹冪嚎绋嬫寔鏈夌殑閿?/span>鈥?/span>闈炶嚜鍙戞€т笂涓嬫枃鍒囨崲銆傛寚绾跨▼鐢变簬绾跨▼璋冨害鍣ㄧ殑鍘熷洜琚揩鍒囧嚭銆傝繖绉嶆儏鍐靛線寰€鏄敱浜庤鍒囧嚭鐨勭嚎绋嬬殑鏃堕棿鐗囩敤瀹岋紝鎴栬€呮湁涓€涓瘮琚垏鍑虹嚎绋嬫洿楂樹紭鍏堢骇鐨勭嚎绋嬮渶瑕佽繍琛屻€傛澶栵紝Java 铏氭嫙鏈虹殑鍨冨溇鍥炴敹鍔ㄤ綔涔熷彲鑳藉鑷撮潪鑷彂鎬т笂涓嬫枃鍒囨崲锛岃繖鏄洜涓哄瀮鍦惧洖鏀跺櫒鍦ㄦ墽琛?GC 鐨勮繃绋嬩腑鍙兘闇€瑕佹殏鍋滄墍鏈夊簲鐢ㄧ嚎绋嬫墠鑳藉畬鎴?/span> 绯荤粺鍦ㄤ竴娈垫椂闂村唴浜х敓鐨勪笂涓嬫枃鍒囨崲娆℃暟瓒婂锛岀敱姝ゅ鑷寸殑澶勭悊鍣ㄨ祫婧愭秷鑰椾篃灏辫秺澶氾紝鐩稿簲鐨勮繖娈垫椂闂村唴鐪熸鑳藉鐢ㄤ簬鎵ц鐩爣浠g爜鐨勫鐞嗗櫒璧勬簮灏辫秺灏戯紝鍥犳鎴戜滑涔熼渶瑕佽€冭檻灏介噺鍑忓皯涓婁笅鏂囧垏鎹㈢殑娆℃暟锛岃繖鍦ㄥ悗缁枃绔犱腑浼氫粙缁?/p>
绾跨▼璋冨害鏄寚鎿嶄綔绯荤粺涓虹嚎绋嬪垎閰嶅鐞嗗櫒浣跨敤鏉冪殑杩囩▼銆備富瑕佺殑璋冨害鏂瑰紡鏈変袱绉嶏細 鈥?/span>鍗忓悓寮忕嚎绋嬭皟搴︺€傚湪杩欑绛栫暐涓嬶紝绾跨▼鐨勬墽琛屾椂鏈虹敱绾跨▼鏈韩鏉ュ喅瀹氾紝绾跨▼閫氳繃涓诲姩閫氱煡绯荤粺鍒囨崲鍒板彟涓€涓嚎绋嬬殑鏂瑰紡鏉ヨ鍑哄鐞嗗櫒鐨勪娇鐢ㄦ潈銆傝绛栫暐鐨勪紭鐐规槸瀹炵幇绠€鍗曪紝鍙互閫氳繃绮惧噯鎺у埗绾跨▼鐨勬墽琛岄『搴忔潵閬垮厤绾跨▼瀹夊叏鎬ч棶棰樸€傜己鐐规槸鍙兘浼氱敱浜庡崟涓嚎绋嬬殑浠g爜缂洪櫡闂瀵艰嚧鏃犳硶鍒囨崲鍒颁笅涓€涓嚎绋嬶紝鏈€缁堝鑷磋繘绋嬭闃诲鈥?/span>鎶㈠崰寮忕嚎绋嬭皟搴︺€傝繖涔熸槸 Java 骞冲彴浣跨敤鐨勭嚎绋嬭皟搴︾瓥鐣ャ€傚湪杩欑绛栫暐涓嬶紝鐢辨搷浣滅郴缁熸潵鍐冲畾褰撳墠澶勭悊鍣ㄦ椂闂寸墖浜ょ敱鍝釜绾跨▼鏉ヤ娇鐢紝绾跨▼鏃犳硶鍐冲畾鍏蜂綋鐨勮繍琛屾椂鏈哄拰杩愯椤哄簭銆傝櫧鐒舵垜浠彲浠ラ€氳繃 涓€娆″彧鑳借涓€涓嚎绋嬪崰鐢ㄧ殑璧勬簮绉颁负鎺掍粬鎬ц祫婧愩€傚父瑙佺殑鎺掍粬鎬ц祫婧愬寘鎷?span class="mq-2094">閿併€佸鐞嗗櫒銆佹枃浠?/strong>绛夈€傜敱浜庤祫婧愮殑绋€缂烘€ф垨鑰呰祫婧愭湰韬殑鐗规€э紝鎴戜滑寰€寰€闇€瑕佸湪澶氫釜绾跨▼闂村叡浜悓涓€涓帓浠栨€ц祫婧愩€傚綋涓€涓嚎绋嬪崰鐢ㄤ竴涓帓浠栨€ц祫婧愯€屾湭閲婃斁鍏跺璧勬簮鐨勬墍鏈夋潈鏃讹紝瀛樺湪鍏跺畠绾跨▼鍚屾椂璇曞浘璁块棶璇ヨ祫婧愮殑鐜拌薄灏辫绉颁负璧勬簮浜夌敤锛岀畝绉颁簤鐢ㄣ€傛樉鐒讹紝浜夌敤鏄湪骞跺彂鐜涓嬩骇鐢熺殑涓€绉嶇幇璞★紝鍚屾椂璇曞浘璁块棶涓€涓凡缁忚鍏跺畠绾跨▼鍗犵敤鐨勬帓浠栨€ц祫婧愮殑绾跨▼鏁伴噺瓒婂锛屼簤鐢ㄧ殑绋嬪害灏辫秺楂橈紝鍙嶄箣浜夌敤鐨勭▼搴﹀氨瓒婁綆銆傜浉搴旂殑浜夌敤灏辫绉颁负楂樹簤鐢ㄥ拰浣庝簤鐢?/strong> 鍚屼竴鏃堕棿娈靛唴锛屽浜庤繍琛岀姸鎬佺殑绾跨▼鏁伴噺瓒婂锛屾垜浠氨绉板苟鍙戠殑绋嬪害瓒婇珮锛岀畝绉伴珮骞跺彂銆傝櫧鐒堕珮骞跺彂鍔犲ぇ浜嗕簤鐢ㄧ殑鍙兘鎬э紝浣嗘槸楂樺苟鍙戞湭蹇呭氨鎰忓懗鐫€楂樹簤鐢紝鍥犱负绾跨▼骞堕潪灏辨槸涓€瀹氫細鍦ㄦ煇涓椂鍒绘潵涓€璧风敵璇疯祫婧愶紝璧勬簮鐨勭敵璇锋搷浣滃浜庡涓嚎绋嬫潵璇村彲鑳芥槸浜ら敊寮€鐨勶紝鎴栬€呮瘡涓嚎绋嬫寔鏈夋帓浠栨€ц祫婧愮殑鏃堕棿寰堢煭銆傚绾跨▼缂栫▼鐨勭悊鎯虫儏鍐靛氨鏄珮骞跺彂銆佷綆浜夌敤 褰撳涓嚎绋嬪悓鏃剁敵璇峰悓涓€涓帓浠栨€ц祫婧愶紝鐢宠璧勬簮澶辫触鐨勭嚎绋嬪線寰€鏄細瀛樺叆涓€涓瓑寰呴槦鍒椾腑锛屽綋鍚庣画璧勬簮琚叾鎸佹湁绾跨▼閲婃斁鏃讹紝濡傛灉鍒氬ソ鏈変竴涓椿璺冪嚎绋嬫潵鐢宠璧勬簮锛屾鏃堕€夋嫨鍝竴涓嚎绋嬫潵鑾峰彇璧勬簮鐨勭嫭鍗犳潈灏辨槸涓€涓祫婧愯皟搴︾殑杩囩▼锛岃祫婧愯皟搴︾瓥鐣ョ殑涓€涓噸瑕佸睘鎬у氨鏄兘鍚?span class="mq-2100">淇濊瘉鍏钩鎬?/strong>銆傛墍璋撳叕骞虫€э紝鏄寚璧勬簮鐨勭敵璇疯€呮槸鍚︿弗鏍兼寜鐓х敵璇烽『搴忚€岃鎺堜簣璧勬簮鐨勭嫭鍗犳潈銆傚鏋滆祫婧愮殑浠讳綍涓€涓厛鐢宠鑰呮€绘槸鑳藉琚瘮浠讳綍涓€涓悗鐢宠鑰呭厛鑾峰緱璧勬簮鐨勭嫭鍗犳潈锛岄偅涔堣绛栫暐灏辫绉颁负鍏钩璋冨害绛栫暐銆傚鏋滆祫婧愮殑鍚庣敵璇疯€呭彲鑳芥瘮鍏堢敵璇疯€呭厛鑾峰緱璧勬簮鐨勭嫭鍗犳潈锛岄偅涔堣绛栫暐灏辫绉颁负闈炲叕骞宠皟搴︾瓥鐣?/strong>銆傛敞鎰忥紝闈炲叕骞宠皟搴︾瓥鐣ュ線寰€鍙槸涓嶄繚璇佽祫婧愯皟搴︾殑鍏钩鎬э紝鍗冲畠鍙槸鍏佽涓嶅叕骞崇殑璧勬簮璋冨害鐜拌薄锛岃€屼笉鏄〃绀哄畠鍒绘剰閫犲氨涓嶅叕骞崇殑璧勬簮璋冨害 鍏钩鐨勮祫婧愯皟搴︾瓥鐣ヤ笉鍏佽鎻掗槦鐜拌薄鐨勫嚭鐜帮紝璧勬簮鐢宠鑰呮€绘槸鎸夌収鍏堟潵鍚庡埌鐨勯『搴忚幏寰楄祫婧愮殑鐙崰鏉冦€傚鏋滃綋鍓嶇瓑寰呴槦鍒椾负绌猴紝鍒欐潵鐢宠璧勬簮鐨勭嚎绋嬪彲浠ョ洿鎺ヨ幏寰楄祫婧愮殑鐙崰鏉冦€傚鏋滅瓑寰呴槦鍒椾笉涓虹┖锛岄偅涔堟瘡涓柊鍒版潵鐨勭嚎绋嬪氨琚彃鍏ョ瓑寰呴槦鍒楃殑闃熷熬銆傚叕骞崇殑璧勬簮璋冨害绛栫暐鐨勪紭鐐规槸锛氭瘡涓祫婧愮敵璇疯€呬粠寮€濮嬬敵璇疯祫婧愬埌鑾峰緱鐩稿簲璧勬簮鐨勭嫭鍗犳潈鎵€闇€鏃堕棿鐨勫亸宸細姣旇緝灏忥紝鍗虫瘡涓敵璇疯€呮垚鍔熺敵璇峰埌璧勬簮鎵€闇€鐨勬椂闂村熀鏈浉鍚岋紝涓斿彲浠ラ伩鍏嶅嚭鐜扮嚎绋嬮ゥ楗跨幇璞°€傜己鐐规槸鍚炲悙鐜囪緝浣庯紝涓轰簡淇濊瘉 FIFO 鍔犲ぇ浜嗗彂鐢熺嚎绋嬩笂涓嬫枃鍒囨崲鐨勫彲鑳芥€?/p>
闈炲叕骞崇殑璧勬簮璋冨害绛栫暐鍒欏厑璁告彃闃熺幇璞°€傛柊鍒版潵鐨勭嚎绋嬩細鐩存帴灏濊瘯鐢宠璧勬簮锛屽彧鏈夊綋鐢宠澶辫触鏃舵墠浼氬皢绾跨▼鎻掑叆绛夊緟闃熷垪鐨勯槦灏俱€傚亣璁句袱绉嶅涓嚎绋嬩竴璧风珵浜夊悓涓€涓帓浠栨€ц祫婧愮殑鍦烘櫙锛?/p>
1.褰撹祫婧愯閲婃斁鏃讹紝濡傛灉鍒氬ソ鏈変竴涓椿璺冪嚎绋嬫潵鐢宠璧勬簮锛岃绾跨▼灏卞彲浠ョ洿鎺ユ姠鍗犲埌璧勬簮锛岃€屾棤闇€鍘诲敜閱掔瓑寰呴槦鍒椾腑鐨勭嚎绋嬨€傝繖绉嶅満鏅浉瀵瑰叕骞宠皟搴︾瓥鐣ュ氨灏戜簡灏嗘柊鍒版潵鐨勭嚎绋嬫殏鍋?/strong>鍜?span class="mq-2109">灏嗙瓑寰呴槦鍒楅槦澶寸殑绾跨▼鍞ら啋鐨勪袱涓搷浣滐紝鑰岃祫婧愪篃涓€鏍锋湁琚緱鍒颁娇鐢?/span>2.鍗充娇绛夊緟闃熷垪涓殑鏌愪釜绾跨▼宸茬粡琚敜閱掓潵璇曞浘鎶㈠崰璧勬簮鐨勭嫭鍗犳潈锛屽鏋滄柊鍒版潵鐨勬椿璺冪嚎绋嬪崰鐢ㄨ祫婧愮殑鏃堕棿涓嶉暱鐨勮瘽锛岄偅涔堝氨鏈夊彲鑳藉湪琚敜閱掔殑绾跨▼寮€濮嬬敵璇疯祫婧愪箣鍓嶏紝鏂板埌鏉ョ殑娲昏穬绾跨▼宸茬粡閲婃斁浜嗗璧勬簮鐨勭嫭鍗犳潈锛屼粠鑰屼笉濡ㄧ琚敜閱掔殑绾跨▼鐢宠璧勬簮銆傝繖绉嶅満鏅篃涓€鏍烽伩鍏嶄簡灏嗘柊鍒版潵鐨勭嚎绋嬫殏鍋?/strong>杩欎箞涓€涓搷浣?/span> 鍥犳锛岄潪鍏钩璋冨害绛栫暐鐨勪紭鐐逛富瑕佹湁涓ょ偣锛?/p>
1.鍚炲悙鐜囦竴鑸潵璇翠細姣斿叕骞宠皟搴︾瓥鐣ラ珮锛屽嵆鍗曚綅鏃堕棿鍐呭畠鍙互涓烘洿澶氱殑鐢宠鑰呰皟閰嶈祫婧?/span>2.闄嶄綆浜嗗彂鐢熶笂涓嬫枃鍒囨崲鐨勬鐜?/span> 闈炲叕骞宠皟搴︾瓥鐣ョ殑缂虹偣涓昏鏈変袱鐐癸細 1.鐢变簬鍏佽鎻掗槦鐜拌薄锛屾瀬绔儏鍐典笅鍙兘瀵艰嚧绛夊緟闃熷垪涓殑绾跨▼姘歌繙涔熸棤娉曡幏寰楀叾鎵€闇€鐨勮祫婧愶紝鍗冲嚭鐜?span class="mq-2123">绾跨▼楗ラタ鐨勬椿鎬ф晠闅滅幇璞?/span>2.姣忎釜璧勬簮鐢宠鑰呬粠寮€濮嬬敵璇疯祫婧愬埌鑾峰緱鐩稿簲璧勬簮鐨勭嫭鍗犳潈鎵€闇€鏃堕棿鐨勫亸宸彲鑳借緝澶э紝鍗虫湁鐨勭嚎绋嬪彲鑳藉緢蹇氨鑳界敵璇峰埌璧勬簮锛岃€屾湁鐨勭嚎绋嬪垯瑕佺粡鍘嗚嫢骞叉鏆傚仠涓庡敜閱掓墠鑳芥垚鍔熺敵璇峰埌璧勬簮 缁间笂鎵€璇夛紝鍏钩璋冨害绛栫暐閫傜敤浜庤祫婧愯鎸佹湁鐨勬椂闂磋緝闀挎垨鑰呯嚎绋嬬敵璇疯祫婧愮殑骞冲潎鏃堕棿闂撮殧杈冮暱鐨勬儏褰紝鎴栬€呰姹傜敵璇疯祫婧愭墍闇€鐨勬椂闂村亸宸緝灏忕殑鎯呭喌銆傛€荤殑鏉ヨ锛屼娇鐢ㄥ叕骞宠皟搴︾瓥鐣ョ殑寮€閿€浼氭瘮浣跨敤闈炲叕骞宠皟搴︾瓥鐣ョ殑寮€閿€瑕佸ぇ锛屽洜姝ゅ湪娌℃湁鐗瑰埆闇€姹傜殑鎯呭喌涓嬶紝搴旇榛樿浣跨敤闈炲叕骞宠皟搴︾瓥鐣?/p>
銆奐ava 澶氱嚎绋嬬紪绋嬪疄鎴樻寚鍗楋紙鏍稿績绡囷級銆?/p>
銆婃繁鍏ョ悊瑙?Java 铏氭嫙鏈恒€?/p>
銆奐ava 骞跺彂缂栫▼鐨勮壓鏈€?/p>
update()
鏂规硶銆傚綋绾跨▼ A 鎵ц update()
鏂规硶鏃讹紝鍦ㄧ嚎绋?A 鎵ц瀹岃鍙?涔嬪悗鑰屾湭鎵ц璇彞2涔嬪墠锛屾鏃剁嚎绋?B 灏变細鐪嬪埌 goodsCount
宸查€掑鍔犱竴鑰?nbsp;clerk
杩樻湭閫掑鍔犱竴鐨勮繖鏍蜂竴涓腑闂存晥鏋溿€傛鏃讹紝鎴戜滑灏辫 update()
鏂规硶浣滀负涓€涓暣浣撲笉鍏峰鍘熷瓙鎬?/p>
class Shop(var goodsCount: Int, var clerk: Int) {
fun update() {
goodsCount++ //璇彞1
clerk++ //璇彞2
}
}
2銆佸彲瑙佹€?/h4>
3銆佹湁搴忔€?/h4>
3銆佷笉瀹夊叏鐨勭嚎绋嬪畨鍏ㄧ被
java.util.Vector銆?/code>Vector 绫诲唴閮ㄧ殑
鍜?nbsp;add()
銆?code class="mq-1723">removeAt()size()
绛夊緢澶氭柟娉曢兘浣跨敤浜?synchronize 杩涜淇グ锛屼繚璇佷簡鍦ㄥ绾跨▼鐜涓嬬殑瀹夊叏鎬э紝浣嗚繖绉嶅悓姝ヤ繚闅滀篃鏃犳硶闃绘寮€鍙戣€呭湪閫昏緫灞傞潰涓婂啓鍑轰笉瀹夊叏鐨勪唬鐮?/p>
add()
鍜?nbsp;removeAt()
涓や釜鏂规硶鐢变簬 Vector 绫诲唴閮ㄧ殑鍚屾澶勭悊锛屼繚闅滀簡涓や釜鏂规硶涓€瀹氭槸涓茶鎵ц鐨勶紝浣嗙敱浜庢柟娉曡皟鐢ㄧ缂哄皯浜嗛澶栫殑鍚屾澶勭悊锛屽鑷磋皟鐢ㄧ鍙兘浼氳鍙栧埌涓€涓繃鏃剁殑 vector.size
鍊硷紝鏈€缁堝鑷寸储寮曡秺鐣屾姏鍑?nbsp;ArrayIndexOutOfBoundsException
/**
* 浣滆€咃細leavesC
* 鏃堕棿锛?/span>2020/8/26 22:52
* 鎻忚堪锛?/span>
* GitHub锛?/span>https://github.com/leavesC
*/
private val vector = Vector<Int>()
private val threadNum = 5
fun main() {
val addThreadList = mutableListOf<Thread>()
for (i in 1..threadNum) {
val thread = object : Thread() {
override fun run() {
for (item in 1..10) {
vector.add(item)
}
}
}
addThreadList.add(thread)
}
val printThreadList = mutableListOf<Thread>()
for (i in 1..threadNum) {
val thread = object : Thread() {
override fun run() {
for (index in 1..vector.size) {
vector.removeAt(i)
}
}
}
printThreadList.add(thread)
}
addThreadList.forEach {
it.start()
}
printThreadList.forEach {
it.start()
}
addThreadList.forEach {
it.join()
}
printThreadList.forEach {
it.join()
}
}
4銆佷笂涓嬫枃鍒囨崲
浜斻€佺嚎绋嬭皟搴?/h3>
Thread.yieid()
鏂规硶鏉ヨ鍑烘椂闂寸墖锛屼絾鏄棤娉曚富鍔ㄦ姠澶烘椂闂寸墖锛屼笖铏界劧 Thread 绫讳篃鎻愪緵浜嗚缃嚎绋嬩紭鍏堢骇鐨勬柟娉曪紝浣嗙嚎绋嬬殑鍏蜂綋鎵ц椤哄簭杩樻槸鍙栧喅浜庡叾杩愯绯荤粺銆傝绛栫暐鐨勪紭鐐规槸涓嶄細鐢变簬涓€涓嚎绋嬬殑闂瀵艰嚧鏁翠釜杩涚▼琚樆濉烇紝涓旀彁楂樹簡骞跺彂鎬с€傜己鐐规槸瀹炵幇杈冧负澶嶆潅锛屼笖浼氬甫鏉ュ绾跨▼瀹夊叏鎬ч棶棰?/span>鍏€佽祫婧愪簤鐢ㄥ拰璧勬簮璋冨害
1銆佽祫婧愪簤鐢?/h4>
2銆佽祫婧愯皟搴?/h4>
涓冦€佸弬鑰冪殑涔︾睄