NO.29 鏈夊涔冨ぇ锛氱嚎绋嬫睜
Posted 浠g爜鑽h€€
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了NO.29 鏈夊涔冨ぇ锛氱嚎绋嬫睜相关的知识,希望对你有一定的参考价值。
杩欐槸绋嬪簭鍛楥hatbook绗?span>83绡囧師鍒?/span>
璺濈2018骞磋繕鏈?/span>4澶?/span>
浠婃棩闅惧害绯绘暟 锛氿煂燄煂燄煂?/p>
棰勮闃呰鏃堕棿 锛?nbsp;3 鍒嗛挓
鈻?/span>
00銆佷负浠€涔堣浣跨敤绾跨▼姹?- Why
澶氱嚎绋嬬殑杞欢璁捐鏂规硶鍙互鏈€澶ч檺搴﹀湴鍙戞尌鐜颁唬澶氭牳澶勭悊鍣ㄧ殑璁$畻鑳藉姏锛屾彁楂樿蒋浠剁郴缁熻繍琛岀殑鍚炲悙閲忓拰鎬ц兘銆備絾鏄紝濡傛灉瀵圭嚎绋嬬殑浣跨敤涓嶅姞浠ユ帶鍒朵笌绠$悊锛岃繖鏍峰杞欢绯荤粺鐨勬€ц兘鍙嶈€屼細浜х敓涓嶅埄鐨勫奖鍝嶃€?br>
涓€绉嶆垜浠粡甯稿垱寤虹嚎绋嬬殑浣跨敤鍦烘櫙濡備唬鐮?鎵€绀猴細
1
new Thread(new Runnable(){
@Override
public void run() {
// TODO
}
}).start();
閫氳繃浠ヤ笂鏂规硶锛屾垜浠垱寤轰簡涓€涓嚎绋嬶紝骞跺湪run()鏂规硶缁撴潫鍚庯紝鑷姩鍥炴敹璇ョ嚎绋嬫墍鍗犵敤鐨勭郴缁熻祫婧愩€傚湪绠€鍗曠殑Demo鎴栧苟鍙戦噺姣旇緝浣庣殑鐜涓紝杩欐牱鐨勪娇鐢ㄧ嚎绋嬬殑鏂瑰紡鏄病鏈夐棶棰橈紝浣嗗湪鐪熷疄鐨勫鐢ㄦ埛銆侀珮骞跺彂鐨勪娇鐢ㄥ満鏅腑锛岄€氳繃涓婅堪浠g爜锛?/p>
涓€鏂归潰浠庢€ц兘寮€閿€鏉ョ湅锛岄渶瑕侀绻佸垱寤哄涓嚎绋嬶紝棰戠箒鍥炴敹绾跨▼鎵€鑰楄垂鐨勮祫婧愶紝鑰屽垱寤虹嚎绋嬪璞′笌鍥炴敹绾跨▼瀵硅薄閮借鏈夋椂闂村紑閿€锛?/p>
鍙﹀涓€鏂归潰浠庣┖闂村紑閿€鏉ョ湅锛岀敱浜庝笂杩颁唬鐮佹病鏈夊绾跨▼鐨勫垱寤鸿繘琛岄檺鍒讹紝褰撴捣閲忕敤鎴峰彂鍑鸿姹傦紝鍚庡彴鐢ㄦ埛鎵ц璇ヤ唬鐮佹椂锛屽氨浼氬垱寤烘捣閲忕敤鎴峰彂鍑鸿姹傦紝鍚庡彴鐢ㄦ埛鎵ц璇ヤ唬鐮佹椂锛屽氨浼氬垱寤烘捣閲忕嚎绋嬶紝杩涜€岃€楀敖鏈嶅姟鍣ㄦ湁闄愮殑鍐呭瓨涓嶤PU璧勬簮銆?/p>
涓烘锛屾垜浠渶瑕佺瀛﹀悎鐞嗗湴浣跨敤绾跨▼鎶€鏈€傚湪瀹為檯鐢熶骇鐜涓紝绾跨▼鐨勬暟閲忓繀椤诲緱鍒版帶鍒讹紝鐩茬洰鐨勫ぇ閲忓垱寤虹嚎绋嬪绯荤粺鎬ц兘鏄湁鎹熷潖鐨勩€?/p>
01銆佷粈涔堟槸绾跨▼姹?nbsp;- What
鎴戜滑鍙互鍒╃敤绾跨▼姹犳妧鏈紝澶嶇敤宸插垱寤虹殑绾跨▼锛岃繘鑰屾湁鏁堝湴閬垮厤杞欢绯荤粺棰戠箒鐨勫垱寤哄拰閿€姣佺嚎绋嬨€?/p>
鎵€璋撶嚎绋嬫睜锛屽氨鏄垜浠湪绋嬪簭杩愯鐨勫垵濮嬮樁娈碉紝棣栧厛鍒涘缓濂戒竴瀹氭暟閲忕殑绾跨▼锛堣繖浜涘垱寤哄ソ鐨勭嚎绋嬭褰㈣薄鍖栫О涓虹嚎绋嬫睜锛夛紝褰撶▼搴忛渶瑕佷娇鐢ㄧ嚎绋嬫椂锛屼笉鍐嶅崟鐙垱寤虹嚎绋嬶紝鑰屾槸鐩存帴浠庣嚎绋嬫睜鑾峰緱绌洪棽绾跨▼锛涘綋绾跨▼鎵ц瀹屾垚鏃讹紝骞朵笉鐪熸鐨勯攢姣佺嚎绋嬶紝鑰屾槸鎶婄嚎绋嬪綊鍏ョ嚎绋嬫睜锛屽苟璁剧疆涓虹┖闂茬姸鎬併€?/p>
绠€瑷€涔嬶紝閲囩敤绾跨▼姹犲悗锛屽垱寤虹嚎绋嬪彉鎴愪簡浠庣嚎绋嬫睜涓幏寰楃┖闂茬嚎绋嬶紝鍏抽棴绾跨▼鍙樻垚浜嗗悜绾跨▼姹犲綊杩樼嚎绋嬨€?/p>
02銆佸浣曚娇鐢ㄧ嚎绋嬫睜 - How
鎴戜滑鍙互鍒╃敤绾跨▼姹犳妧鏈紝澶嶇敤宸插垱寤虹殑绾跨▼锛岃繘鑰屾湁鏁堝湴閬垮厤杞欢绯荤粺棰戠箒鐨勫垱寤哄拰閿€姣佺嚎绋嬨€?/p>
绾跨▼姹犳槸涓€涓ぇ瀹舵棌锛屾棤娉曞湪涓€绡囨枃绔犱腑璇存槑鍏跺叏閮ㄧ殑鐢ㄦ硶涓庝娇鐢ㄥ満鏅紝浣嗘槸涓囧彉涓嶇鍏朵腑锛屼竾璞℃€绘湁鏍规湰锛屼粖澶╂垜浠鍏堟潵璋堣皥绾跨▼姹犱腑鐨勪竴涓牳蹇冩妧鏈紝杩欎釜鎶€鏈疄鐜版槸鍏朵粬绾跨▼姹犲疄鐜扮殑鍩虹鈥斺€擳hreadPoolExecutor銆?/p>
2
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler);
浠g爜2涓槸ThreadPoolExecutor鐨勬瀯閫犲嚱鏁帮紝鍏跺弬鏁板惈涔夊涓嬶細
corePoolSize锛氭寚瀹氫簡绾跨▼姹犱腑鐨勭嚎绋嬫暟閲忥紱
maximumPoolSize锛氭寚瀹氫簡绾跨▼姹犱腑鐨勬渶澶х嚎绋嬫暟閲忥紱
keepAliveTime锛氬綋绾跨▼姹犵嚎绋嬫暟閲忚秴杩嘽orePoolSize鏃讹紝澶氫綑鐨勭┖闂茬嚎绋嬬殑瀛樻椿鏃堕棿銆傚嵆锛氳秴杩嘽orePoolSize鐨勭┖闂茬嚎绋嬶紝鍦ㄥ娆℃椂闂村唴锛屼細琚攢姣侊紱
unit锛歬eepAliveTime鐨勫崟浣嶏紱
workQueue锛氫换鍔¢槦鍒楋紝琚彁浜や絾灏氭湭琚墽琛岀殑浠诲姟锛?/p>
threadFactory锛氱嚎绋嬪伐鍘傦紝鐢ㄤ簬鍒涘缓绾跨▼锛屼竴鑸敤榛樿鐨勫嵆鍙紱
handler锛氭嫆缁濈瓥鐣ャ€傚綋浠诲姟澶鏉ヤ笉鍙婂鐞嗭紝濡備綍鎷掔粷浠诲姟銆?/p>
鍏朵腑锛屽弬鏁皐orkQueue鏄鎻愪氦浣嗚繕鏈鎵ц鐨勪换鍔¢槦鍒楋紝瀹冩槸涓€涓狟lockingQueue鎺ュ彛鐨勫璞★紝浠呯敤浜庡瓨鏀綬unnable瀵硅薄銆傚畠鍙互鏄互涓嬫儏鍐典腑鐨勪竴绉嶏細
鐩存帴鎻愪氦鐨勯槦鍒楋紝璇ュ姛鑳芥湁SynchronousQueue瀵硅薄鎻愪緵銆傚埄鐢ㄨ瀵硅薄鎻愪氦鐨勪换鍔′笉浼氳鐪熷疄鐨勪繚瀛橈紝鑰屾€绘槸灏嗘柊浠诲姟鎻愪氦缁欑嚎绋嬫墽琛岋紝濡傛灉娌℃湁绌洪棽鐨勭嚎绋嬶紝鍒欏皾璇曞垱寤烘柊鐨勭嚎绋嬶紝濡傛灉绾跨▼鏁伴噺宸茬粡杈惧埌鏈€澶у€硷紝鍒欐墽琛屾嫆缁濈瓥鐣ャ€傚父闇€閰嶅悎璁剧疆maximumPoolSize锛屽惁鍒欏緢瀹规槗鎵ц鎷掔粷绛栫暐銆?/p>
鏈夌晫闃熷垪锛岃鍔熻兘鐢盇rrayBlockingQueue瀵硅薄鎻愪緵锛岃瀵硅薄鐨勬瀯閫犲嚱鏁板繀椤诲甫涓€涓閲忓弬鏁帮紝鐢ㄤ簬璁剧疆璇ラ槦鍒楃殑鏈€澶у閲忋€傚埄鐢ㄨ瀵硅薄鎻愪氦浠诲姟鏃讹紝濡傛灉绾跨▼姹犵殑瀹為檯绾跨▼灏忎簬corePoolSize锛屽垯浼氫紭鍏堝垱寤烘柊鐨勭嚎绋嬶紝鑻ュぇ浜巆orePoolSize锛屽垯浼氬皢鏂颁换鍔″姞鍏ョ瓑寰呴槦鍒椼€傚鏋滅瓑寰呴槦鍒楀凡婊★紝鏃犳硶鍔犲叆锛屽垯鍦ㄦ€荤嚎绋嬫暟涓嶈秴杩噈aximumPoolSize鐨勫墠鎻愪笅锛屽垱寤烘柊绾跨▼浠诲姟锛涘鏋滃ぇ浜巑aximumPoolSize锛屽垯鎵ц鎷掔粷绛栫暐銆?/p>
鏃犵晫浠诲姟闃熷垪锛岃鍔熻兘鐢盠inkedBlockingQueue绫诲疄鐜般€備笌鏈夌晫闃熷垪鐩告瘮锛岄櫎闈炵郴缁熻祫婧愯€楀敖锛屽惁鍒欐棤鐣岀殑浠诲姟闃熷垪涓嶅瓨鍦ㄤ换鍔″叆闃熷垪澶辫触鐨勬儏鍐点€傚綋鏈夋柊鐨勪换鍔℃彁浜ゆ椂锛岀郴缁熺殑绾跨▼鏁板皬浜巆orePoolSize鏃讹紝绾跨▼姹犱細鐢熸垚鏂扮殑绾跨▼鎵ц浠诲姟锛屼絾鏄綋绯荤粺鐨勭嚎绋嬫暟杈惧埌corePoolSize鍚庯紝灏变笉浼氱户缁鍔犮€傝嫢鍚庣画浠嶇劧鏈夋柊鐨勪换鍔″姞鍏ワ紝鑰屽張娌℃湁绌洪棽鐨勭嚎绋嬭祫婧愶紝鍒欎换鍔$洿鎺ヨ繘鍏ラ槦鍒楃瓑寰呫€?/p>
浠诲姟浼樺厛闃熷垪锛岃鍔熻兘鐢盤riorityBlockingQueue瀹炵幇锛屽彲浠ョ‘淇濋珮浼樺厛绾х殑浠诲姟鍏堟墽琛屻€?/p>
2
//鍒╃敤绾跨▼姹犳妧鏈ā鎷熸墜鏈哄彂閫侀獙璇佺爜鐨勫満鏅?/span>
public class PhoneMsgSender {
//鏋勫缓涓€涓嚎绋嬫睜
private static final ExecutorService exeutor =
new ThreadPoolExecutor(
1,
Runtime.getRuntime().availableProcessors(),
60,
TimeUnit.SECONDS,
new SynchronousQueue<Runnable>(),
new ThreadFactory(){
public Thread newThread(Runnable r){
Thread t = new Thread(r,"PhoneMsgSender");
t.setDaemon(true);
return t;
}
}, new ThreadPoolExecutor.DiscardPolicy());
public void sendPhoneMsg(final String phoneNumber){Runable task = new Runnable(){
public void run(){
doSend(phoneNumber);
}
};
//鎻愪氦鏂颁换鍔?/span>
exeutor.submit(task);
}
//鍏蜂綋鎵ц鍙戦€佷俊鎭换鍔?/span>
public void doSend(String phoneNumber){
System.out.println("Sending msg : " + phoneNumber);
//Todo
}
}
鍒掗噸鐐?/strong>
杈冨鏉傜殑鍦烘櫙锛屼竴鑸埄鐢ㄧ嚎绋嬫睜鏉ュ疄鐜板绾跨▼锛?/strong>
鍒涘缓绾跨▼姹犳椂锛屼竴瀹氳鏍规嵁鍏蜂綋鐨勪笟鍔′娇鐢ㄥ満鏅潵閫夋嫨workQueue鐨勫疄鐜版柟寮忥紱
銆愬弬鑰冭祫鏂欍€?/span>
1 Java澶氱嚎绋嬬紪绋嬪疄鎴樻寚鍗楋紝 榛勬枃娴凤紝 涓浗宸ヤ俊鍑虹増闆嗗洟銆?/span>
2 瀹炴垬Java楂樺苟鍙戠▼搴忚璁★紝 钁涗竴楦o紝閮秴锛?涓浗宸ヤ俊鍑虹増闆嗗洟锛岀數瀛愬伐涓氬嚭鐗堢ぞ銆?/span>
鏈夋晥鍦板埄鐢ㄦ偍鐨勭鐗囧寲鏃堕棿锛屾瘡鏃ョ簿杩涳紱鐙蹇紝浼楄杩滐紒鎯充簡瑙f洿澶氳蒋浠跺紑鍙戞妧鏈笌浜虹敓鎰熸偀鍒嗕韩锛岃闀挎寜浜岀淮鐮佸浘鐗囷紝鍏虫敞绋嬪簭鍛榗hatbook銆傚鏋滆寰楁枃绔犳湁鐢紝鍚屾椂娆㈣繋骞舵劅璋㈡偍鑳芥妸鏂囩珷鍒嗕韩缁欐湁闇€瑕佺殑浼欎即锛屽ぇ瀹跺叡鍚岃繘姝ャ€?/p>
绋嬪簭鍛楥hatbook 绋嬪簭鍛橀兘鍏虫敞浜嗭紝鏉ヤ笉鍙婅В閲婏紝闀挎寜鍥剧墖锛屽揩涓婅溅 姣忔棩涓€鍙?/strong> 鏃╀笉璧疯兘璇竴澶╀簨锛屽皯涓嶅鑳借涓€鐢熶簨銆?/p>
鈥斺€旇挋鍙よ皻璇?/p>
鏈枃寤朵几闃呰 鐭ヨ瘑鐐?锛欽ava鍐呭瓨妯″瀷 鐭ヨ瘑鐐?锛氱紪鍐欓珮骞跺彂绋嬪簭鐨勨€滄纭Э鍔库€?/p>
鐭ヨ瘑鐐?锛氱被銆佹娊璞$被銆佹帴鍙g殑鍖哄埆鍙婂簲鐢ㄥ満鏅?/p>
鐭ヨ瘑鐐?锛氬唴閮ㄧ被鐨勭被鍨嬨€佸疄鐜版柟寮忓強搴旂敤鍦烘櫙 鎺ㄨ崘1锛?/p>
鎺ㄨ崘2锛?/p>
以上是关于NO.29 鏈夊涔冨ぇ锛氱嚎绋嬫睜的主要内容,如果未能解决你的问题,请参考以下文章