缁忔祹瑙傚療
杩愯惀绠$悊

android绾跨▼姹犵鐞?

鍓嶈█

绾跨▼姹犳槸Java涓殑涓€涓噸瑕佹蹇碉紝浠嶢ndroid涓婃潵璇达紝褰撴垜浠窡鏈嶅姟绔繘琛屾暟鎹氦浜掔殑鏃跺€欐垜浠兘鐭ラ亾涓荤嚎绋嬩笉鑳借繘琛岃仈缃戞搷浣滀互鍙婅€楁椂鎿嶄綔锛屼富绾跨▼杩涜鑱旂綉鎿嶄綔鍦?.0涔嬪悗浼氭姤涓€涓狽ewWorkOnMainTHreadException鐨勫紓甯革紝鑰屽湪涓荤嚎绋嬭繘琛岃€楁椂鎿嶄綔鍒欎細寮曡捣ANR(Application Not Responding),浣嗘槸鎴戜滑缁忓父瑕佽窡鏈嶅姟绔繘琛屼氦浜掞紝涓嬭浇鍜屼笂浼犳暟鎹瓑锛岃繖涔熷氨鏄繘琛岃仈缃戞搷浣滐紝鍦ㄥ垵瀛︽椂鎴戜滑閮芥槸閫氳繃new Thread鏉ュ紑鍚竴涓嚎绋嬭繘琛岃仈缃戞搷浣滐紝浣嗘槸璺熸湇鍔$浜や簰澶氫簡锛屽鏋滆繕鏄娇鐢╪ew Thread()鏉ュ紑鍚瓙绾跨▼锛屽湪涓€涓簲鐢ㄤ腑鎴戜滑棰戠箒鐨勫幓閫氳繃杩欎釜鏂规硶鍘诲紑鍚嚎绋嬶紝杩欏鎬ц兘鏉ヨ鏄緢澶х殑娴垂锛岄绻佺殑寮€鍚攢姣佺嚎绋嬪鍐呭瓨鐨勬秷鑰楁槸寰堝ぇ鐨勶紝鑰岄绻佺殑寮€鍚嚎绋嬩篃璁╂暣涓簲鐢ㄧ殑绾跨▼绠$悊鏄惧緱寰堟贩涔憋紝杩欐槸涓嶅彲鍙栫殑锛岃繖鏃跺€欎娇鐢ㄧ嚎绋嬫睜灏卞彲浠ヨВ鍐宠繖浜涢棶棰樹簡锛岃繖绡囨枃绔犳垜灏濊瘯灏嗙嚎绋嬫睜姒傚康鍜屽簲鐢ㄨ娓呮锛岀劧鍚庡皝瑁呬竴涓嚜瀹氫箟鐨勭嚎绋嬫睜绛栫暐浠ュ悗鏍规嵁涓氬姟闇€姹傜◢寰洿鏀逛笅鐩稿叧鐨勫弬鏁板嵆鍙€?/p>


绾跨▼姹犲ソ澶?/strong>

绾跨▼姹犲彲浠ヨВ鍐充袱涓笉鍚岄棶棰橈細鐢变簬鍑忓皯浜嗘瘡涓换鍔¤皟鐢ㄧ殑寮€閿€锛屽畠浠€氬父鍙互鍦ㄦ墽琛屽ぇ閲忓紓姝ヤ换鍔℃椂鎻愪緵澧炲己鐨勬€ц兘锛屽苟涓旇繕鍙互鎻愪緵缁戝畾鍜岀鐞嗚祫婧愶紙鍖呮嫭鎵ц浠诲姟闆嗘椂浣跨敤鐨勭嚎绋嬶級鐨勬柟娉曘€傛瘡涓?ThreadPoolExecutor 杩樼淮鎶ょ潃涓€浜涘熀鏈殑缁熻鏁版嵁锛屽瀹屾垚鐨勪换鍔℃暟銆?- - - - -Doug Lea锛堢嚎绋嬫睜璁捐鑰咃級

浠庡墠鏂囦篃鍙互寰楀嚭绾跨▼姹犳湁涓嬮潰鍑犱釜濂藉

锛?锛夎嚜瀹氫箟绾跨▼姹犵瓥鐣ュ彲浠ラ噸鐢ㄧ嚎绋嬫睜涓殑绾跨▼锛岄伩鍏嶅簲鐢ㄤ腑棰戠箒鐨勫垱寤哄拰閿€姣佺嚎绋嬫墍閫犳垚鐨勫唴瀛樻秷鑰椾互鍙婃€ц兘涓婁笉蹇呰鐨勫紑閿€锛?

锛?锛夐€氳繃鎺у埗绾跨▼姹犵殑鏈€澶х嚎绋嬫暟鑳芥湁鏁堟帶鍒剁嚎绋嬫睜鐨勬渶澶у苟鍙戞暟锛岄伩鍏嶅ぇ閲忕殑绾跨▼鍚屼竴鏃堕棿骞跺彂鎶㈠崰绯荤粺璧勬簮鑰岄€犳垚鐨勯樆濉炲拰鐣岄潰鍗℃锛?

锛?锛夊彲浠ヨ嚜宸辩鐞嗙嚎绋嬶紝瀹氭椂鎵ц锛岄棿闅斿惊鐜墽琛岋紝绾跨▼鍏抽棴绛夛紝閫氳繃瀵圭嚎绋嬬殑绠$悊鍙互閬垮厤婊ョ敤澶氱嚎绋嬮€犳垚鐨勯棶棰橈紝姣斿鍐呭瓨娉勯湶銆佺晫闈㈠崱姝汇€丆PU闃诲绛?/p>


ThreadPoolExecutor鏋勯€犳柟娉曡В閲?/strong>

绾跨▼姹犳湁鐫€寰堟繁鐨勪綋绯伙紝濡傛灉姣忎釜绫绘瘡涓帴鍙i兘缁嗚鏄惧緱涓嶇幇瀹烇紝鎵€浠ヨ繖閲岄噸璁插畠鐨勫疄鐜扮被锛孍xecutor鏄嚎绋嬫睜鐨勯《绾ф帴鍙o紝鑰孴hreadPoolExecutor鏄畠鐨勫疄鐜扮被锛岃繖涓被鎻愪緵浜嗕竴绯诲垪鏂规硶鏉ラ厤缃垜浠嚜宸辩殑绾跨▼姹狅紝瀹冧篃灏佽浜嗕竴浜涘吀鍨嬬殑绾跨▼姹犵瓥鐣ヤ緵鎴戜滑浣跨敤.

ThreadPoolExecutor缁ф壙鑷狝bstractExecutorService锛岃€孉bstractExecutorService瀹炵幇浜咵xecutorService鎺ュ彛锛孍xecutorService鎺ュ彛缁ф壙浜咵xecutor锛岄€氳繃鏀瑰彉ThreadPoolExecutor鏋勯€犳柟娉曢噷鐨勫弬鏁版垜浠彲浠ヨ嚜宸卞畾涔夌嚎绋嬫睜鐨勫睘鎬э紝瀹冧竴鍏辨湁鍥涗釜鏋勯€犳柟娉曪紝姣旇緝甯哥敤鐨勬槸涓嬮潰杩欎釜鏂规硶

