Posted 浠g爜鑽h€€
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了相关的知识,希望对你有一定的参考价值。
璺濈2018骞磋繕鏈?/span>3澶?/span>
浠婃棩闅惧害绯绘暟 锛氿煂燄煂燄煂?/p>
棰勮闃呰鏃堕棿 锛?nbsp;3 鍒嗛挓
鈻?/span>
00銆佷粠涓€涓緥瀛愯皥璧?/strong>
鏃堕棿锛氫负浜嗚兘鏇村姞璇存槑闂锛岃鎴戜滑鎶婃椂闂撮€€鍥炲埌10骞村墠锛岄偅涓椂鍊欒繕娌℃湁鎵€璋撶殑鎺掗槦鍙彿鏈嶅姟锛屼负浜嗗姙鐞嗘煇绉嶄笟鍔¤繕闇€瑕佹帓闃熺殑閭d釜骞翠唬銆?/p>
鍦烘櫙锛氭煇鍦扮殑鏌愰摱琛岃惀涓氬巺鏌滃彴寮€浜?涓獥鍙?/strong>锛屾瘡涓獥鍙i噷闈㈤兘鍧愪簡涓€浣嶅伐浣滀汉鍛橈紱褰撳偍钃勭敤鎴锋潵鍒伴摱琛屽ぇ鍘呭悗锛岀湅瑙佹湁绐楀彛鏄┖闂茬殑锛岄偅涔堝氨鍙互鐩存帴鍓嶅線绌洪棽鐨勭獥鍙e幓鍔炵悊涓氬姟锛涙參鎱㈠湴锛屽埌閾惰鍔炵悊鍌ㄨ搫涓氬姟鐨勪汉瓒婃潵瓒婂浜嗭紝8涓獥鍙i兘鍦ㄥ澶栨彁渚涙湇鍔★紱杩欎釜鏃跺€欙紝鏂板埌鐨勫鎴峰氨鍙兘杩涜鎺掗槦銆?/p>
濡傛灉鏂版潵瀹㈡埛鐨勫闀块€熷害杩滆繙澶т簬閾惰宸ヤ綔浜哄憳瀹屾垚涓€椤瑰鎴蜂笟鍔$殑閫熷害锛岃繖鏍峰鎴锋帓闃熷氨浼氳秺绉秺澶氥€傝繖鏃跺€欙紝閾惰鐨勫ぇ鍫傜粡鐞嗗彲鑳戒細澧炲紑2涓复鏃剁獥鍙?/strong>涓烘帓闃熺殑瀹㈡埛鎻愪緵鏈嶅姟銆?/p>
閭g煡閬撲粖澶╂槸闄ゅ锛岃繃鏉ュ姙鐞嗗偍钃勪笟鍔″鎴疯秺鏉ヨ秺澶氾紝10锛?+2锛変釜绐楀彛涔熶笉澶熺敤浜嗭紝杩欎釜鏃跺€欏ぇ鍫傜粡鐞嗗彲鑳藉氨瑕佽€冭檻涓嶅啀鎺ユ敹鏂板鎴锋垨瑙勫姖锛?span>鎶涘純锛夌幇鍦ㄦ帓闃熺殑瀹㈡埛鍘诲氨杩戠殑閾惰钀ヤ笟鍘呭幓鍔炵悊涓氬姟浜嗐€?/p>
鏃堕棿鍒颁簡涓嬪崍锛岃繃鏉ュ姙鐞嗗偍钃勪笟鍔$殑瀹㈡埛瓒婃潵瓒婂皯浜嗭紙鍙兘澶у閮藉幓鍑嗗骞村楗簡锛夛紝澶у爞缁忕悊鍙戠幇鏂板鐨?涓复鏃剁獥鍙g┖闂插悗锛屽張绛夊緟瑙傚療浜?0鍒嗛挓鍚庯紝鍙戠幇绌洪棽渚濇棫锛屽彲鑳藉氨瑕佽€冭檻鎶婁箣鍓嶇殑2涓复鏃剁獥鍙e叧闂?/strong>浜嗭紝鍙繚鐣欎箣鍓嶇殑8涓獥鍙c€?/p>
濂戒簡锛屼緥瀛愯瀹屼簡锛岃€侀搧浠竴瀹氭兂鍒颁簡锛屼笂杩扮殑渚嬪瓙鍙槸瀵圭嚎绋嬫睜宸ヤ綔杩囩▼鐨勪竴涓舰璞″寲姣斿柣銆備笅闈㈡垜浠ThreadPoolSize鍐嶆鐧诲満锛屽浠g爜1鎵€绀猴細 1 public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler); 浠g爜1涓槸ThreadPoolExecutor鐨勬瀯閫犲嚱鏁帮紝鍏跺弬鏁板惈涔夊涓嬶細 corePoolSize锛氭寚瀹氫簡绾跨▼姹犱腑鐨勭嚎绋嬫暟閲忥紱鎸囦唬鈥滀笂渚嬩腑鐨?涓獥鍙b€?/p> maximumPoolSize锛氭寚瀹氫簡绾跨▼姹犱腑鐨勬渶澶х嚎绋嬫暟閲忥紱鎸囦唬鈥滀笂渚嬩腑鐨?0涓獥鍙o紙8涓寮忕獥鍙?2涓柊澧炰复鏃剁獥鍙o級鈥?/p> keepAliveTime锛氬綋绾跨▼姹犵嚎绋嬫暟閲忚秴杩嘽orePoolSize鏃讹紝澶氫綑鐨勭┖闂茬嚎绋嬬殑瀛樻椿鏃堕棿銆傚嵆锛氳秴杩嘽orePoolSize鐨勭┖闂茬嚎绋嬶紝鍦ㄥ闀挎椂闂村唴锛屼細琚攢姣併€傛寚浠b€滃ぇ鍫傜粡鐞嗗彂鐜版柊澧炵殑2涓复鏃剁獥鍙g┖闂插悗锛屽張绛夊緟瑙傚療浜?0鍒嗛挓鈥濅腑鐨勬暟鍊?0 unit锛歬eepAliveTime鐨勫崟浣嶏紱鎸囦唬鈥滀笂渚嬩腑鐨?0鍒嗛挓鈥濅腑鐨勫垎閽?br> workQueue锛氫换鍔¢槦鍒楋紝琚彁浜や絾灏氭湭琚墽琛岀殑浠诲姟锛涙寚浠b€滀笂渚嬩腑鐨?0涓獥鍙b€?/p> threadFactory锛氱嚎绋嬪伐鍘傦紝鐢ㄤ簬鍒涘缓绾跨▼锛屼竴鑸敤榛樿鐨勫嵆鍙紱鍚庣画浼氫笓棰樿璁?/p> handler锛氭嫆缁濈瓥鐣ャ€傚綋浠诲姟澶鏉ヤ笉鍙婂鐞嗭紝濡備綍鎷掔粷浠诲姟锛涘悗缁細涓撻璁ㄨ 01銆佽繘涓€姝ュ垎鏋?/strong> 閫氳繃涓婁緥鐨勫満鏅唬鍏ワ紝鎴戜滑鍐嶅绾跨▼姹犺繘琛屾繁鍏ュ垎鏋愩€?/p>
绾跨▼姹犱腑鏈変袱涓牳蹇冨弬鏁帮紝寰堝鏄撳紕閿欙紝鎴戜滑鍐嶈鲸鏋愪竴涓嬶細 corePoolSize锛氳鍊艰瀹氬ソ浠ュ悗锛岄粯璁ゆ儏鍐典笅骞朵笉浼氬湪绾跨▼姹犱腑鍒涘缓浠讳綍绾跨▼锛岃€屾槸绛夊緟鏈変换鍔℃潵鏃舵墠寮€濮嬪垱寤虹嚎绋嬶紱鍙湁鏄惧紡璋冪敤浜唒restartAllCoreThreads鏂规硶鍚庯紝绾跨▼姹犳墠浼氶鍏堝垱寤篶orePoolSize涓嚎绋嬨€備篃灏辨槸璇达紝榛樿鎯呭喌涓嬪垱寤虹殑绾跨▼姹狅紝绾跨▼姹犱腑鐨勭嚎绋嬫暟涓?锛屽綋鏈変换鍔℃潵涔嬪悗锛屾墠浼氬垱寤轰互绾跨▼鍘绘墽琛屼换鍔★紝褰撶嚎绋嬫睜涓殑绾跨▼鏁扮洰杈惧埌corePoolSize鍚庯紝灏变細鎶婂埌杈剧殑浠诲姟鏀惧埌缂撳瓨闃熷垪褰撲腑銆?/p> maximumPoolSize锛氬畠琛ㄧず绾跨▼姹犱腑鏈€澶氳兘鍒涘缓澶氬皯涓嚎绋嬨€?/p> 杈惧埌锛坈orePoolSize+1锛夋暟鐩箣鍓嶇殑绾跨▼骞朵笉浼氳绾跨▼姹犻噴鏀鹃攢姣侊紝浣嗘槸浠庯紙corePoolSize+1锛墌 maximumPoolSize涔嬮棿鐨勫垱寤虹殑绾跨▼浼氳閲婃斁閿€姣併€?/p> 鏂囩珷鐨勬渶鍚庯紝鎴戜滑閫氳繃ThreadPoolExecutor鐨勬墽琛岃皟搴︾▼搴忔簮浠g爜锛屽垎鏋愪竴涓嬪悜绾跨▼姹犳彁浜や綔涓氱殑鏃跺€欙紝鍒板簳鍙戠敓浜嗕粈涔堬紝瑙佷唬鐮?锛涘浘1璇存槑浜嗙嚎绋嬫睜鐨勬墽琛岃繃绋嬨€?/p>
2 public void execute(Runnable command) { if (command == null) throw new NullPointerException(); int c = ctl.get(); //鍒╃敤workerCountOf鑾峰彇褰撳墠绾跨▼姹犵殑绾跨▼鎬绘暟 //濡傛灉灏忎簬corePoolSize锛岃皟鐢╝ddWorker璋冨害绾跨▼鎵ц if (workerCountOf(c) < corePoolSize) { if (addWorker(command, true)) return; c = ctl.get(); } //濡傛灉澶т簬corePoolSize锛屽垯璋冪敤workQueue.offer //灏嗕换鍔¤繘鍏ョ瓑寰呴槦鍒?/span> if (isRunning(c) && workQueue.offer(command)) { int recheck = ctl.get(); if (! isRunning(recheck) && remove(command)) reject(command); else if (workerCountOf(recheck) == 0) addWorker(null, false); } //濡傛灉杩涘叆绛夊緟闃熷垪澶辫触锛屽垯灏嗕换鍔℃彁浜ょ粰绾跨▼姹?/span> //濡傛灉绾跨▼姹犳暟鐩凡缁忚揪鍒癿aximumPoolSize锛屽垯鎵ц鎷掔粷绛栫暐 else if (!addWorker(command, false)) reject(command); } 鍥? 绾跨▼姹犳墽琛岃皟搴﹁繃绋?/p>
銆愬弬鑰冭祫鏂欍€?/span> 1 Java澶氱嚎绋嬬紪绋嬪疄鎴樻寚鍗楋紝 榛勬枃娴凤紝 涓浗宸ヤ俊鍑虹増闆嗗洟銆?/span> 2 瀹炴垬Java楂樺苟鍙戠▼搴忚璁★紝 钁涗竴楦o紝閮秴锛?涓浗宸ヤ俊鍑虹増闆嗗洟锛岀數瀛愬伐涓氬嚭鐗堢ぞ銆?/span> 鏈夋晥鍦板埄鐢ㄦ偍鐨勭鐗囧寲鏃堕棿锛屾瘡鏃ョ簿杩涳紱鐙蹇紝浼楄杩滐紒鎯充簡瑙f洿澶氳蒋浠跺紑鍙戞妧鏈笌浜虹敓鎰熸偀鍒嗕韩锛岃闀挎寜浜岀淮鐮佸浘鐗囷紝鍏虫敞绋嬪簭鍛榗hatbook銆傚鏋滆寰楁枃绔犳湁鐢紝鍚屾椂娆㈣繋骞舵劅璋㈡偍鑳芥妸鏂囩珷鍒嗕韩缁欐湁闇€瑕佺殑浼欎即锛屽ぇ瀹跺叡鍚岃繘姝ャ€?/p>
绋嬪簭鍛楥hatbook 绋嬪簭鍛橀兘鍏虫敞浜嗭紝鏉ヤ笉鍙婅В閲婏紝闀挎寜鍥剧墖锛屽揩涓婅溅 姣忔棩涓€鍙?/strong> 鏃╀笉璧疯兘璇竴澶╀簨锛屽皯涓嶅鑳借涓€鐢熶簨銆?/p>
鈥斺€旇挋鍙よ皻璇?/p>
鏈枃寤朵几闃呰 鐭ヨ瘑鐐?锛欽ava鍐呭瓨妯″瀷 鐭ヨ瘑鐐?锛氱紪鍐欓珮骞跺彂绋嬪簭鐨勨€滄纭Э鍔库€?/p>
鐭ヨ瘑鐐?锛氫粈涔堟槸绾跨▼姹?/p>
鐭ヨ瘑鐐?锛氬唴閮ㄧ被鐨勭被鍨嬨€佸疄鐜版柟寮忓強搴旂敤鍦烘櫙 鎺ㄨ崘1锛?/p>
鎺ㄨ崘2锛?/p>
以上是关于的主要内容,如果未能解决你的问题,请参考以下文章
TimeUnit unit,