工作中禁止使用Executors快捷創(chuàng)建線程池原理詳解_第1頁(yè)
工作中禁止使用Executors快捷創(chuàng)建線程池原理詳解_第2頁(yè)
工作中禁止使用Executors快捷創(chuàng)建線程池原理詳解_第3頁(yè)
工作中禁止使用Executors快捷創(chuàng)建線程池原理詳解_第4頁(yè)
工作中禁止使用Executors快捷創(chuàng)建線程池原理詳解_第5頁(yè)
已閱讀5頁(yè),還剩2頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(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ì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論