Java線程池Executor怎么使用_第1頁
Java線程池Executor怎么使用_第2頁
Java線程池Executor怎么使用_第3頁
Java線程池Executor怎么使用_第4頁
Java線程池Executor怎么使用_第5頁
已閱讀5頁,還剩1頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

第Java線程池Executor怎么使用使用Executors創(chuàng)建線程池

使用Executors可以創(chuàng)建四種線程池:分別對應(yīng)上邊提到的四種線程池初始化方法

Executors.newCachedThreadPool

newCachedThreadPool是一個根據(jù)需要創(chuàng)建新線程的線程池,當(dāng)一個任務(wù)提交時,corePoolSize為0不創(chuàng)建核心線程,SynchronousQueue是一個不存儲元素的隊列,可以理解為隊里永遠是滿的,因此最終會創(chuàng)建非核心線程來執(zhí)行任務(wù)。對于非核心線程空閑60s時將被回收。因為Integer.MAX_VALUE非常大,可以認為是可以無限創(chuàng)建線程的,在資源有限的情況下容易引起OOM異常。

//創(chuàng)建newCachedThreadPool線程池源碼

publicstaticExecutorServicenewCachedThreadPool(){

*corePoolSize:0,核心線程池的數(shù)量為0

*maximumPoolSize:Integer.MAX_VALUE,可以認為最大線程數(shù)是無限的

*keepAliveTime:60L

*unit:秒

*workQueue:SynchronousQueue

returnnewThreadPoolExecutor(0,Integer.MAX_VALUE,

60L,TimeUnit.SECONDS,

newSynchronousQueueRunnable

}

使用案例:

publicstaticvoidmAIn(String[]args){

ExecutorServiceexecutor=Executors.newCachedThreadPool();

for(inti=0;ii++){

finalintindex=i;

executor.execute(newRunnable(){

@Override

publicvoidrun(){

(task:{},index);

}

值得注意的一點是,newCachedThreadPool的返回值是ExecutorService類型,該類型只包含基礎(chǔ)的線程池方法,但卻不包含線程監(jiān)控相關(guān)方法,因此在使用返回值為ExecutorService的線程池類型創(chuàng)建新線程時要考慮到具體情況。

Executors.newSingleThreadExecutor

newSingleThreadExecutor是單線程線程池,只有一個核心線程,用唯一的一個共用線程執(zhí)行任務(wù),保證所有任務(wù)按指定順序執(zhí)行(FIFO、優(yōu)先級hellip;)

//newSingleThreadExecutor創(chuàng)建線程池源碼

publicstaticExecutorServicenewSingleThreadExecutor(){

*corePoolSize:1,核心線程池的數(shù)量為1

*maximumPoolSize:1,只可以創(chuàng)建一個非核心線程

*keepAliveTime:0L

*unit=秒

*workQueue=LinkedBlockingQueue

returnnewFinalizableDelegatedExecutorService

(newThreadPoolExecutor(1,1,

0L,TimeUnit.MILLISECONDS,

newLinkedBlockingQueueRunnable()));

}

當(dāng)一個任務(wù)提交時,首先會創(chuàng)建一個核心線程來執(zhí)行任務(wù),如果超過核心線程的數(shù)量,將會放入隊列中,因為LinkedBlockingQueue是長度為Integer.MAX_VALUE的隊列,可以認為是無界隊列,因此往隊列中可以插入無限多的任務(wù),在資源有限的時候容易引起OOM異常,同時因為無界隊列,maximumPoolSize和keepAliveTime參數(shù)將無效,壓根就不會創(chuàng)建非核心線程。

Executors.newFixedThreadPool

定長線程池,核心線程數(shù)和最大線程數(shù)由用戶傳入,可以設(shè)置線程的最大并發(fā)數(shù),超出在隊列等待

//newFixedThreadPool創(chuàng)建線程池源碼

publicstaticExecutorServicenewFixedThreadPool(intnThreads){

*corePoolSize:核心線程的數(shù)量為自定義輸入nThreads

*maximumPoolSize:最大線程的數(shù)量為自定義輸入nThreads

*keepAliveTime:0L

*unit:秒

*workQueue:LinkedBlockingQueue

returnnewThreadPoolExecutor(nThreads,nThreads,

0L,TimeUnit.MILLISECONDS,

newLinkedBlockingQueueRunnable

}

newFixedThreadPool和SingleThreadExecutor類似,唯一的區(qū)別就是核心線程數(shù)不同,并且由于使用的是LinkedBlockingQueue,在資源有限的時候容易引起OOM異常。

Executors.newScheduledThreadPool

定長線程池,核心線程數(shù)由用戶傳入,支持定時和周期任務(wù)執(zhí)行

//newScheduledThreadPool創(chuàng)建線程池源碼

publicstaticScheduledExecutorServicenewScheduledThreadPool(intcorePoolSize){

returnnewScheduledThreadPoolExecutor(corePoolSize);

publicScheduledThreadPoolExecutor(intcorePoolSize){

*corePoolSize:核心線程的數(shù)量為自定義輸入corePoolSize

*maximumPoolSize:最大線程的數(shù)量為Integer.MAX_VALUE

*keepAliveTime:0L

*unit:納秒

*workQueue:DelayedWorkQueue

super(corePoolSize,Integer.MAX_VALUE,0,NANOSECONDS,

newDelayedWorkQueue());

}

當(dāng)一個任務(wù)提交時,corePoolSize為自定義輸入,首先創(chuàng)建核心線程,核心線程滿了之后,因此最終會創(chuàng)建非核心線程來執(zhí)行任務(wù)。非核心線程使用后將被回收。因為Integer.MAX_VALUE非常大,可以認為是可以無限創(chuàng)建線程的,在資源有限的情況下容易引起OOM異常。因為使用的DelayedWorkQueue可以實現(xiàn)定時和周期任務(wù)。ScheduledExecutorService提供了三種方法可以使用:

schedule:延遲后執(zhí)行任務(wù)scheduleAtFixedRate:以指定的速率執(zhí)行任務(wù)scheduleWithFixedDelay:以指定的延遲執(zhí)行任務(wù)使用案例:

publicstaticvoidmain(String[]args){

ScheduledExecutorServiceexecutorService=Executors.newScheduledThreadPool(1);

//executorService.schedule(newRunnable(){

//@Override

//publicvoidrun(){

//log.warn(schedulerun

////延遲3秒后執(zhí)行

//},3,TimeUnit.SECONDS);

//executorService.shutdown();

//executorService.scheduleWithFixedDelay(newRunnable(){

//@Override

//publicvoidrun(){

//log.warn(scheduleWithFixedDelayrun

////延遲一秒后每隔3秒執(zhí)行

//},1,3,TimeUnit.SECONDS);

executorService.scheduleAtFixedRate(newRunnable(){

@Override

publicvoidrun(){

log.warn(schedulerun

//延遲一秒后每隔3秒執(zhí)行

},1,3,TimeUnit.SECONDS);

*定時器調(diào)度,不推薦使用,

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論