public ThreadPoolExecutor(int corePoolSize,

int maximumPoolSize,

long keepAliveTime,

TimeUnit unit,

BlockingQueue workQueue,

ThreadFactory threadFactory)

浣嗘槸涓轰簡瀵硅繖涓被杩涜璇存槑鎴戦€夋嫨鐨勬槸鍙傛暟鏈€澶氱殑鏂规硶锛屽洜涓轰笉鍚屾瀯閫犳柟娉曟渶缁堝洖璋冪殑閮芥槸杩欎釜鏂规硶锛屽彧鏄璁¤€呭府鎴戝仛浜嗕竴浜涢粯璁ょ殑鎿嶄綔鎵€浠ヤ笉鐢ㄨ嚜宸辫缃€?/p>

public ThreadPoolExecutor(int corePoolSize,

int maximumPoolSize,

long keepAliveTime,

TimeUnit unit,

BlockingQueue workQueue,

ThreadFactory threadFactory,

RejectedExecutionHandler handler)

corePoolSize

绾跨▼姹犵殑淇濆瓨鐨勭嚎绋嬫暟锛屽畠鍖呮嫭绌洪棽绾跨▼锛屼笂闈㈡槸api涓殑瑙i噴锛屽叾瀹炶缁嗕簡璇磋繖鏄嚎绋嬫睜涓殑鏍稿績绾跨▼鏁帮紝鍦ㄩ粯璁ゆ儏鍐典笅锛屽嵆浣跨嚎绋嬫槸绌洪棽鐘舵€侊紝杩欎釜鏍稿績绾跨▼涔熶細鍦ㄧ嚎绋嬫睜涓瓨鍦ㄣ€備絾鏄璁¤繘琛屼簡涓€浜涢€昏緫澶勭悊

