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>

鎶€鏈浘鐗? src=

 

 

 杩涚▼鎯宠鎵ц浠诲姟灏遍渶瑕佷緷璧栫嚎绋嬨€傛崲鍙ヨ瘽璇达紝灏辨槸杩涚▼涓殑鏈€灏忔墽琛屽崟浣嶅氨鏄嚎绋嬶紝骞朵笖涓€涓繘绋嬩腑鑷冲皯鏈変竴涓嚎绋嬨€?/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>

                 鎶€鏈浘鐗? src=

 

 

 

 

璇磋繖涓瘽鍏跺疄鍙湁涓€鍗婂锛屽洜涓哄弽搴?ldquo;澶氳鑹?rdquo;鐨勭▼搴忎唬鐮侊紝鏈€璧风爜姣忎釜瑙掕壊瑕佺粰浠栦竴涓嚎绋嬪惂锛屽惁鍒欒繛瀹為檯鍦烘櫙閮芥棤娉曟ā鎷燂紝褰撶劧涔熸病娉曡鑳界敤鍗曠嚎绋嬫潵瀹炵幇锛氭瘮濡傛渶甯歌鐨?ldquo;鐢熶骇鑰咃紝娑堣垂鑰呮ā鍨?rdquo;銆?/span>
寰堝浜洪兘瀵瑰叾涓殑涓€浜涙蹇典笉澶熸槑纭紝濡傚悓姝ャ€佸苟鍙戠瓑绛夛紝璁╂垜浠厛寤虹珛涓€涓暟鎹瓧鍏革紝浠ュ厤浜х敓璇細銆?/span>

  • 澶氱嚎绋嬶細鎸囩殑鏄繖涓▼搴忥紙涓€涓繘绋嬶級杩愯鏃朵骇鐢熶簡涓嶆涓€涓嚎绋?/span>
  • 骞惰涓庡苟鍙戯細
  • 骞惰锛氬涓猚pu瀹炰緥鎴栬€呭鍙版満鍣ㄥ悓鏃舵墽琛屼竴娈靛鐞嗛€昏緫锛屾槸鐪熸鐨勫悓鏃躲€?/span>
  • 骞跺彂锛氶€氳繃cpu璋冨害绠楁硶锛岃鐢ㄦ埛鐪嬩笂鍘诲悓鏃舵墽琛岋紝瀹為檯涓婁粠cpu鎿嶄綔灞傞潰涓嶆槸鐪熸鐨勫悓鏃躲€傚苟鍙戝線寰€鍦ㄥ満鏅腑鏈夊叕鐢ㄧ殑璧勬簮锛岄偅涔堥拡瀵硅繖涓叕鐢ㄧ殑璧勬簮寰€寰€浜х敓鐡堕锛屾垜浠細鐢═PS鎴栬€匭PS鏉ュ弽搴旇繖涓郴缁熺殑澶勭悊鑳藉姏銆?/span>

 

鎶€鏈浘鐗? class=

 

  • 绾跨▼瀹夊叏锛氱粡甯哥敤鏉ユ弿缁樹竴娈典唬鐮併€傛寚鍦ㄥ苟鍙戠殑鎯呭喌涔嬩笅锛岃浠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>

 鎶€鏈浘鐗? src=

 

 

 

浜嗚В浜嗚繖涓や釜姒傚康涔嬪悗锛屾垜浠啀鏉ヨ璇翠粈涔堟槸澶氱嚎绋嬨€備妇涓緥瀛愶紝鎴戜滑鎵撳紑鑵捐绠″锛岃吘璁瀹舵湰韬氨鏄竴涓▼搴忥紝涔熷氨鏄瀹冨氨鏄竴涓繘绋嬶紝瀹冮噷闈㈡湁寰堝鐨勫姛鑳斤紝鎴戜滑鍙互鐪嬩笅鍥撅紝鑳芥煡鏉€鐥呮瘨銆佹竻鐞嗗瀮鍦俱€佺數鑴戝姞閫熺瓑浼楀鍔熻兘銆?/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.琚粓姝㈢殑绾跨▼

绾跨▼鍥犲涓嬩袱涓師鍥犱箣涓€鑰岃缁堟锛?/span>
銆€銆€鍥犱负 run 鏂规硶姝e父閫€鍑鸿€岃嚜鐒舵浜°€?/span>
銆€銆€鍥犱负涓€涓病鏈夋崟鑾风殑寮傚父缁堟浜?run 鏂规硶鑰屾剰澶栨浜?/span>
鐗瑰埆鏄紝 鍙互璋冪敤绾跨▼鐨?stop 鏂规硶鏉€姝讳竴涓嚎绋嬨€?璇ユ柟娉曟姏鍑?ThreadDeath 閿欒瀵硅薄 ,鐢辨鏉€姝荤嚎绋嬨€備絾鏄紝stop 鏂规硶宸茶繃鏃讹紝 涓嶈鍦ㄨ嚜宸辩殑浠g爜涓皟鐢ㄨ繖涓柟娉曘€?/span>
銆怉PI銆慾ava.iang.Thread 1.0锛?/span>
銆€銆€void join( )
绛夊緟缁堟鎸囧畾鐨勭嚎绋嬨€?/span>
銆€銆€void join( long millis )
绛夊緟鎸囧畾鐨勭嚎绋嬫浜℃垨鑰呯粡杩囨寚瀹氱殑姣鏁般€?/span>
銆€銆€Thread.State getState () 5 . 0
寰楀埌杩欎竴绾跨▼鐨勭姸鎬侊紱NEW銆丷UNNABLE BLOCKED銆?WAITING 銆乀IMED_WAITNG鎴?TERMINATED 涔嬩竴銆?/span>
銆€銆€void stop( )
鍋滄璇ョ嚎绋嬨€傝繖涓€鏂规硶宸茶繃鏃?/span>
銆€銆€void suspend()
鏆傚仠杩欎竴绾跨▼鐨勬墽琛屻€傝繖涓€鏂规硶宸茶繃鏃躲€?/span>
銆€銆€void resume()
鎭㈠绾跨▼銆傝繖涓€鏂规硶浠呬粎鍦ㄨ皟鐢?suspend() 涔嬪悗璋冪敤銆?/span>

 

