版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
第工作中禁止使用Executors快捷創(chuàng)建線程池原理詳解目錄問(wèn)題?1.1newFixedThreadPool的潛在問(wèn)題1.2newSingleThreadExecutor的潛在問(wèn)題?1.3newCachedThreadPool的潛在問(wèn)題1.4newScheduledThreadPool潛在問(wèn)題1.5總結(jié)
問(wèn)題?
在很多公司(如阿里、華為等)的編程規(guī)范中,非常明確地禁止使用Executors快捷創(chuàng)建線程池,為什么呢?這里從源碼講起,介紹使用Executors工廠方法快捷創(chuàng)建線程池將會(huì)面臨的潛在問(wèn)題。
1.1newFixedThreadPool的潛在問(wèn)題
基本使用
//線程池
ExecutorServicesingleThreadExecutor=Executors.newFixedThreadPool(2);
//批量添加線程
for(inti=0;ii++){
singleThreadExecutor.execute(newTargetTask());
//singleThreadExecutor.submit(newTargetTask());
Thread.sleep(1000);
//線程池銷毀
singleThreadExecutor.shutdown();;
查看源碼
publicstaticExecutorServicenewFixedThreadPool(intnThreads){
returnnewThreadPoolExecutor(nThreads,nThreads,
0L,TimeUnit.MILLISECONDS,
newLinkedBlockingQueueRunnable
*Createsa{@codeLinkedBlockingQueue}withacapacityof
*{@linkInteger#MAX_VALUE}.
publicLinkedBlockingQueue(){
this(Integer.MAX_VALUE);
我們可以看出:
corePoolSize(核心線程數(shù))=maximumPoolSize(最大線程數(shù))。LinkedBlockingQueue是一個(gè)無(wú)界隊(duì)列,如果提交的任務(wù)過(guò)快會(huì)造成任務(wù)大量的的堆積,消耗完服務(wù)器資源。如果隊(duì)列很大,很有可能導(dǎo)致JVM出現(xiàn)OOM(OutOfMemory)異常,即內(nèi)存資源耗盡。
1.2newSingleThreadExecutor的潛在問(wèn)題?
基本使用
//線程池
ExecutorServicesingleThreadExecutor=Executors.newSingleThreadExecutor();
//批量添加線程
for(inti=0;ii++){
singleThreadExecutor.execute(newTargetTask());
//singleThreadExecutor.submit(newTargetTask());
Thread.sleep(1000);
//線程池銷毀
singleThreadExecutor.shutdown();;
查看源碼
publicstaticExecutorServicenewSingleThreadExecutor(){
returnnewFinalizableDelegatedExecutorService
(newThreadPoolExecutor(1,1,
0L,TimeUnit.MILLISECONDS,
newLinkedBlockingQueueRunnable()));
*Createsa{@codeLinkedBlockingQueue}withacapacityof
*{@linkInteger#MAX_VALUE}.
publicLinkedBlockingQueue(){
this(Integer.MAX_VALUE);
嘗試修改核心線程數(shù)
packageExecutorDemo.newSingleThreadExecutor;
importjava.util.concurrent.ExecutorService;
importjava.util.concurrent.Executors;
importjava.util.concurrent.ThreadPoolExecutor;
*@description:
*@author:shu
*@createDate:2025/11/110:45
*@version:1.0
publicclassUpdateSingleThreadExecutor{
publicstaticvoidmain(String[]args){
//創(chuàng)建一個(gè)固定大小的線程池
ExecutorServicefixedExecutorService=
Executors.newFixedThreadPool(1);
ThreadPoolExecutorthreadPoolExecutor=
(ThreadPoolExecutor)fixedExecutorService;
System.out.println(threadPoolExecutor.getMaximumPoolSize());
//設(shè)置核心線程數(shù)
threadPoolExecutor.setCorePoolSize(8);
//創(chuàng)建一個(gè)單線程化的線程池
ExecutorServicesingleExecutorService=
Executors.newSingleThreadExecutor();
//轉(zhuǎn)換成普通線程池,會(huì)拋出運(yùn)行時(shí)異常java.lang.ClassCastException
((ThreadPoolExecutor)singleExecutorService).setCorePoolSize(8);
我們可以看出:
單例存在,我們無(wú)法去修改核心線程數(shù),否則會(huì)造成異常處理。corePoolSize(核心線程數(shù))=maximumPoolSize(最大線程數(shù))=1。LinkedBlockingQueue是一個(gè)無(wú)界隊(duì)列,如果提交的任務(wù)過(guò)快會(huì)造成任務(wù)大量的的堆積,消耗完服務(wù)器資源。如果隊(duì)列很大,很有可能導(dǎo)致JVM出現(xiàn)OOM(OutOfMemory)異常,即內(nèi)存資源耗盡。
1.3newCachedThreadPool的潛在問(wèn)題
基本使用
//線程池
ExecutorServicesingleThreadExecutor=Executors.newCachedThreadPool();
//批量添加線程
for(inti=0;ii++){
singleThreadExecutor.execute(newTargetTask());
//singleThreadExecutor.submit(newTargetTask());
Thread.sleep(1000);
//線程池銷毀
singleThreadExecutor.shutdown();;
源碼分析
publicstaticExecutorServicenewCachedThreadPool(){
returnnewThreadPoolExecutor(0,Integer.MAX_VALUE,
60L,TimeUnit.SECONDS,
newSynchronousQueueRunnable
*Createsa{@codeSynchronousQueue}withnonfairaccesspolicy.
publicSynchronousQueue(){
this(false);
ThreadPoolExecutor標(biāo)準(zhǔn)構(gòu)造器創(chuàng)建一個(gè)核心線程數(shù)為0、最大線程數(shù)不設(shè)限制的線程池理論上可緩存線程池可以擁有無(wú)數(shù)個(gè)工作線程,即線程數(shù)量幾乎無(wú)限制??删彺婢€程池的workQueue為SynchronousQueue同步隊(duì)列,這個(gè)隊(duì)列類似于一個(gè)接力棒,入隊(duì)出隊(duì)必須同時(shí)傳遞,正因?yàn)榭删彺婢€程池可以無(wú)限制地創(chuàng)建線程,不會(huì)有任務(wù)等待,所以才使用SynchronousQueue。但是,maximumPoolSize的值為Integer.MAX_VALUE(非常大),可以認(rèn)為可以無(wú)限創(chuàng)建線程,如果任務(wù)提交較多,就會(huì)造成大量的線程被啟動(dòng),很有可能造成OOM異常,甚至導(dǎo)致CPU線程資源耗盡。
1.4newScheduledThreadPool潛在問(wèn)題
基本使用
//線程池
ScheduledExecutorServiceservice=Executors.newScheduledThreadPool(2);
//批量添加線程
for(inti=0;ii++){
ScheduledFuturefuture=service.scheduleWithFixedDelay(newTargetTask(),0,500,TimeUnit.MILLISECONDS);
Thread.sleep(1000);
//線程池銷毀
service.shutdown();;
源碼分析
publicScheduledThreadPoolExecutor(intcorePoolSize){
super(corePoolSize,Integer.MAX_VALUE,0,NANOSECONDS,
newDelayedWorkQueue());
staticclassDelayedWorkQueueextendsAbstractQueueRunnable
implementsBlockingQueueRunnable{
privatestaticfinalintINITIAL_CAPACITY=16;
privateRunnableScheduledFuture[]queue=
newRunnableScheduledFuture[INITIAL_CAPACITY];
privatefinalReentrantLocklock=newReentrantLock();
privateintsize=0;
privateThreadleader=null;
privatefinalConditionavailable=lock.newCondition();
maximumPoolSize為Integer.MAX_VALUE,表示線程數(shù)不設(shè)上限,其workQueue為一
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 校園食品安全管理制度
- 罕見(jiàn)腫瘤的腫瘤異質(zhì)性分析
- 2026天津中醫(yī)藥大學(xué)招聘58人備考題庫(kù)及參考答案詳解一套
- 2026廣東廣州大學(xué)招聘事業(yè)編制輔導(dǎo)員12人備考題庫(kù)(第一次)及答案詳解一套
- 2026天津市武清區(qū)“一區(qū)五園”面向社會(huì)招聘國(guó)企工作人員24人備考題庫(kù)及完整答案詳解
- 2026華東交通大學(xué)海外優(yōu)青項(xiàng)目全球引才備考題庫(kù)(含答案詳解)
- 同興會(huì)計(jì)事務(wù)所財(cái)務(wù)制度
- 佛協(xié)財(cái)務(wù)制度細(xì)則
- 汽車美容快修財(cái)務(wù)制度
- 農(nóng)村村委財(cái)務(wù)制度
- 2025-2026學(xué)年北京市昌平區(qū)高三(上期)期末考試英語(yǔ)試卷(含答案)
- 交通運(yùn)輸安全檢查與處理規(guī)范(標(biāo)準(zhǔn)版)
- UCL介紹教學(xué)課件
- 扁鵲凹凸脈法課件
- 2026年開(kāi)封大學(xué)單招職業(yè)適應(yīng)性測(cè)試題庫(kù)及完整答案詳解1套
- 北京市2025北京市體育設(shè)施管理中心應(yīng)屆畢業(yè)生招聘2人筆試歷年參考題庫(kù)典型考點(diǎn)附帶答案詳解(3卷合一)2套試卷
- 建筑施工現(xiàn)場(chǎng)材料采購(gòu)流程
- DB31∕T 1234-2020 城市森林碳匯計(jì)量監(jiān)測(cè)技術(shù)規(guī)程
- 園林綠化施工工藝及注意事項(xiàng)
- 2025年高中語(yǔ)文必修上冊(cè)《登泰山記》文言文對(duì)比閱讀訓(xùn)練(含答案)
- 2025年金蝶AI蒼穹平臺(tái)新一代企業(yè)級(jí)AI平臺(tái)報(bào)告-
評(píng)論
0/150
提交評(píng)論