/**

* Returns true if this pool allows core threads to time out and

* terminate if no tasks arrive within the keepAlive time, being

* replaced if needed when new tasks arrive. When true, the same

* keep-alive policy applying to non-core threads applies also to

* core threads. When false (the default), core threads are never

* terminated due to lack of incoming tasks.

*

* @return {@code true} if core threads are allowed to time out,

* else {@code false}

*

* @since 1.6

public boolean allowsCoreThreadTimeOut() {

return allowCoreThreadTimeOut;

}

* If false (default), core threads stay alive even when idle.

* If true, core threads use keepAliveTime to time out waiting

* for work.

private volatile boolean allowCoreThreadTimeOut;

浠庢敞閲婃垜浠彲浠ョ湅鍑哄鏋滄妸allowCoreThreadTimeOut鐨勫睘鎬у€艰缃负true锛堥粯璁ゆ槸false锛夐偅涔堢幇鍦ㄧ┖闂茬殑鏍稿績绾跨▼鍦ㄧ瓑寰呮柊浠诲姟鏃朵細浣跨敤涓€涓秴鏃剁瓥鐣ワ紝杩欎釜鏃堕棿闂撮殧鐢眐eepAliveTime璁剧疆锛屽綋绌洪棽绾跨▼绛夊緟鐨勬椂闂磋秴杩囪缃椂闂存椂鏍稿績绾跨▼灏嗚缁堟銆?/p>

maximumPoolSize

绾跨▼姹犲厑璁稿绾崇殑鏈€澶х嚎绋嬫暟锛屽綋娲诲姩鐨勭嚎绋嬭揪鍒拌繖涓渶澶ф暟鏃跺鏋滃悗缁繕鏈変换鍔★紝閭f柊浠诲姟灏嗕細琚樆濉烇紙閫氳繃鍙傛暟鍙互璁剧疆闃诲鏃剁殑涓嶅悓澶勭悊绛栫暐锛夈€?/p>

keepAliveTime

闈炴牳蹇冪嚎绋嬮棽缃椂鐨勮秴鏃舵椂闀匡紝闈炴牳蹇冪嚎绋嬪彲浠ヨ繖涔堢悊瑙o紝姣斿鏍稿績绾跨▼鏁版槸2锛屾渶澶х嚎绋嬫暟3锛岄偅涔堣繖涓変釜閲岄潰鏈変竴涓氨鏄潪鏍稿績绾跨▼浜嗭紝鐢ㄤ釜姣斿柣灏辨槸涓存椂宸ワ紝褰撴牳蹇冩垚鍛樺共涓嶅畬浠诲姟灏变細鍙笂涓存椂宸ヤ竴璧凤紝浣嗘槸浠诲姟瀹屾垚浜嗕复鏃跺伐浠€涔堟椂鍊欒緸閫€灏辨槸鑷繁鎸囧畾浜?姣斿柣鍙兘涓嶆伆褰擄紝涓嶅甫浠讳綍鍏朵粬鐨勯殣鍠?,鍥炲埌鏂囩珷锛屽綋瓒呰繃杩欎釜鏃堕暱鏃讹紝闈炴牳蹇冪嚎绋嬪氨浼氳鍥炴敹锛屼絾鏄垰鎵嶄粙缁峜orePoolSize鐨勬椂鍊欎篃璇翠簡锛屽鏋滄妸allowCoreThreadTimeOut鐨勫睘鎬ц缃负true锛宬eepAliveTime涔熷彲浠ュ悓鏍峰鏍稿績绾跨▼杩涜缁堟鎿嶄綔銆?/p>

unit

灏辨槸keepAliveTime鐨勬椂闂村崟浣嶏紝鐩存帴鐢═imeUnit杩欎釜鏋氫妇灏卞彲浠ョ偣鍑烘潵锛屽父鐢ㄧ殑鏈塗imeUnit.MINUTS(鍒?,TimeUnit.SECONDS(绉?锛孴imeUnit.MILLISECONDS(姣).涓嶅父鐢ㄧ殑鏈塗imeUnit.HOURS(灏忔椂)锛孴imeUnit.DAYS锛堝ぉ锛夈€?/p>

workQueue

绾跨▼姹犱腑鐨勪换鍔¢槦鍒?闃诲闃熷垪)锛岀嚎绋嬫睜鐨別xecute鏂规硶鎻愪氦鐨凴unnable瀵硅薄瀛樺偍鍦ㄨ繖涓槦鍒楅噷闈?BlockingQueue鏄嚎绋嬪畨鍏ㄧ殑锛屽父鐢ㄧ殑闃诲闃熷垪鏈夊涓嬩簲涓?/p>

(1).LinkedBlockingQueue

閾捐〃闃诲闃熷垪锛岃繖涓槦鍒楃敱涓€涓摼琛ㄦ瀯鎴愶紝瀹冨唴閮ㄧ淮鎶や簡涓€涓暟鎹紦瀛橀槦鍒楋紝杩欎釜闃熷垪鎸?FIFO锛堝厛杩涘厛鍑猴級鎺掑簭鍏冪礌銆傞槦鍒楃殑澶撮儴 鏄湪闃熷垪涓椂闂存渶闀跨殑鍏冪礌銆傞槦鍒楃殑灏鹃儴 鏄湪闃熷垪涓椂闂存渶鐭殑鍏冪礌銆傛柊鍏冪礌鎻掑叆鍒伴槦鍒楃殑灏鹃儴锛屽苟涓旈槦鍒楄幏鍙栨搷浣滀細鑾峰緱浣嶄簬闃熷垪澶撮儴鐨勫厓绱犮€傞摼鎺ラ槦鍒楃殑鍚炲悙閲忛€氬父瑕侀珮浜庡熀浜庢暟缁勭殑闃熷垪锛屼絾鏄湪澶у鏁板苟鍙戝簲鐢ㄧ▼搴忎腑锛屽叾鍙鐭ョ殑鎬ц兘瑕佷綆銆?褰撴瀯閫犺繖涓槦鍒楀璞℃椂锛屽鏋滀笉鎸囧畾闃熷垪瀹归噺澶у皬锛屽畠鐨勯粯璁ゅ閲忓ぇ灏忔槸Integer.MAX_VALUE(鏃犻檺澶?,杩欐牱灏辨槸涓€涓棤鐣岄槦鍒楋紝闄ら潪鎻掑叆鑺傜偣浼氫娇闃熷垪瓒呭嚭瀹归噺锛屽惁鍒欐瘡娆℃彃鍏ュ悗浼氬姩鎬佸湴鍒涘缓閾炬帴鑺傜偣銆?/p>

(2).ArrayBlockingQueue

鏁扮粍闃诲闃熷垪锛岃繖鏄竴涓湁鐣岄樆濉為槦鍒楀唴閮紝缁存姢浜嗕竴涓畾闀挎暟缁勭敤鏉ョ紦瀛橀槦鍒椾腑鐨勬暟鎹璞★紝閫氳繃涓や釜鏁村瀷鍙橀噺鏉ユ爣璇嗛槦鍒楃殑澶磋窡灏惧湪鏁扮粍涓殑浣嶇疆锛岃繖涓槦鍒楁寜 FIFO锛堝厛杩涘厛鍑猴級鍘熷垯瀵瑰厓绱犺繘琛屾帓搴忋€傞槦鍒楃殑澶撮儴鏄湪闃熷垪涓瓨鍦ㄦ椂闂存渶闀跨殑鍏冪礌銆傞槦鍒楃殑灏鹃儴 鏄湪闃熷垪涓瓨鍦ㄦ椂闂存渶鐭殑鍏冪礌銆傛柊鍏冪礌鎻掑叆鍒伴槦鍒楃殑灏鹃儴锛岄槦鍒楄幏鍙栨搷浣滃垯鏄粠闃熷垪澶撮儴寮€濮嬭幏寰楀厓绱犮€傝繖鏄竴涓吀鍨嬬殑鈥滄湁鐣岀紦瀛樺尯鈥濓紝鍥哄畾澶у皬鐨勬暟缁勫湪鍏朵腑淇濇寔鐢熶骇鑰呮彃鍏ョ殑鍏冪礌鍜屼娇鐢ㄨ€呮彁鍙栫殑鍏冪礌銆備竴鏃﹀垱寤轰簡杩欐牱鐨勭紦瀛樺尯锛屽氨涓嶈兘鍐嶅鍔犲叾瀹归噺銆傝瘯鍥惧悜宸叉弧闃熷垪涓斁鍏ュ厓绱犱細瀵艰嚧鎿嶄綔鍙楅樆濉烇紱璇曞浘浠庣┖闃熷垪涓彁鍙栧厓绱犲皢瀵艰嚧绫讳技闃诲銆?

姝ょ被鏀寔瀵圭瓑寰呯殑鐢熶骇鑰呯嚎绋嬪拰浣跨敤鑰呯嚎绋嬭繘琛屾帓搴忕殑鍙€夊叕骞崇瓥鐣ャ€傞粯璁ゆ儏鍐典笅锛屼笉淇濊瘉鏄繖绉嶆帓搴忋€傜劧鑰岋紝閫氳繃灏嗗叕骞虫€?(fairness) 璁剧疆涓?true 鑰屾瀯閫犵殑闃熷垪鍏佽鎸夌収 FIFO 椤哄簭璁块棶绾跨▼銆傚叕骞虫€ч€氬父浼氶檷浣庡悶鍚愰噺锛屼絾涔熷噺灏戜簡鍙彉鎬у拰閬垮厤浜嗏€滀笉骞宠 鎬р€濄€?/p>

(3).DelayQueue

寤惰繜闃熷垪锛屽畠涔熸槸涓€涓棤鐣岄樆濉為槦鍒楋紝鍙湁鍦ㄥ欢杩熸湡婊℃椂鎵嶈兘浠庝腑鎻愬彇鍏冪礌銆傝繖涓槦鍒楃殑澶撮儴鏄欢杩熸湡婊″悗淇濆瓨鏃堕棿鏈€闀跨殑 Delayed 鍏冪礌銆傚鏋滃欢杩熼兘杩樻病鏈夋湡婊★紝鍒欓槦鍒楁病鏈夊ご閮紝骞朵笖 poll 灏嗚繑鍥?null銆傚綋涓€涓厓绱犵殑 getDelay(TimeUnit.NANOSECONDS) 鏂规硶杩斿洖涓€涓皬浜庣瓑浜?0 鐨勫€兼椂锛屽皢鍙戠敓鍒版湡銆傚嵆浣挎棤娉曚娇鐢?take 鎴?poll 绉婚櫎鏈埌鏈熺殑鍏冪礌锛屼篃涓嶄細灏嗚繖浜涘厓绱犱綔涓烘甯稿厓绱犲寰呫€備緥濡傦紝size 鏂规硶鍚屾椂杩斿洖鍒版湡鍜屾湭鍒版湡鍏冪礌鐨勮鏁般€傛闃熷垪涓嶅厑璁镐娇鐢?null 鍏冪礌銆?閫氬父鐢ㄨ繖涓槦鍒楁潵绠$悊涓€涓秴鏃舵湭鍝嶅簲闃熷垪銆?/p>

(4).PriorityBlockingQueue

浼樺厛闃诲闃熷垪锛屽畠涔熸槸涓€涓棤鐣岄樆濉為槦鍒楋紝瀹冧娇鐢ㄤ笌绫?PriorityQueue 鐩稿悓鐨勯『搴忚鍒欙紝骞朵笖鎻愪緵浜嗛樆濉炶幏鍙栨搷浣溿€傝櫧鐒惰繖涓槦鍒楅€昏緫涓婃槸鏃犵晫鐨勶紝浣嗘槸璧勬簮琚€楀敖鏃惰瘯鍥炬墽琛?add 鎿嶄綔涔熷皢澶辫触锛堝鑷?OutOfMemoryError锛夈€傛绫讳笉鍏佽浣跨敤null(绌?鍏冪礌銆備緷璧栬嚜鐒堕『搴忕殑浼樺厛绾ч槦鍒椾篃涓嶅厑璁告彃鍏ヤ笉鍙瘮杈冪殑瀵硅薄锛堣繖鏍峰仛浼氬鑷存姏鍑?ClassCastException锛夈€?鍦ㄥ垱寤洪槦鍒楀璞℃椂閫氳繃鏋勯€犲嚱鏁颁腑鐨凜omparator灞炴€у璞℃潵鍐冲畾浼樺厛绾с€?/p>

(5).SynchronousQueue

鍚屾闃诲闃熷垪锛屼竴绉嶆棤缂撳啿闃诲闃熷垪锛屽叾涓瘡涓彃鍏ユ搷浣滃繀椤荤瓑寰呭彟涓€涓嚎绋嬬殑瀵瑰簲绉婚櫎鎿嶄綔 锛屽弽涔嬩害鐒躲€傚悓姝ラ槦鍒楁病鏈変换浣曞唴閮ㄥ閲忥紝鐢氳嚦杩炰竴涓槦鍒楃殑瀹归噺閮芥病鏈夈€備笉鑳藉湪鍚屾闃熷垪涓婅繘琛?peek锛屽洜涓轰粎鍦ㄨ瘯鍥捐绉婚櫎鍏冪礌鏃讹紝璇ュ厓绱犳墠瀛樺湪锛涢櫎闈炲彟涓€涓嚎绋嬭瘯鍥剧Щ闄ゆ煇涓厓绱狅紝鍚﹀垯涔熶笉鑳斤紙浣跨敤浠讳綍鏂规硶锛夋彃鍏ュ厓绱狅紱涔熶笉鑳借凯浠i槦鍒楋紝鍥犱负鍏朵腑娌℃湁鍏冪礌鍙敤浜庤凯浠c€傞槦鍒楃殑澶?鏄皾璇曟坊鍔犲埌闃熷垪涓殑棣栦釜宸叉帓闃熸彃鍏ョ嚎绋嬬殑鍏冪礌锛涘鏋滄病鏈夎繖鏍风殑宸叉帓闃熺嚎绋嬶紝鍒欐病鏈夊彲鐢ㄤ簬绉婚櫎鐨勫厓绱犲苟涓?poll() 灏嗕細杩斿洖 null銆傚浜庡叾浠?Collection (闆嗗悎)鏂规硶锛堜緥濡?contains锛夛紝鍚屾闃熷垪浣滀负涓€涓┖闆嗗悎杩欎釜涓嶅厑璁竛ull(绌?鍏冪礌銆?

鍚屾闃熷垪绫讳技浜?CSP 鍜?Ada 涓娇鐢ㄧ殑 rendezvous 淇¢亾銆傚畠闈炲父閫傚悎浜庝紶閫掓€ц璁★紝鍦ㄨ繖绉嶈璁′腑锛屽湪涓€涓嚎绋嬩腑杩愯鐨勫璞¤灏嗘煇浜涗俊鎭€佷簨浠舵垨浠诲姟浼犻€掔粰鍦ㄥ彟涓€涓嚎绋嬩腑杩愯鐨勫璞★紝瀹冨氨蹇呴』涓庤瀵硅薄鍚屾銆?

瀵逛簬姝e湪绛夊緟鐨勭敓浜ц€呭拰浣跨敤鑰呯嚎绋嬭€岃█锛屾绫绘敮鎸佸彲閫夌殑鍏钩鎺掑簭绛栫暐銆傞粯璁ゆ儏鍐典笅涓嶄繚璇佽繖绉嶆帓搴忋€備絾鏄紝浣跨敤鍏钩璁剧疆涓?true 鎵€鏋勯€犵殑闃熷垪鍙繚璇佺嚎绋嬩互 FIFO 鐨勯『搴忚繘琛岃闂€?/p>

threadFactory

绾跨▼宸ュ巶锛岀敤浜庡湪绾跨▼姹犱腑鍒涘缓鏂扮嚎绋嬶紝绾跨▼姹犺璁¤€呮彁渚涗簡涓や釜锛堝叾瀹炴槸涓€涓級绾跨▼姹犲伐鍘傜粰鎴戜滑浣跨敤锛屼竴涓槸defaultThreadFactory()锛屽彟涓€涓槸privilegedThreadFactory锛屼絾鏄煡鐪嬫簮鐮佹垜鍙戠幇

* Legacy security code; do not use.

public static ThreadFactory privilegedThreadFactory() {

return new PrivilegedThreadFactory();

}

璁捐鑰呰杩欐槸閬楃暀鐨勫畨鍏ㄤ唬鐮侊紝鍙垜浠笉瑕佷娇鐢╬rivilegedThreadFactory..,鎵€浠ヤ竴鑸嚎绋嬫睜宸ュ巶鎴戜滑鐢ㄧ殑鏄痙efaultThreadFactory锛岀敤娉曞緢绠€鍗曪紝鐩存帴鐢‥xecutors.defaultThreadFactory();灏卞彲浠ュ垱寤轰竴涓粯璁ょ嚎绋嬫睜宸ュ巶銆?/p>

handler

杩欎釜鍙傛暟鏄竴涓猂ejectedExecutionHandler瀵硅薄锛屽畠鏄竴涓帴鍙o紝鍙湁rejectedExecution杩欎釜鏂规硶锛岃繖涓弬鏁扮殑浣滅敤鏄綋绾跨▼姹犵敱浜庝换鍔¢槦鍒楀凡婊℃垨鍒殑鍘熷洜鏃犳硶鎵ц鏂颁换鍔℃椂锛孴hreadPoolExecutor灏变細鍥炶皟瀹炵幇浜嗚繖涓帴鍙g殑绫绘潵澶勭悊琚嫆缁濅换鍔★紝瀹冪殑浣跨敤鏄洿鎺ョ敤THreadPoolExecutor.XXX,绾跨▼姹犺璁¤€呮彁渚涗簡鍥涗釜澶勭悊鏂规硶锛?/p>

(1).ThreadPoolExecutor.AbortPolicy

鐩存帴鎶涘嚭RejectedExecutionException寮傚父锛屽鏋滀笉璁剧疆澶勭悊绛栫暐榛樿鏄繖涓柟娉曡繘琛屽鐞?/p>

(2).ThreadPoolExecutor.CallerRunsPolicy

鐩存帴鍦?execute 鏂规硶鐨勮皟鐢ㄧ嚎绋嬩腑杩愯琚嫆缁濈殑浠诲姟锛涘鏋滄墽琛岀▼搴忓凡鍏抽棴锛屽垯浼氫涪寮冭浠诲姟.

(3).ThreadPoolExecutor.DiscardOldestPolicy

鏀惧純鏈€鏃х殑鏈鐞嗚姹傦紝鐒跺悗閲嶈瘯 execute锛涘鏋滄墽琛岀▼搴忓凡鍏抽棴锛屽垯浼氫涪寮冭浠诲姟銆?/p>

(4).ThreadPoolExecutor.DiscardPolicy

榛樿鎯呭喌涓嬬洿鎺ヤ涪寮冭鎷掔粷鐨勪换鍔°€?/p>

鍦ㄥ钩甯镐娇鐢ㄦ椂鍙互鏍规嵁瀹為檯闇€瑕佸埗瀹氱浉鍏崇殑鎿嶄綔銆?/p>

灏忕粨

THreadPoolExecutor鍦ㄦ墽琛屼换鍔℃椂澶т綋鏄繖鏍风殑锛屽鏋滅嚎绋嬫睜涓嚎绋嬬殑鏁伴噺娌℃湁杈惧埌鏍稿績绾跨▼鐨勬暟閲忥紝瀹冨氨浼氬惎鍔ㄤ竴涓牳蹇冪嚎绋嬫潵鎵ц浠诲姟锛屽鏋滆揪鍒版垨瓒呰繃鏍稿績绾跨▼鏁伴噺锛岄偅涔堜换鍔′細鎻掑叆鍒颁换鍔¢槦鍒椾腑绛夊緟鎵ц锛屽鏋滀换鍔¢槦鍒楁弧浜嗐€備絾鏄嚎绋嬫暟閲忔病鏈夎揪鍒扮嚎绋嬫睜鐨勬渶澶ф暟锛岄偅灏卞洖鍚姩涓€涓潪鏍稿績绾跨▼(涓存椂宸?鏉ユ墽琛屼换鍔★紝濡傛灉绾跨▼鏁伴噺杈惧埌绾跨▼姹犳渶澶ф暟锛岄偅涔堝氨浼氭嫆缁濆鐞嗘柊浠诲姟骞惰皟鐢╤andler鐨剅ejectedExecution鏉ュ鐞嗘嫆缁濅换鍔?澶勭悊绛栫暐灏辨槸涓婇潰鍥涚)銆?/p>


绾跨▼姹犺璁¤€呮彁渚涚殑鍥涚绾跨▼姹犵瓥鐣?/strong>

绾跨▼姹犺璁¤€呬负鎴戜滑鎻愪緵浜嗗洓绉嶄笉鍚岀殑绾跨▼姹犵瓥鐣ワ紝鍒嗗埆鏄疐ixedThreadPool锛孲ingleThreadExecutor锛孋achedThreadPool锛孲cheduledThreadPool锛屼粬浠斁鍦ㄤ簡Executors杩欎釜绫讳腑锛屼笅闈㈤€愪竴鍒嗘瀽

(1).FixedThreadPool

杩欎釜绛栫暐婧愮爜涓猴細

public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {

return new ThreadPoolExecutor(nThreads, nThreads,

0L, TimeUnit.MILLISECONDS,

new LinkedBlockingQueue(),

threadFactory);

}

閫氳繃Executors.newFixedThreadPool()杩欎釜鏂规硶鎴戜滑鍙互鍒涘缓杩欎釜绾跨▼姹狅紝杩欐槸涓€涓嚎绋嬫暟閲忓浐瀹氫笖鍙噸鐢ㄧ殑绾跨▼姹犲畠鐨勬牳蹇冪嚎绋嬫暟鍜屾渶澶х嚎绋嬫暟鏄竴鏍风殑锛屽綋绾跨▼澶勪簬绌洪棽鐘舵€佹椂锛岄櫎闈炵嚎绋嬫睜鍏抽棴锛屽惁鍒欑嚎绋嬩笉浼氳鍥炴敹锛岋紝褰撴墍鏈夌嚎绋嬮兘澶勪簬娲诲姩鐘舵€佹椂锛屾柊浠诲姟閮戒細澶勪簬绛夊緟鐘舵€侊紝杩欎釜绾跨▼姹犵瓥鐣ユ病鏈夎秴鏃剁瓥鐣ワ紙0L锛夛紝浠诲姟闃熷垪涔熸槸鏃犵晫闃熷垪(LinkedBlockingQueue),绾跨▼宸ュ巶鐢辫皟鐢ㄨ€呰嚜宸辨寚瀹氾紝鎷掔粷浠诲姟澶勭悊绛栫暐鏄粯璁ゅ鐞嗙瓥鐣?鐩存帴鎶涘嚭RejectedExecutionException寮傚父)銆?/p>

(2).SingleThreadExecutor

杩欎釜绛栫暐婧愮爜涓?

public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) {

return new FinalizableDelegatedExecutorService

(new ThreadPoolExecutor(1, 1,

0L, TimeUnit.MILLISECONDS,

new LinkedBlockingQueue(),

threadFactory));

}

浠庤繑鍥炵殑鍙傛暟鍙互鐪嬪嚭杩欎釜绛栫暐鍙湁涓€涓牳蹇冪嚎绋嬶紝瀹冪‘淇濅簡鎵€鏈変换鍔¢兘鏄湪鍚屼竴涓嚎绋嬩腑鎸夐『搴忔潵鎵ц锛岃繖灏变笉闇€瑕佸鐞嗙嚎绋嬪悓姝ョ殑闂浜嗭紝鐢变簬鏈€澶х嚎绋嬫暟璺熸牳蹇冪嚎绋嬫暟閮芥槸1锛屾墍浠ヤ篃灏变笉瀛樺湪闈炴牳蹇冪嚎绋嬭秴鏃剁瓥鐣ヤ簡锛屼换鍔¢槦鍒椾负鏃犵晫闃熷垪锛岀嚎绋嬫睜宸ュ巶鏄粯璁ょ殑绾跨▼姹犲伐鍘傘€?/p>

(3).CachedThreadPool

杩欎釜绛栫暐婧愮爜涓?

public static ExecutorService newCachedThreadPool() {

return new ThreadPoolExecutor(0, Integer.MAX_VALUE,

60L, TimeUnit.SECONDS,

new SynchronousQueue());

}

浠庤繑鍥炵殑鍙傛暟鍙互鐪嬪嚭锛屽畠骞舵病鏈夋牳蹇冪嚎绋嬫暟(0),鏈€澶х嚎绋嬫暟涓篒nteger.MAX_VALUE(鍙互鐪嬪仛浠绘剰澶?,褰撶嚎绋嬫睜鐨勭嚎绋嬮兘澶勪簬娲诲姩鐘舵€佹椂锛屽鏋滄湁鏂颁换鍔★紝绾跨▼姹犱細鍒涘缓鏂扮殑绾跨▼鏉ュ鐞嗘柊浠诲姟锛屽惁鍒欏氨浼氱敤闂茬疆绾跨▼鏉ュ鐞嗕换鍔★紝瀹冪殑闈炴牳蹇冪嚎绋嬭秴鏃剁瓥鐣ユ槸60绉?60L),瓒呰繃60绉掗棽缃嚎绋嬪氨浼氳鍥炴敹锛屽墠闈㈣杩嘢ynchronousQueue鏄竴涓┖闆嗗悎锛屾槸鏃犳硶瀛樺偍鍏冪礌鐨勶紝杩欏氨閫犳垚浜嗗彧瑕佹湁浠诲姟灏变細琚珛鍗虫墽琛岋紝杩欑绾跨▼姹犵瓥鐣ュ鏍告墽琛屽ぇ閲忓崟鑰楁椂杈冨皯鐨勪换鍔★紝濡傛灉绾跨▼姹犲浜庨棽缃姸鎬侊紝鐢变簬鏈夎秴鏃剁瓥鐣ワ紝绾跨▼姹犱腑鐨勬墍鏈夌嚎绋嬮兘浼氳鍋滄锛岃繖鏃剁嚎绋嬫睜涓病鏈変换浣曠嚎绋嬶紝鎵€浠ヤ笉鍗犵敤浠讳綍绯荤粺璧勬簮銆?/p>

(4).ScheduledThreadPool

杩欎釜绛栫暐婧愮爜涓猴細

public static ScheduledExecutorService newSingleThreadScheduledExecutor() {

return new DelegatedScheduledExecutorService

(new ScheduledThreadPoolExecutor(1));

}

public ScheduledThreadPoolExecutor(int corePoolSize) {

super(corePoolSize, Integer.MAX_VALUE,

DEFAULT_KEEPALIVE_MILLIS, MILLISECONDS,

new DelayedWorkQueue());

}

浠庡弬鏁板彲浠ョ湅鍑猴紝瀹冩牳蹇冪嚎绋嬫槸璋冪敤鑰呰嚜宸辫瀹氱殑锛屼篃灏辨槸涓€涓浐瀹氱殑鍊硷紝浣嗘槸鏈€澶х嚎绋嬫暟鏄棤闄愬ぇ锛岃€屼笖瓒呮椂绛栫暐鏄?0姣锛堝熀鏈氨鏄珛鍗充簡..锛夊洖鏀讹紝鑰岄樆濉為槦鍒楁槸寤舵椂闃熷垪锛岃繖涔熻鏄庝簡杩欑绾跨▼姹犵瓥鐣ユ槸鎵ц瀹氭椂鎴栧懆鏈熼噸澶嶇殑浠诲姟銆?/p>

灏忕粨

鍏跺疄褰撴垜浠墠闈㈡妸ThreadPoolExecutor鏋勯€犳柟娉曡繘琛屽垎鏋愪互鍚庣悊瑙oug Lea锛堢嚎绋嬫睜璁捐鑰咃級缁欐垜浠彁渚涚殑鍑犵绾跨▼姹犵瓥鐣ュ氨涓嶉毦浜嗭紝鎴戜釜浜鸿寰桪oug Lea涔嬫墍浠ョ粰鎴戜滑鎻愪緵浜嗗嚑绉嶇瓥鐣ユ槸涓轰簡鏂逛究鎴戜滑浣跨敤绾跨▼姹狅紝浣嗘槸濡傛灉鎴戜滑浜嗚В浜嗗弬鏁扮殑鍚箟鎴戜滑瀹屽叏鍙互鑷繁瀹氫箟涓€涓睘浜庤嚜宸辩殑绾跨▼姹犵瓥鐣ャ€備笅闈㈡垜灏佽涓€涓嚜瀹氫箟鐨勭嚎绋嬫睜绛栫暐銆?/p>


灏佽鑷繁鐨勭嚎绋嬫睜绛栫暐

鍓嶉潰璇翠簡Doug Lea锛堢嚎绋嬫睜璁捐鑰咃級鎻愪緵浜嗗洓绉嶅叿鏈変笉鍚岀壒鎬х殑绾跨▼姹犵瓥鐣ョ粰鎴戜滑浣跨敤锛屾棩甯稿紑鍙戜娇鐢ㄥ叾涓竴绉嶄篃鏄彲浠ョ殑锛屼絾鏄垜浠棦鐒舵噦寰椾簡鍘熺悊褰撶劧鍙互鑷繁鑷畾涔変竴涓潵浣跨敤锛屼笅闈㈡垜涓€姝ユ灏佽涓€涓嚎绋嬫睜绛栫暐鐒跺悗杩涜娴嬭瘯(杩欎釜绛栫暐鏌愪簺鍙傛暟鍙傝€冧簡AsyncTask)銆?/p>

棣栧厛鏄嚎绋嬫睜浠g悊绫伙紝婧愮爜涓猴細

public class ThreadPoolProxy {

private ThreadPoolExecutor executor;

private int corePollSize;//鏍稿績绾跨▼鏁?/p>

private int maximumPoolSize;//鏈€澶х嚎绋嬫暟

private long keepAliveTime;//闈炴牳蹇冪嚎绋嬫寔缁椂闂?/p>

public ThreadPoolProxy(int corePollSize, int maximumPoolSize, long keepAliveTime) {

this.corePollSize = corePollSize;

this.maximumPoolSize = maximumPoolSize;

this.keepAliveTime = keepAliveTime;

}

/**

* 鍗曚緥锛屼骇鐢熺嚎绋?/p>

@TargetApi(Build.VERSION_CODES.GINGERBREAD)

public void getThreadPoolProxyInstant() {

if (executor == null)

synchronized (ThreadPoolProxy.class) {

if (executor == null) {

BlockingDeque workQueue = new LinkedBlockingDeque(128);//浠诲姟闃熷垪瀹归噺128

ThreadFactory threadFactory = Executors.defaultThreadFactory();//榛樿鐨勭嚎绋嬪伐鍘?/p>

RejectedExecutionHandler handler = new ThreadPoolExecutor.AbortPolicy();//鎷掔粷浠诲姟鏃剁殑澶勭悊绛栫暐锛岀洿鎺ユ姏寮傚父

executor = new ThreadPoolExecutor(corePollSize,

maximumPoolSize,

keepAliveTime,

TimeUnit.SECONDS,

workQueue,

threadFactory,

handler);

}

}

}

/**

* 鎵ц浠诲姟

*

* @param task

public void excute(Runnable task) {

getThreadPoolProxyInstant();

executor.execute(task);

}

/**

* 鍒犻櫎浠诲姟

*

* @param task

public boolean remove(Runnable task) {

getThreadPoolProxyInstant();

boolean remove = executor.remove(task);

return remove;

}

}

杩欓噷鎴戞妸闈炴牳蹇冪嚎绋嬭秴鏃剁瓥鐣ュ崟浣嶈缃负浜嗙锛屼换鍔¢槦鍒楀閲忚缃负128锛岀嚎绋嬫睜宸ュ巶璁剧疆鎴愪簡榛樿宸ュ巶锛屾嫆缁濅换鍔$殑绛栫暐璁剧疆鎴愪簡鐩存帴鎶涘嚭寮傚父銆傜劧鍚庡墿涓嬬殑涓変釜鍙傛暟鍦ㄥ伐鍘傜被鐢熶骇绾跨▼鏃惰繘琛岃缃紝宸ュ巶绫讳唬鐮佷负锛?/p>

public class ThreadPoolFactory {

private static ThreadPoolProxy commonThreadPool;//鑾峰彇涓€涓櫘閫氱殑绾跨▼姹犲疄渚?/p>

private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();//鑾峰緱CPU鏁伴噺

private static final int COMMOM_CORE_POOL_SIZE = CPU_COUNT + 1;//鏍规嵁CPU鏍告暟鏉ヨ瀹氭牳蹇冪嚎绋嬫暟

private static final int COMMON_MAXPOOL_SIZE = CPU_COUNT * 2 + 1;//鏈€澶х嚎绋嬫睜鏁?/p>

private static final long COMMON_KEEP_LIVE_TIME = 1L;//鎸佺画鏃堕棿

public static ThreadPoolProxy getCommonThreadPool() {

if (commonThreadPool == null)

synchronized (ThreadPoolFactory.class) {

if (commonThreadPool == null)

commonThreadPool = new ThreadPoolProxy(COMMOM_CORE_POOL_SIZE,

COMMON_MAXPOOL_SIZE, COMMON_KEEP_LIVE_TIME);

}

return commonThreadPool;

}

}

杩欓噷鐨勬牳蹇冪嚎绋嬭窡璺熸渶澶х嚎绋嬫暟鏄牴鎹墜鏈虹殑cpu鏍稿績鏁拌繘琛屽姩鎬佽缃紝鐒跺悗鎶婁娇鐢ㄦ柟娉曞皝瑁呮垚涓€涓猆til绫伙細

public class ThreadPoolUtil {

/**

* 鍦ㄩ潪UI绾跨▼涓墽琛?/p>

*

* @param task

public static void runTaskInThread(Runnable task) {

ThreadPoolFactory.getCommonThreadPool().excute(task);

}

public static Handler handler = new Handler();

/**

* 鍦║I绾跨▼涓墽琛?/p>

*

* @param task

public static void runTaskInUIThread(Runnable task) {

handler.post(task);

}

/**

* 绉婚櫎绾跨▼姹犱腑绾跨▼

*

* @param task

public static boolean removeTask(Runnable task) {

boolean remove = ThreadPoolFactory.getCommonThreadPool().remove(task);

return remove;

}

}

閫氳繃涓婇潰鐨勫皝瑁呭氨鍙互鑷畾涔変竴涓嚜宸辩殑绾跨▼姹犵瓥鐣ヤ簡锛屽綋鐒惰繖涓瓥鐣ョ殑鍙傛暟鍙互鏍规嵁瀹為檯闇€瑕佽繘琛岄厤缃紝鎴戣繖閲屾槸鐩存帴鍙傝€傾syncTask鏉ラ厤缃殑銆?

鐒跺悗鍋氫釜鏍楀瓙鏉ラ獙璇侊細

鎴戣繖閲屾槸妯℃嫙涓嬭浇锛岀晫闈㈡埅鍥惧涓嬶細

涓荤晫闈笟鍔¢€昏緫涓猴細

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private ProgressBar pb1;

private ProgressBar pb2;

private ProgressBar pb3;

private ProgressBar pb4;

private Button btn1;

private Button btn2;

private Button btn3;

private Button btn4;

private static final int FLAGS1 = 1;

private static final int FLAGS2 = 2;

private static final int FLAGS3 = 3;

private static final int FLAGS4 = 4;

private Handler mHandler = new Handler() {

@Override

public void handleMessage(Message msg) {

switch (msg.what) {

case FLAGS1:

pb1.setProgress(msg.arg1);

break;

case FLAGS2:

pb2.setProgress(msg.arg1);

break;

case FLAGS3:

pb3.setProgress(msg.arg1);

break;

case FLAGS4:

pb4.setProgress(msg.arg1);

break;

default:

break;

}

}

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

init();

}

private void init() {

pb1 = (ProgressBar) findViewById(R.id.pb1);

pb2 = (ProgressBar) findViewById(R.id.pb2);

pb3 = (ProgressBar) findViewById(R.id.pb3);

pb4 = (ProgressBar) findViewById(R.id.pb4);

btn1 = (Button) findViewById(R.id.btn1);

btn2 = (Button) findViewById(R.id.btn2);

btn3 = (Button) findViewById(R.id.btn3);

btn4 = (Button) findViewById(R.id.btn4);

btn1.setOnClickListener(this);

btn2.setOnClickListener(this);

btn3.setOnClickListener(this);

btn4.setOnClickListener(this);

}

private Runnable runnable1 = new MRunnable(FLAGS1);

private Runnable runnable2 = new MRunnable(FLAGS2);

private Runnable runnable3 = new MRunnable(FLAGS3);

private Runnable runnable4 = new MRunnable(FLAGS4);

@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.btn1:

updateProcessBar(runnable1);

break;

case R.id.btn2:

updateProcessBar(runnable2);

break;

case R.id.btn3:

updateProcessBar(runnable3);

break;

case R.id.btn4:

updateProcessBar(runnable4);

break;

default:

break;

}

}

private void updateProcessBar(Runnable runnable) {

ThreadPoolUtil.runTaskInThread(runnable);

}

private class MRunnable implements Runnable {

private int mFlags;

public MRunnable(int flags) {

this.mFlags = flags;

}

@Override

public void run() {

for (int i = 0; i

Message msg = mHandler.obtainMessage();

//闅忔満鏁帮紝璁╀笅杞芥洿鐪熷疄...

Random rand = new Random();

int randNum = rand.nextInt(100);

SystemClock.sleep(randNum);

msg.what = mFlags;

msg.arg1 = i;

mHandler.sendMessage(msg);

}

}

}

/**

* 閫€鍑烘椂娓呴櫎绾跨▼

@Override

protected void onDestroy() {

super.onDestroy();

ThreadPoolUtil.removeTask(runnable1);

ThreadPoolUtil.removeTask(runnable2);

ThreadPoolUtil.removeTask(runnable3);

ThreadPoolUtil.removeTask(runnable4);

}

}

鐒跺悗涓轰簡鏂逛究娴嬭瘯锛屾垜鎶婃牳蹇冪嚎绋嬫暟鍥哄畾锛岃€屼笉鏄牴鎹瓹PU鏍稿績鏁版潵鏀瑰彉锛屽綋鎴戞妸鏍稿績绾跨▼鏁板畾涓?锛岃繖鏃舵晥鏋滀负锛?

鍙互鐪嬪埌锛屽綋鎴戜緷娆″惎鍔?銆?銆?銆?鏃讹紝鐢变簬闃熷垪鎴戜滑璁剧疆鎴愪簡LinkedBlockingDeque锛岃繖涓槦鍒楁槸鎸?FIFO锛堝厛杩涘厛鍑猴級鎺掑簭鍏冪礌鐨勶紝鎵€浠ュ氨浼氭寜椤哄簭鏉ユ墽琛岋紝涔熷氨鏄綋1鎵ц鏃跺叾浠栦换鍔$殑閮藉湪闃熷垪涓樆濉烇紝鍙湁1瀹屾垚浜嗘墠浼氬線涓嬫墽琛岋紝杩欎篃鍗拌瘉浜嗘垜浠叧浜庣嚎绋嬫睜鐨勭殑鍙傛暟灞炴€ц瑙o紝杩欓噷鍙兘浣犱滑浼氭湁鐤戦棶锛屽墠闈笉鏄褰撲换鍔℃暟瓒呰繃corePoolSize灏变細鍒涘缓鏂扮殑闈炴牳蹇冪嚎绋?涓存椂宸?鍘诲鐞嗕换鍔″悧锛岃繖閲屾€庝箞娌℃湁杩欐牱?鍏跺疄鍒涘缓鏂扮殑绾跨▼鏄湁鏉′欢鐨勶紝杩欎釜鏉′欢鏄綘鐨勪换鍔¢槦鍒楀凡缁忔弧浜嗭紝杩欐椂濡傛灉杩樺姞鏂扮殑浠诲姟鎵嶄細鍘诲垱寤洪潪鏍稿績绾跨▼鍘诲鐞嗚繖涓换鍔★紝瑕侀獙璇佸叾瀹炰篃寰堢畝鍗曪紝鍙鎶?/p>

BlockingDeque workQueue = new LinkedBlockingDeque(128);

杩欎釜闃熷垪澶у皬浠?28鏀逛负2锛屽啀閲嶆柊鍚姩锛屾晥鏋滀负锛?

浠庡浘鐗囧彲浠ョ湅鍒帮紝褰撴垜渚濇鍚姩浠诲姟1銆?銆?銆?鏃讹紝椤哄簭鏄厛1鍚姩锛岀劧鍚?銆?闃诲锛岀劧鍚庡啀鍚姩4,4涔熻窡鐫€涓€璧锋墽琛岋紝鐒跺悗杩欎袱涓换鍔℃墽琛屽畬涔嬪悗鎵嶄緷娆℃墽琛?銆?锛岃繖涔熼獙璇佷簡鎴戜滑涓婇潰鐨勫垎鏋愶紝鎴戝惎鍔ㄤ换鍔?锛?绔嬪嵆鎵ц锛岃繖鏃朵换鍔¢槦鍒楁槸娌℃湁浠诲姟鍦ㄧ瓑寰呯殑锛岀劧鍚庢垜鍚姩浠诲姟2銆?锛岃繖鏃朵换鍔¢槦鍒楀苟娌℃湁婊★紝鎵€浠ヨ繖涓や釜浠诲姟灏卞湪浠诲姟闃熷垪涓樆濉炵瓑寰咃紝褰撴垜鍐嶅惎鍔ㄤ换鍔?鏃讹紝杩欐椂浠诲姟闃熷垪宸茬粡婊′簡锛岃€岀嚎绋嬫暟閲忔病杈惧埌鏈€澶х嚎绋嬫暟锛屾墍浠ヨ繖涓瓥鐣ュ氨鏂板紑浜嗕竴涓潪鏍稿績绾跨▼鍘绘墽琛?杩欎釜鏂扮殑浠诲姟锛岀劧鍚庣敱浜庤繖鏃舵病鏈夋柊浠诲姟锛屾墍浠ユ€诲叡灏辨湁涓や釜绾跨▼鍘绘墽琛屼换鍔′簡锛屽綋1,4鏈変竴涓墽琛屽畬浜嗭紝灏变細鍘婚槦鍒椾腑渚濇鎵ц2,3銆?/p>

鎶婁换鍔¢槦鍒楀ぇ灏忚皟鍥?28缁х画鎴戜滑鐨勮璇侊紝鎶婃牳蹇冪嚎绋嬫暟鏀规垚2锛岃繖鏃舵晥鏋滀负锛?

鍙互鐪嬪埌瀹冩瘡娆¤捣涓や釜绾跨▼鍘诲畬鎴愪笅杞戒换鍔★紝鐒跺悗濡傛灉涓や釜鏍稿績绾跨▼浠诲姟娌$粨鏉熷悗缁换鍔′細闃诲绛夊緟锛屽彧鏈夊叾涓竴涓畬鎴愪簡鎵嶄細鎺ョ潃鎵ц銆?/p>

浠庝笂闈㈢殑鏍楀瓙涔熷彲浠ョ湅鍑猴紝褰撴垜浠妸鏍稿績绾跨▼淇敼鏄氨鍙互鑷繁瀹氫箟涓嬭浇鐨勮鍒欎簡锛屽钩甯告垜浠湅涓€浜汚ndroid甯傚満涓嬭浇杞欢鍘熺悊鐢ㄧ殑灏辨槸鑷繁鐨勭嚎绋嬫睜绛栫暐銆?/p>


鎬荤粨

Android鐨勭嚎绋嬫睜绛栫暐瀵规垜浠鐞嗗簲鐢ㄤ腑鐨勭嚎绋嬫槸涓€涓緢鏂逛究楂樻晥鐨勪簨锛屽湪鏃ュ父寮€鍙戜腑鎴戜滑鍙互鏍规嵁瀹為檯闇€瑕佸幓閰嶇疆绗﹀悎涓氬姟闇€姹傜殑绾跨▼姹犵瓥鐣ワ紝杩欐牱鍙互璁╁簲鐢ㄥ鐞嗘秹鍙婅€楁椂鐨勪换鍔℃椂鏇撮珮鏁堝拰鑺傜渷璧勬簮寮€閿€锛岃繖绡囨枃绔犳垜鏄緢鐢ㄥ績鐨勫啓浜嗭紝鍦ㄥ啓鐨勮繃绋嬩腑鎴戣嚜宸卞鏌愪簺鐭ヨ瘑鐐逛篃鏄湁涓€绉嶆亶鐒跺ぇ鎮熺殑鎰熻锛屾墍浠ユ垜寰堝笇鏈涜繖绡囨枃绔犲鎯充簡瑙h繖鏂归潰鐭ヨ瘑鐐圭殑浜烘湁鎵€甯姪锛屽鏋滄煇浜涘湴鏂逛笉浜嗚В鎴栬寰楀摢涓偣閿欎簡锛屽彲浠ヨ瘎璁?铏界劧涓嶄竴瀹氬強鏃剁湅鍒?.)浣嗘槸鏈夌枒闂垜涓€瀹氬敖鍔涘幓瑙g瓟(寰瑧鑴?銆?/p>


鏈€鍚庤櫧鐒舵墍鏈夌被閮藉湪鏂囩珷涓絾鏄繕鏄檮涓枃绔犳秹鍙婄殑婧愮爜锛屽彲鑳芥湁浜涚瓛瀛愭槸鐩存帴鐪嬫簮鐮佺殑(姣斿鎴?鈥?

骞冲父涓€鐩寸敤svn绠$悊浠g爜锛屽墠涓嶄箙鍒氬浼氭€庝箞浣跨敤git(鎰熻鑷繁鏈夌偣鍦熼€肩殑鎰熻)锛屾墍浠ヨ繖娆$洿鎺ユ妸婧愮爜浼犲埌github涓?鏈変簺鍚屽鍙兘涔犳儻鍦╟sdn涓婁笅杞斤紝鎵€浠ュ鎼炰簡涓猚sdn閾炬帴锛屾垜灏辨槸杩欎箞浜烘€у寲(楠勫偛鑴?..骞寸邯澶т簡鏈変簺纰庣鍢翠簡..

github鍦板潃:http://github.com/songjiran/ThreadPoolDemo

csdn鍦板潃锛歨ttp://download.csdn.net/detail/lxzmmd/9535019

杞浇璇锋敞鏄庡嚭澶?a href="http://nbafans365.com/">缁忔祹绠$悊缃?/a> » android绾跨▼姹犵鐞?/a>