绗簩閮ㄥ垎锛氬疄楠?/strong>

1銆佸疄楠岀洰鐨勪笌瑕佹眰

(1) 鎺屾彙Java搴旂敤绋嬪簭鐨勬墦鍖呮搷浣滐紱

(2) 鎺屾彙绾跨▼姒傚康锛?/span>

(3) 鎺屾彙绾跨▼鍒涘缓鐨勪袱绉嶆妧鏈€?/span>

2銆佸疄楠屽唴瀹瑰拰姝ラ

瀹為獙1锛?/strong> 瀵煎叆绗?3绔犵ず渚嬬▼搴忥紝娴嬭瘯绋嬪簭骞惰繘琛屼唬鐮佹敞閲娿€?/span>

娴嬭瘯绋嬪簭1

鍦?/strong>elipse IDE涓皟璇曡繍琛屾暀鏉?/strong>585椤电▼搴?/strong>13-1锛岀粨鍚堢▼搴忚繍琛岀粨鏋滅悊瑙g▼搴忥紱

灏嗘墍鐢熸垚鐨凧AR鏂囦欢绉诲埌鍙﹀涓€涓笉鍚岀殑鐩綍涓紝鍐嶈繍琛岃褰掓。鏂囦欢锛屼互渚跨‘璁ょ▼搴忔槸浠嶫AR鏂囦欢涓紝鑰屼笉鏄粠褰撳墠鐩綍涓鍙栫殑璧勬簮銆?/strong>

鎺屾彙鍒涘缓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);
   }
}

 

鎶€鏈浘鐗? src=

 

 浠ヤ笅鏄綊妗o細

鎶€鏈浘鐗? src=鎶€鏈浘鐗? src=

 

 

 

鎶€鏈浘鐗? src=

 

 

 

 

娴嬭瘯绋嬪簭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>

鎶€鏈浘鐗? src=

 

 

鍦╦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>

鎶€鏈浘鐗? src=

 

 

 

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>

鎶€鏈浘鐗? src=

 涓や釜瀹為獙瀵规瘮锛氱涓€涓▼搴忛潪绾跨▼鐨勶紝鎵ц涓€娆$▼搴忚绛夋墽琛屽畬鎵嶈兘鎵ц涓嬩竴娆★紱绗簩涓▼搴忔槸绾跨▼鐨勶紝鎵ц绗竴娆$▼搴忎笉鐢ㄧ瓑鎵ц瀹屽彲浠ユ帴鐫€鎵ц绗簩娆℃搷浣溿€?/span>

绗笁閮ㄥ垎锛氬疄楠屾€荤粨

  杩欒妭璇剧粨鏉燂紝涓€鏁村鏈熺殑璇剧▼灏卞凡缁忕粨鏉熶簡銆傚湪鍋氬悗涓€绔犺妭涓紝涓昏瀛︿範鍐呭鏄叧浜庡绾跨▼鐨勪簡瑙o紝鍏充簬浠€涔堟槸绾跨▼锛岀嚎绋嬪畨鍏ㄧ殑闆嗗悎锛屼腑鏂嚎绋嬶紝绾跨▼鐨勭姸鎬佺瓑鍑犱釜鏂归潰杩涜浜嗗涔犮€?/span>鑰岄€氳繃鏈瀹為獙鎰熻鑷繁涔熸湁浜嗕竴瀹氱殑杩涙銆侸ava瀛︿簡涓€瀛︽湡锛岃嚜宸变篃鏀惰幏浜嗗緢澶氾紝甯屾湜鍦ㄤ互鍚庯紝鍙互灏嗗鍒扮殑鐭ヨ瘑杩愮敤鍒板疄闄呯敓娲讳腑銆?/span>

以上是关于201871010101-闄堟潵寮熴€婇潰鍚戝璞$▼搴忚璁★紙java锛夈€嬬鍗佸叚鍛ㄥ涔犳€荤粨的主要内容,如果未能解决你的问题,请参考以下文章

201871010101-陈来弟《面向对象程序设计(Java)》第十二周学习总结

201871010101-陈来弟《面向对象程序设计(Java)》第十一周学习总结

濡備綍鍦╳indows10涓婇潰杩愯HyperLedger Fabric1.4

陈来弟-201871010101《面向对象程序设计(Java)》第八周学习总结

201871010101-陈来弟《面向对象程序设计(java)》第十七周学习总结

湖北省智能科教研究会走进璞华,调研璞公英教学平台个性化教学新模式

(c)2006-2024 SYSTEM All Rights Reserved IT常识