201871010101-闄堟潵寮熴€婇潰鍚戝璞$▼搴忚璁★紙java锛夈€嬬鍗佸叚鍛ㄥ涔犳€荤粨
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了201871010101-闄堟潵寮熴€婇潰鍚戝璞$▼搴忚璁★紙java锛夈€嬬鍗佸叚鍛ㄥ涔犳€荤粨相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/%e5%a4%84%e7%90%86%e6%9c%ba' title='澶勭悊鏈?>澶勭悊鏈?/a> pack while adt 寰堝 UNC 绗笁閮ㄥ垎 rmi als
瀹為獙鍗佸洓 搴旂敤绋嬪簭褰掓。涓庣嚎绋嬪垵姝?/strong>
瀹為獙鏃堕棿 2019-12-12
绗竴閮ㄥ垎锛氬熀纭€鐭ヨ瘑
1. 绋嬪簭涓庤繘绋嬶細
杩涚▼鏄寚涓€涓叿鏈変竴瀹氱嫭绔嬪姛鑳界殑绋嬪簭鍏充簬鏌愪釜鏁版嵁闆嗗悎鐨勪竴娆¤繍琛屾椿鍔ㄣ€傜數鑴戜腑鏃朵細鏈夊緢澶氬崟鐙繍琛岀殑绋嬪簭锛屾瘡涓▼搴忔湁涓€涓嫭绔嬬殑杩涚▼锛岃€岃繘绋嬩箣闂存槸鐩镐簰鐙珛瀛樺湪鐨勩€傛瘮濡備笅鍥句腑鐨凲Q銆侀叿鐙楁挱鏀惧櫒銆佺數鑴戠瀹剁瓑绛夈€?/span>
杩涚▼鎯宠鎵ц浠诲姟灏遍渶瑕佷緷璧栫嚎绋嬨€傛崲鍙ヨ瘽璇达紝灏辨槸杩涚▼涓殑鏈€灏忔墽琛屽崟浣嶅氨鏄嚎绋嬶紝骞朵笖涓€涓繘绋嬩腑鑷冲皯鏈変竴涓嚎绋嬨€?/span>
杩涚▼涓庣▼搴忕殑鍖哄埆锛?/span>
1锛夈€?nbsp; 绋嬪簭鏄寚浠ょ殑鏈夊簭闆嗗悎锛屾槸涓€涓潤鎬佹蹇碉紝鍏舵湰浜嬫病鏈変换浣曡繍琛岀殑鍚箟銆傝€岃繘绋嬫槸绋嬪簭鍦ㄥ鐞嗘満涓婄殑涓€娆℃墽琛岃繃绋嬶紝鏄竴鍔ㄦ€佺殑姒傚康銆?/span>
2锛夈€?nbsp; 绋嬪簭鍙互浣滀负涓€绉嶈蒋浠惰祫鏂欓暱鏈熶繚瀛橈紝鑰岃繘绋嬪垯鏄湁涓€瀹氱敓鍛藉懆鏈熺殑锛屽畠鑳藉鍔ㄦ€佺殑浜х敓鍜屾秷浜°€?/span>
3锛夈€?nbsp; 杩涚▼鏄竴涓兘鐙珛杩愯鐨勫崟浣嶏紝鑳戒笌鍏朵粬杩涚▼骞惰娲诲姩銆?/span>
4锛夈€?nbsp; 杩涚▼鏄珵浜夎绠楁満绯荤粺鏈夐檺璧勬簮鐨勫熀鏈崟浣嶏紝涔熸槸杩涜澶勭悊鏈鸿皟搴︾殑鍩烘湰鍗曚綅銆傜▼搴忔棤姝よ姒傚康銆?/span>
5锛夈€?nbsp; 杩涚▼涓庣▼搴忎箣闂存棤涓€涓€瀵瑰簲鍏崇郴銆備笉鍚岀殑杩涚▼鍙互鍖呭惈鍚屼竴绋嬪簭锛屽悓涓€绋嬪簭鍦ㄦ墽琛屼腑涔熷彲浠ヤ骇鐢熷涓繘绋嬨€?/span>
6锛夈€?nbsp; 绋嬪簭鏄褰曞湪浠嬭川涓婃寚浠ょ殑鏈夊簭闆嗗悎锛岃€岃繘绋嬪垯鐢辩▼搴忋€佹暟鎹拰杩涚▼鎺у埗鍧?閮ㄥ垎缁勬垚
2. 澶氱嚎绋?/span>
鐢ㄥ绾跨▼鍙湁涓€涓洰鐨勶紝閭e氨鏄洿濂界殑鍒╃敤cpu鐨勮祫婧愶紝鍥犱负鎵€鏈夌殑澶氱嚎绋嬩唬鐮侀兘鍙互鐢ㄥ崟绾跨▼鏉ュ疄鐜般€傛彁鍒板绾跨▼杩欓噷瑕佽涓や釜姒傚康锛屽氨鏄覆琛屽拰骞惰锛屾悶娓呮杩欎釜锛屾垜浠墠鑳芥洿濂藉湴鐞嗚В澶氱嚎绋嬨€?/span>
鎵€璋撲覆琛岋紝鍏跺疄鏄浉瀵逛簬鍗曟潯绾跨▼鏉ユ墽琛屽涓换鍔℃潵璇寸殑锛屾垜浠氨鎷夸笅杞芥枃浠舵潵涓句釜渚嬪瓙锛氬綋鎴戜滑涓嬭浇澶氫釜鏂囦欢鏃讹紝鍦ㄤ覆琛屼腑瀹冩槸鎸夌収涓€瀹氱殑椤哄簭鍘昏繘琛屼笅杞界殑锛屼篃灏辨槸璇达紝蹇呴』绛変笅杞藉畬A涔嬪悗鎵嶈兘寮€濮嬩笅杞紹锛屽畠浠湪鏃堕棿涓婃槸涓嶅彲鑳藉彂鐢熼噸鍙犵殑銆?/span>
璇磋繖涓瘽鍏跺疄鍙湁涓€鍗婂锛屽洜涓哄弽搴?ldquo;澶氳鑹?rdquo;鐨勭▼搴忎唬鐮侊紝鏈€璧风爜姣忎釜瑙掕壊瑕佺粰浠栦竴涓嚎绋嬪惂锛屽惁鍒欒繛瀹為檯鍦烘櫙閮芥棤娉曟ā鎷燂紝褰撶劧涔熸病娉曡鑳界敤鍗曠嚎绋嬫潵瀹炵幇锛氭瘮濡傛渶甯歌鐨?ldquo;鐢熶骇鑰咃紝娑堣垂鑰呮ā鍨?rdquo;銆?/span>
寰堝浜洪兘瀵瑰叾涓殑涓€浜涙蹇典笉澶熸槑纭紝濡傚悓姝ャ€佸苟鍙戠瓑绛夛紝璁╂垜浠厛寤虹珛涓€涓暟鎹瓧鍏革紝浠ュ厤浜х敓璇細銆?/span>
- 澶氱嚎绋嬶細鎸囩殑鏄繖涓▼搴忥紙涓€涓繘绋嬶級杩愯鏃朵骇鐢熶簡涓嶆涓€涓嚎绋?/span>
- 骞惰涓庡苟鍙戯細
- 骞惰锛氬涓猚pu瀹炰緥鎴栬€呭鍙版満鍣ㄥ悓鏃舵墽琛屼竴娈靛鐞嗛€昏緫锛屾槸鐪熸鐨勫悓鏃躲€?/span>
- 骞跺彂锛氶€氳繃cpu璋冨害绠楁硶锛岃鐢ㄦ埛鐪嬩笂鍘诲悓鏃舵墽琛岋紝瀹為檯涓婁粠cpu鎿嶄綔灞傞潰涓嶆槸鐪熸鐨勫悓鏃躲€傚苟鍙戝線寰€鍦ㄥ満鏅腑鏈夊叕鐢ㄧ殑璧勬簮锛岄偅涔堥拡瀵硅繖涓叕鐢ㄧ殑璧勬簮寰€寰€浜х敓鐡堕锛屾垜浠細鐢═PS鎴栬€匭PS鏉ュ弽搴旇繖涓郴缁熺殑澶勭悊鑳藉姏銆?/span>
- 绾跨▼瀹夊叏锛氱粡甯哥敤鏉ユ弿缁樹竴娈典唬鐮併€傛寚鍦ㄥ苟鍙戠殑鎯呭喌涔嬩笅锛岃浠g爜缁忚繃澶氱嚎绋嬩娇鐢紝绾跨▼鐨勮皟搴﹂『搴忎笉褰卞搷浠讳綍缁撴灉銆傝繖涓椂鍊欎娇鐢ㄥ绾跨▼锛屾垜浠彧闇€瑕佸叧娉ㄧ郴缁熺殑鍐呭瓨锛宑pu鏄笉鏄鐢ㄥ嵆鍙€傚弽杩囨潵锛岀嚎绋嬩笉瀹夊叏灏辨剰鍛崇潃绾跨▼鐨勮皟搴﹂『搴忎細褰卞搷鏈€缁堢粨鏋滐紝濡備笉鍔犱簨鍔$殑杞处浠g爜锛?/span>
void transferMoney(User from, User to, float amount){
to.setMoney(to.getBalance() + amount);
from.setMoney(from.getBalance() - amount);
}
- 鍚屾锛欽ava涓殑鍚屾鎸囩殑鏄€氳繃浜轰负鐨勬帶鍒跺拰璋冨害锛屼繚璇佸叡浜祫婧愮殑澶氱嚎绋嬭闂垚涓虹嚎绋嬪畨鍏紝鏉ヤ繚璇佺粨鏋滅殑鍑嗙‘銆傚涓婇潰鐨勪唬鐮佺畝鍗曞姞鍏?code>@synchronized鍏抽敭瀛椼€傚湪淇濊瘉缁撴灉鍑嗙‘鐨勫悓鏃讹紝鎻愰珮鎬ц兘锛屾墠鏄紭绉€鐨勭▼搴忋€傜嚎绋嬪畨鍏ㄧ殑浼樺厛绾ч珮浜庢€ц兘銆?/span>
骞惰锛氫笅杞藉涓枃浠讹紝寮€鍚鏉$嚎绋嬶紝澶氫釜鏂囦欢鍚屾椂杩涜涓嬭浇锛岃繖閲屾槸涓ユ牸鎰忎箟涓婄殑锛屽湪鍚屼竴鏃跺埢鍙戠敓鐨勶紝骞惰鍦ㄦ椂闂翠笂鏄噸鍙犵殑銆?/span>
浜嗚В浜嗚繖涓や釜姒傚康涔嬪悗锛屾垜浠啀鏉ヨ璇翠粈涔堟槸澶氱嚎绋嬨€備妇涓緥瀛愶紝鎴戜滑鎵撳紑鑵捐绠″锛岃吘璁瀹舵湰韬氨鏄竴涓▼搴忥紝涔熷氨鏄瀹冨氨鏄竴涓繘绋嬶紝瀹冮噷闈㈡湁寰堝鐨勫姛鑳斤紝鎴戜滑鍙互鐪嬩笅鍥撅紝鑳芥煡鏉€鐥呮瘨銆佹竻鐞嗗瀮鍦俱€佺數鑴戝姞閫熺瓑浼楀鍔熻兘銆?/span>
鎸夌収鍗曠嚎绋嬫潵璇达紝鏃犺浣犳兂瑕佹竻鐞嗗瀮鍦俱€佽繕鏄鐥呮瘨鏌ユ潃锛岄偅涔堜綘蹇呴』鍏堝仛瀹屽叾涓殑涓€浠朵簨锛屾墠鑳藉仛涓嬩竴浠朵簨锛岃繖閲岄潰鏄湁涓€涓墽琛岄『搴忕殑銆?/span>
濡傛灉鏄绾跨▼鐨勮瘽锛屾垜浠叾瀹炲湪娓呯悊鍨冨溇鐨勬椂鍊欙紝杩樺彲浠ヨ繘琛屾煡鏉€鐥呮瘨銆佺數鑴戝姞閫熺瓑绛夊叾浠栫殑鎿嶄綔锛岃繖涓槸涓ユ牸鎰忎箟涓婄殑鍚屼竴鏃跺埢鍙戠敓鐨勶紝娌℃湁鎵ц涓婄殑鍏堝悗椤哄簭銆?/span>
浠ヤ笂灏辨槸锛屼竴涓繘绋嬭繍琛屾椂浜х敓浜嗗涓嚎绋嬨€?/span>
鍦ㄤ簡瑙e畬杩欎釜闂鍚庯紝鎴戜滑鍙堥渶瑕佸幓浜嗚В涓€涓娇鐢ㄥ绾跨▼涓嶅緱涓嶈€冭檻鐨勯棶棰?mdash;—绾跨▼瀹夊叏銆?/span>
3. 绾跨▼瀹夊叏
褰撳涓嚎绋嬭闂煇涓柟娉曟椂锛屼笉绠′綘閫氳繃鎬庢牱鐨勮皟鐢ㄦ柟寮忋€佹垨鑰呰杩欎簺绾跨▼濡備綍浜ゆ浛鍦版墽琛岋紝鎴戜滑鍦ㄤ富绋嬪簭涓笉闇€瑕佸幓鍋氫换浣曠殑鍚屾锛岃繖涓被鐨勭粨鏋滆涓洪兘鏄垜浠鎯崇殑姝g‘琛屼负锛岄偅涔堟垜浠氨鍙互璇磋繖涓被鏄嚎绋嬪畨鍏ㄧ殑銆?/span>
鏃㈢劧鏄嚎绋嬪畨鍏ㄩ棶棰橈紝閭d箞姣棤鐤戦棶锛屾墍鏈夌殑闅愭偅閮芥槸鍦ㄥ涓嚎绋嬭闂殑鎯呭喌涓嬩骇鐢熺殑锛屼篃灏辨槸鎴戜滑瑕佺‘淇濆湪澶氭潯绾跨▼璁块棶鐨勬椂鍊欙紝鎴戜滑鐨勭▼搴忚繕鑳芥寜鐓ф垜浠鏈熺殑琛屼负鍘绘墽琛岋紝鎴戜滑鐪嬩竴涓嬩笅闈㈢殑浠g爜銆?/span>
Integer count = 0;
public void getCount() {
count ++;
System.out.println(count);
}
鏈€甯歌鐨勪竴绉嶏紝灏辨槸鎴戜滑A绾跨▼鍦ㄨ繘鍏ユ柟娉曞悗锛屾嬁鍒颁簡count鐨勫€硷紝鍒氭妸杩欎釜鍊艰鍙栧嚭鏉ワ紝杩樻病鏈夋敼鍙榗ount鐨勫€肩殑鏃跺€欙紝缁撴灉绾跨▼B涔熻繘鏉ョ殑锛岄偅涔堝鑷寸嚎绋婣鍜岀嚎绋婤鎷垮埌鐨刢ount鍊兼槸涓€鏍风殑銆?/span>
閭d箞鐢辨鎴戜滑鍙互浜嗚В鍒帮紝杩欑‘瀹炰笉鏄竴涓嚎绋嬪畨鍏ㄧ殑绫伙紝鍥犱负浠栦滑閮介渶瑕佹搷浣滆繖涓叡浜殑鍙橀噺銆傚叾瀹炶瀵圭嚎绋嬪畨鍏ㄩ棶棰樼粰鍑轰竴涓槑纭殑瀹氫箟锛岃繕鏄洰澶嶆潅鐨勶紝鎴戜滑鏍规嵁鎴戜滑杩欎釜绋嬪簭鏉ユ€荤粨涓嬩粈涔堟槸绾跨▼瀹夊叏銆?/span>
褰撳涓嚎绋嬭闂煇涓柟娉曟椂锛屼笉绠′綘閫氳繃鎬庢牱鐨勮皟鐢ㄦ柟寮忋€佹垨鑰呰杩欎簺绾跨▼濡備綍浜ゆ浛鍦版墽琛岋紝鎴戜滑鍦ㄤ富绋嬪簭涓笉闇€瑕佸幓鍋氫换浣曠殑鍚屾锛岃繖涓被鐨勭粨鏋滆涓洪兘鏄垜浠鎯崇殑姝g‘琛屼负锛岄偅涔堟垜浠氨鍙互璇磋繖涓被鏄嚎绋嬪畨鍏ㄧ殑銆?/span>
4. 绾跨▼鏈夊嚑绉嶇姸鎬侊細
1锛夈€佹柊寤虹姸鎬?new)
2锛夈€佸氨缁姸鎬?Runnable)
3锛夈€佽繍琛岀姸鎬?Running)
4锛夈€侀樆濉炵姸鎬?Blocked)
5锛夈€佹浜$姸鎬?Dead)
5. 绾跨▼鏈変粈涔堢敤澶?/span>
java绋嬪簭涓祦浼犱竴鍙ヨ瘽锛屼笉浼氫娇鐢ㄧ嚎绋嬪氨鍒窡鍒汉璇磋嚜宸卞杩噅ava銆傜洰鍓嶇粷澶ч儴鍒嗗簲鐢ㄧ▼搴忛兘浼氭秹鍙婂埌澶氬苟鍙戠殑闂銆傚彧瑕佸簲鐢ㄧ▼搴忔秹鍙婂埌骞跺彂锛屽氨绂讳笉寮€澶氱嚎绋嬬紪绋嬨€?/span>
6. 绾跨▼--濡備綍浣跨敤
鍦╦ava涓竴涓被瑕佸綋浣滅嚎绋嬫潵浣跨敤鏈変袱绉嶆柟娉曘€?/span>
1)銆佺户鎵縏hread绫伙紝骞堕噸鍐檙un鍑芥暟
2)銆佸疄鐜癛unnable鎺ュ彛锛屽苟閲嶅啓run鍑芥暟
鍥犱负java鏄崟缁ф壙鐨勶紝鍦ㄦ煇浜涙儏鍐典笅涓€涓被鍙兘宸茬粡缁ф壙浜嗘煇涓埗绫伙紝杩欐椂鍦ㄧ敤缁ф壙Thread绫绘柟娉曟潵鍒涘缓绾跨▼鏄剧劧涓嶅彲鑳絡ava璁捐鑰呬滑鎻愪緵浜嗗彟澶栦竴涓柟寮忓垱寤虹嚎绋嬶紝灏辨槸閫氳繃瀹炵幇Runnable鎺ュ彛鏉ュ垱寤虹嚎绋嬨€?/span>
7. 涓柇绾跨▼
Terminated (琚粓姝? 绾跨▼琚粓姝㈢殑鍘熷洜鏈変簩锛?nbsp;
1锛変竴鏄痳un()鏂规硶涓渶鍚庝竴涓鍙ユ墽琛屽畬姣曡€岃嚜 鐒舵浜°€?nbsp;
2锛変簩鏄洜涓轰竴涓病鏈夋崟鑾风殑寮傚父缁堟浜唕un鏂规硶 鑰屾剰澶栨浜°€?nbsp;
鍙互璋冪敤绾跨▼鐨剆top 鏂?娉?鏉€ 姝?涓€ 涓?绾?绋?锛坱hread.stop();)锛屼絾鏄紝stop鏂规硶宸茶繃鏃讹紝 涓嶈鍦ㄨ嚜宸辩殑浠g爜涓皟鐢ㄥ畠銆?/span>
鍏朵粬鍒ゆ柇鍜屽奖鍝嶇嚎绋嬬姸鎬佺殑鏂规硶锛?/span>
1锛塲oin()锛氱瓑寰呮寚瀹氱嚎绋嬬殑缁堟銆?nbsp;
2锛塲oin(long millis)锛氱粡杩囨寚瀹氭椂闂寸瓑寰呯粓姝㈡寚瀹?鐨勭嚎绋嬨€?nbsp;
3锛塱sAlive()锛氭祴璇曞綋鍓嶇嚎绋嬫槸鍚﹀湪娲诲姩銆?nbsp;
4锛墆ield()锛氳褰撳墠绾跨▼鐢?ldquo;杩愯鐘舵€?rdquo;杩涘叆鍒?ldquo;灏?缁姸鎬?rdquo;锛屼粠鑰岃鍏跺畠鍏锋湁鐩稿悓浼樺厛绾х殑绛夊緟绾跨▼ 鑾峰彇鎵ц鏉冦€?/span>
8. 绾跨▼鐘舵€?/span>
Java涓嚎绋嬬殑鐘舵€佸垎涓?绉嶃€?/span>
1). 鍒濆(NEW)锛氭柊鍒涘缓浜嗕竴涓嚎绋嬪璞★紝浣嗚繕娌℃湁璋冪敤start()鏂规硶銆?/span>
2). 杩愯(RUNNABLE)锛欽ava绾跨▼涓皢灏辩华锛坮eady锛夊拰杩愯涓紙running锛変袱绉嶇姸鎬佺缁熺殑绉颁负“杩愯”銆?/span>
绾跨▼瀵硅薄鍒涘缓鍚庯紝鍏朵粬绾跨▼(姣斿main绾跨▼锛夎皟鐢ㄤ簡璇ュ璞$殑start()鏂规硶銆傝鐘舵€佺殑绾跨▼浣嶄簬鍙繍琛岀嚎绋嬫睜涓紝绛夊緟琚嚎绋嬭皟搴﹂€変腑锛岃幏鍙朇PU鐨勪娇鐢ㄦ潈锛屾鏃跺浜庡氨缁姸鎬侊紙ready锛夈€傚氨缁姸鎬佺殑绾跨▼鍦ㄨ幏寰桟PU鏃堕棿鐗囧悗鍙樹负杩愯涓姸鎬侊紙running锛夈€?/span>
3). 闃诲(BLOCKED)锛氳〃绀虹嚎绋嬮樆濉炰簬閿併€?/span>
4). 绛夊緟(WAITING)锛氳繘鍏ヨ鐘舵€佺殑绾跨▼闇€瑕佺瓑寰呭叾浠栫嚎绋嬪仛鍑轰竴浜涚壒瀹氬姩浣滐紙閫氱煡鎴栦腑鏂級銆?/span>
5). 瓒呮椂绛夊緟(TIMED_WAITING)锛氳鐘舵€佷笉鍚屼簬WAITING锛屽畠鍙互鍦ㄦ寚瀹氱殑鏃堕棿鍚庤嚜琛岃繑鍥炪€?/span>
9. 琚樆濉炵嚎绋嬪拰绛夊緟绾跨▼
褰撶嚎绋嬪浜庤闃诲鎴栫瓑寰呯姸鎬佹椂锛屽畠鏆傛椂涓嶆椿鍔ㄣ€傚畠涓嶈繍琛屼换浣曚唬鐮佷笖娑堣€楁渶灏戠殑璧勬簮銆傜洿鍒扮嚎绋嬭皟搴﹀櫒閲嶆柊婵€娲诲畠銆?缁嗚妭鍙栧喅浜庡畠鏄€庢牱杈惧埌闈炴椿鍔ㄧ姸鎬佺殑銆?/span>
銆€銆€褰撲竴涓嚎绋嬭瘯鍥捐幏鍙栦竴涓唴閮ㄧ殑瀵硅薄閿侊紙鑰屼笉鏄?javiutiUoncurrent 搴撲腑鐨勯攣锛? 鑰岃閿佽鍏朵粬绾跨▼鎸佹湁锛?鍒欒绾跨▼杩涗汉闃诲鐘舵€?銆傚綋鎵€鏈夊叾浠栫嚎绋嬮噴鏀捐閿侊紝骞朵笖绾跨▼璋冨害鍣ㄥ厑璁告湰绾跨▼鎸佹湁瀹冪殑鏃跺€欙紝璇ョ嚎绋嬪皢鍙樻垚闈為樆濉炵姸鎬?/span>
銆€銆€褰撶嚎绋嬬瓑寰呭彟涓€涓嚎绋嬮€氱煡璋冨害鍣ㄤ竴涓潯浠舵椂锛?瀹冭嚜宸辫繘鍏ョ瓑寰呯姸鎬佹湁鍑犱釜鏂规硶鏈変竴涓秴鏃跺弬鏁般€傝皟鐢ㄥ畠浠鑷寸嚎绋嬭繘浜鸿鏃剁瓑寰咃紙timed waiting) 鐘舵€併€傝繖涓€鐘舵€佸皢涓€鐩翠繚鎸佸埌瓒呮椂鏈熸弧鎴栬€呮帴鏀跺埌閫傚綋鐨勯€氱煡
10.
琚粓姝㈢殑绾跨▼
銆€銆€鍥犱负涓€涓病鏈夋崟鑾风殑寮傚父缁堟浜?run 鏂规硶鑰屾剰澶栨浜?/span>
銆怉PI銆慾ava.iang.Thread 1.0锛?/span>
绛夊緟缁堟鎸囧畾鐨勭嚎绋嬨€?/span>
銆€銆€void join( long millis )
绛夊緟鎸囧畾鐨勭嚎绋嬫浜℃垨鑰呯粡杩囨寚瀹氱殑姣鏁般€?/span>
銆€銆€Thread.State getState () 5 . 0
寰楀埌杩欎竴绾跨▼鐨勭姸鎬侊紱NEW銆丷UNNABLE BLOCKED銆?WAITING 銆乀IMED_WAITNG鎴?TERMINATED 涔嬩竴銆?/span>
鍋滄璇ョ嚎绋嬨€傝繖涓€鏂规硶宸茶繃鏃?/span>
銆€銆€void suspend()
鏆傚仠杩欎竴绾跨▼鐨勬墽琛屻€傝繖涓€鏂规硶宸茶繃鏃躲€?/span>
銆€銆€void resume()
鎭㈠绾跨▼銆傝繖涓€鏂规硶浠呬粎鍦ㄨ皟鐢?suspend() 涔嬪悗璋冪敤銆?/span>
绗簩閮ㄥ垎锛氬疄楠?/strong>
1銆佸疄楠岀洰鐨勪笌瑕佹眰
(1) 鎺屾彙Java搴旂敤绋嬪簭鐨勬墦鍖呮搷浣滐紱
(2) 鎺屾彙绾跨▼姒傚康锛?/span>
(3) 鎺屾彙绾跨▼鍒涘缓鐨勪袱绉嶆妧鏈€?/span>
2銆佸疄楠屽唴瀹瑰拰姝ラ
瀹為獙1锛?/strong> 瀵煎叆绗?3绔犵ず渚嬬▼搴忥紝娴嬭瘯绋嬪簭骞惰繘琛屼唬鐮佹敞閲娿€?/span>
娴嬭瘯绋嬪簭1
l 鍦?/strong>elipse IDE涓皟璇曡繍琛屾暀鏉?/strong>585椤电▼搴?/strong>13-1锛岀粨鍚堢▼搴忚繍琛岀粨鏋滅悊瑙g▼搴忥紱
l 灏嗘墍鐢熸垚鐨凧AR鏂囦欢绉诲埌鍙﹀涓€涓笉鍚岀殑鐩綍涓紝鍐嶈繍琛岃褰掓。鏂囦欢锛屼互渚跨‘璁ょ▼搴忔槸浠嶫AR鏂囦欢涓紝鑰屼笉鏄粠褰撳墠鐩綍涓鍙栫殑璧勬簮銆?/strong>
l 鎺屾彙鍒涘缓JAR鏂囦欢鐨勬柟娉曪紱
浠g爜濡備笅锛?/span>
package resource;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
/**
* @version 1.41 2015-06-12
* @author Cay Horstmann
*/
public class ResourceTest
{
public static void main(String[] args)
{
EventQueue.invokeLater(() -> {
JFrame frame = new ResourceTestFrame();
frame.setTitle("ResourceTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
});
}
}
/**
* A frame that loads image and text resources.
*/
class ResourceTestFrame extends JFrame
{
private static final int DEFAULT_WIDTH = 300;
private static final int DEFAULT_HEIGHT = 300;
public ResourceTestFrame()
{
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
URL aboutURL = getClass().getResource("about.gif");//鎵惧埌鎸囧畾浣嶇疆鐨勫浘鍍忔枃浠讹紝杩斿洖涓€涓彲浠ュ姞杞借祫婧愮殑URL
Image img = new ImageIcon(aboutURL).getImage();//灏嗗姞杞界殑about.gif鍥惧儚璁剧疆涓哄浘鏍?/span>
setIconImage(img);
JTextArea textArea = new JTextArea();
InputStream stream = getClass().getResourceAsStream("about.txt");//璇诲彇about.txt鏂囨湰鏂囦欢鍐呭
try (Scanner in = new Scanner(stream, "UTF-8"))//灏嗚鍙栧埌鐨刟bout.txt鏂囨湰鏂囦欢閲屽唴瀹规樉绀哄埌鏂囨湰鍖?/span>
{
while (in.hasNext())//璇诲彇鏂囨湰鏂囦欢
textArea.append(in.nextLine() + "
");
}
add(textArea);
}
}
浠ヤ笅鏄綊妗o細
娴嬭瘯绋嬪簭2锛?/span>
l 鍦╡lipse IDE涓皟璇曡繍琛孴hreadTest锛岀粨鍚堢▼搴忚繍琛岀粨鏋滅悊瑙g▼搴忥紱
l 鎺屾彙绾跨▼姒傚康锛?/span>
l 鎺屾彙鐢═hread鐨勬墿灞曠被瀹炵幇绾跨▼鐨勬柟娉曪紱
l 鍒╃敤Runnable鎺ュ彛鏀归€犵▼搴忥紝鎺屾彙鐢≧unnable鎺ュ彛鍒涘缓绾跨▼鐨勬柟娉曘€?/span>
class Lefthand extends Thread { public void run() { for(int i=0;i<=5;i++) { System.out.println("You are Students!"); try{ sleep(500); } catch(InterruptedException e) { System.out.println("Lefthand error.");} } } } class Righthand extends Thread { public void run() { for(int i=0;i<=5;i++) { System.out.println("I am a Teacher!"); try{ sleep(300); } catch(InterruptedException e) { System.out.println("Righthand error.");} } } } public class ThreadTest { static Lefthand left; static Righthand right; public static void main(String[] args) { left=new Lefthand(); right=new Righthand(); left.start(); right.start(); } } |
瀵煎叆浠g爜濡備笅锛?/span>
class Lefthand extends Thread {
public void run()
{
for(int i=0;i<=5;i++)
{ System.out.println("You are Students!");
try{ sleep(500); }
catch(InterruptedException e)
{ System.out.println("Lefthand error.");}
}
}
}
class Righthand extends Thread {
public void run()
{
for(int i=0;i<=5;i++)
{ System.out.println("I am a Teacher!");
try{ sleep(300); }
catch(InterruptedException e)
{ System.out.println("Righthand error.");}
}
}
}
public class ThreadTest
{
static Lefthand left;
static Righthand right;
public static void main(String[] args)
{ left=new Lefthand();
right=new Righthand();
left.start();
right.start();
}
}
杩愯缁撴灉锛?/span>
鍦╦ava涓竴涓被瑕佸綋浣滅嚎绋嬫潵浣跨敤鏈変袱绉嶆柟娉曘€?/span>
1銆佺户鎵縏hread绫伙紝骞堕噸鍐檙un鍑芥暟
2銆佸疄鐜癛unnable鎺ュ彛锛屽苟閲嶅啓run鍑芥暟
鍥犱负java鏄崟缁ф壙鐨勶紝鍦ㄦ煇浜涙儏鍐典笅涓€涓被鍙兘宸茬粡缁ф壙浜嗘煇涓埗绫伙紝杩欐椂鍦ㄧ敤缁ф壙Thread绫绘柟娉曟潵鍒涘缓绾跨▼鏄剧劧涓嶅彲鑳絡ava璁捐鑰呬滑鎻愪緵浜嗗彟澶栦竴涓柟寮忓垱寤虹嚎绋嬶紝灏辨槸閫氳繃瀹炵幇Runnable鎺ュ彛鏉ュ垱寤虹嚎绋嬨€?/span>
娴嬭瘯绋嬪簭3锛?/span>
l 鍦‥lipse鐜涓嬭皟璇曟暀鏉?25椤电▼搴?4-1銆?4-2 銆?4-3锛岀粨鍚堢▼搴忚繍琛岀粨鏋滅悊瑙g▼搴忥紱
l 瀵规瘮涓や釜绋嬪簭锛岀悊瑙g嚎绋嬬殑姒傚康鍜岀敤閫旓紱
l 鎺屾彙绾跨▼鍒涘缓鐨勪袱绉嶆妧鏈€?/span>
1 package bounce;
2
3 import java.awt.*;
4 import java.util.*;
5 import javax.swing.*;
6
7 public class BallComponent extends JPanel
8 {
9 private static final int DEFAULT_WIDTH = 450;
10 private static final int DEFAULT_HEIGHT = 350;
11
12 private java.util.List<Ball> balls = new ArrayList<>();
13
14 /** 鍚戠粍浠朵腑娣诲姞涓€涓悆銆?/span>*/
15 public void add(Ball b)
16 {
17 balls.add(b);
18 }
19
20 public void paintComponent(Graphics g)
21 {
22 super.paintComponent(g); // 鎿﹂櫎鑳屾櫙
23 Graphics2D g2 = (Graphics2D) g;
24 for (Ball b : balls)
25 {
26 g2.fill(b.getShape());
27 }
28 }
29
30 public Dimension getPreferredSize() { return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT); }
31 }
1 package bounce;
2
3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
6
7 public class Bounce
8 {
9 public static void main(String[] args)
10 {
11 EventQueue.invokeLater(() -> {
12 JFrame frame = new BounceFrame();
13 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
14 frame.setVisible(true);
15 });
16 }
17 }
18 /** 甯︽湁鐞冪粍浠跺拰鎸夐挳鐨勬鏋躲€?/span>*/
19 class BounceFrame extends JFrame
20 {
21 private BallComponent comp;
22 public static final int STEPS = 1000;
23 public static final int DELAY = 3;
24
25 /**
26 * 鐢ㄦ樉绀哄脊璺崇悆鍜岀殑缁勪欢鏋勯€犳鏋?27 *鍚姩鍜屽叧闂寜閽?28 **/
29 public BounceFrame()
30 {
31 setTitle("Bounce");
32 comp = new BallComponent();
33 add(comp, BorderLayout.CENTER);
34 JPanel buttonPanel = new JPanel();
35 addButton(buttonPanel, "Start", event -> addBall());
36 addButton(buttonPanel, "Close", event -> System.exit(0));
37 add(buttonPanel, BorderLayout.SOUTH);
38 pack();
39 }
40
41 /**
42 * 灏嗘寜閽坊鍔犲埌瀹瑰櫒涓?43 *鎸夐挳鏍囬
44 * 鎸夐挳鐨勫姩浣滅洃鍚櫒
45 */
46 public void addButton(Container c, String title, ActionListener listener)
47 {
48 JButton button = new JButton(title);
49 c.add(button);
50 button.addActionListener(listener);
51 }
52
53 /**
54 *鍚戦潰鏉夸腑娣诲姞涓€涓脊璺崇悆锛屼娇鍏跺脊璺?000娆°€?55 */
56 public void addBall()
57 {
58 try
59 {
60 Ball ball = new Ball();
61 comp.add(ball);
62
63 for (int i = 1; i <= STEPS; i++)
64 {
65 ball.move(comp.getBounds());
66 comp.paint(comp.getGraphics());
67 Thread.sleep(DELAY);
68 }
69 }
70 catch (InterruptedException e)
71 {
72 }
73 }
74 }
1 package bounce;
2
3 import java.awt.geom.*;
4
5 /**
6 * 浠庨暱鏂瑰舰杈圭紭涓婄Щ鍔ㄥ拰寮硅烦鐨勭悆
7 */
8 public class Ball
9 {
10 private static final int XSIZE = 15;
11 private static final int YSIZE = 15;
12 private double x = 0;
13 private double y = 0;
14 private double dx = 1;
15 private double dy = 1;
16
17 /**
18 * 灏嗙悆绉诲姩鍒颁笅涓€涓綅缃紝濡傛灉鐞冪鍒板叾涓竴鏉¤竟锛屽垯鍙嶅悜绉诲姩
19 */
20 public void move(Rectangle2D bounds)
21 {
22 x += dx;
23 y += dy;
24 if (x < bounds.getMinX())
25 {
26 x = bounds.getMinX();
27 dx = -dx;
28 }
29 if (x + XSIZE >= bounds.getMaxX())
30 {
31 x = bounds.getMaxX() - XSIZE;
32 dx = -dx;
33 }
34 if (y < bounds.getMinY())
35 {
36 y = bounds.getMinY();
37 dy = -dy;
38 }
39 if (y + YSIZE >= bounds.getMaxY())
40 {
41 y = bounds.getMaxY() - YSIZE;
42 dy = -dy;
43 }
44 }
45
46 /**
47 * 鑾峰彇鐞冨湪褰撳墠浣嶇疆鐨勫舰鐘躲€?48 */
49 public Ellipse2D getShape()
50 {
51 return new Ellipse2D.Double(x, y, XSIZE, YSIZE);
52 }
53 }
杩愯缁撴灉锛?/span>
l 鍦‥lipse鐜涓嬭皟璇曟暀鏉?31椤电▼搴?4-4锛岀粨鍚堢▼搴忚繍琛岀粨鏋滅悊瑙g▼搴忥紱
package bounceThread;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class BounceThread
{
public static void main(String[] args)
{
EventQueue.invokeLater(() -> {
JFrame frame = new BounceFrame();
frame.setTitle("BounceThread");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
});
}
}
class BounceFrame extends JFrame
{
private BallComponent comp;
public static final int STEPS = 1000;
public static final int DELAY = 5;
public BounceFrame()
{
comp = new BallComponent();
add(comp, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
addButton(buttonPanel, "Start", event -> addBall());
addButton(buttonPanel, "Close", event -> System.exit(0));
add(buttonPanel, BorderLayout.SOUTH);
pack();
}
public void addButton(Container c, String title, ActionListener listener)
{
JButton button = new JButton(title);
c.add(button);
button.addActionListener(listener);
}
public void addBall() /** 鍦ㄧ敾甯冧笂娣诲姞涓€涓脊璺崇悆锛屽苟鍚姩涓€涓嚎绋嬩娇鍏惰繍琛?/span>*/
{
Ball ball = new Ball();
comp.add(ball);
Runnable r = () -> {
try
{
for (int i = 1; i <= STEPS; i++)
{
ball.move(comp.getBounds());
comp.repaint();
Thread.sleep(DELAY);
}
}
catch (InterruptedException e)
{
}
};
Thread t = new Thread(r);
t.start();
}
}
杩愯缁撴灉锛?/span>
涓や釜瀹為獙瀵规瘮锛氱涓€涓▼搴忛潪绾跨▼鐨勶紝鎵ц涓€娆$▼搴忚绛夋墽琛屽畬鎵嶈兘鎵ц涓嬩竴娆★紱绗簩涓▼搴忔槸绾跨▼鐨勶紝鎵ц绗竴娆$▼搴忎笉鐢ㄧ瓑鎵ц瀹屽彲浠ユ帴鐫€鎵ц绗簩娆℃搷浣溿€?/span>
绗笁閮ㄥ垎锛氬疄楠屾€荤粨
杩欒妭璇剧粨鏉燂紝涓€鏁村鏈熺殑璇剧▼灏卞凡缁忕粨鏉熶簡銆傚湪鍋氬悗涓€绔犺妭涓紝涓昏瀛︿範鍐呭鏄叧浜庡绾跨▼鐨勪簡瑙o紝鍏充簬浠€涔堟槸绾跨▼锛岀嚎绋嬪畨鍏ㄧ殑闆嗗悎锛屼腑鏂嚎绋嬶紝绾跨▼鐨勭姸鎬佺瓑鍑犱釜鏂归潰杩涜浜嗗涔犮€?/span>鑰岄€氳繃鏈瀹為獙鎰熻鑷繁涔熸湁浜嗕竴瀹氱殑杩涙銆侸ava瀛︿簡涓€瀛︽湡锛岃嚜宸变篃鏀惰幏浜嗗緢澶氾紝甯屾湜鍦ㄤ互鍚庯紝鍙互灏嗗鍒扮殑鐭ヨ瘑杩愮敤鍒板疄闄呯敓娲讳腑銆?/span>
以上是关于201871010101-闄堟潵寮熴€婇潰鍚戝璞$▼搴忚璁★紙java锛夈€嬬鍗佸叚鍛ㄥ涔犳€荤粨的主要内容,如果未能解决你的问题,请参考以下文章
201871010101-陈来弟《面向对象程序设计(Java)》第十二周学习总结
201871010101-陈来弟《面向对象程序设计(Java)》第十一周学习总结
濡備綍鍦╳indows10涓婇潰杩愯HyperLedger Fabric1.4
陈来弟-201871010101《面向对象程序设计(Java)》第八周学习总结