程序員必會之最詳細(xì)的ThreadPoolExecutor 線程池七大參數(shù)含義
線程池的 7 大參數(shù)整理。
public ThreadPoolExecutor(
int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler)
corePoolSize 線程池中長期存活的線程數(shù)
ThreadPoolExecutor 在創(chuàng)建之初,是不會立即初始化corePoolSize數(shù)量的Thread的,而是通過外部request來一個(gè)一個(gè)的創(chuàng)建,當(dāng)達(dá)到corePoolSize數(shù)目之后,就會維持至少corePoolSize數(shù)目的Thread在pool中,哪怕他們都處于空閑狀態(tài)(idle)。corePoolSize >= 0。
maximumPoolSize 線程池中的最大線程數(shù)量
maximumPoolSize >= corePoolSize,maximumPoolSize>0
- 若當(dāng)前線程池中線程數(shù) < corePoolSize,則每來一個(gè)任務(wù)就創(chuàng)建一個(gè)線程去執(zhí)行。
- 若當(dāng)前線程池中線程數(shù) >= corePoolSize,會嘗試將任務(wù)添加到任務(wù)隊(duì)列。如果添加成功,則任務(wù)會等待空閑線程將其取出并執(zhí)行(針對的是有界隊(duì)列)。
- 若隊(duì)列已滿,且當(dāng)前線程池中線程數(shù) < maximumPoolSize,創(chuàng)建新的線程。
- 若當(dāng)前線程池中線程數(shù) >= maximumPoolSize,則會采用拒絕策略。
keepAliveTime 空閑線程存活時(shí)間
當(dāng)線程池線程數(shù)量超過corePoolSize時(shí),多余的空閑線程會在多長時(shí)間內(nèi)被銷毀。
銷毀的線程數(shù)=maximumPoolSize(最大線程數(shù))-corePoolSize(核心線程數(shù))。
TimeUnit 時(shí)間單位
空閑線程存活時(shí)間的描述單位,有以下選項(xiàng):
- TimeUnit.DAYS:天
- TimeUnit.HOURS:小時(shí)
- TimeUnit.MINUTES:分
- TimeUnit.SECONDS:秒
- TimeUnit.MILLISECONDS:毫秒
- TimeUnit.MICROSECONDS:微妙
- TimeUnit.NANOSECONDS:納秒
BlockingQueue
提交但未執(zhí)行的任務(wù)隊(duì)列,有以下選項(xiàng):
- LinkedBlockingQueue:用鏈表實(shí)現(xiàn)的隊(duì)列,可以是有界的,也可以是無界的,但在Executors中默認(rèn)使用無界的。當(dāng)有新的任務(wù)來到時(shí),若系統(tǒng)的線程數(shù)小于corePoolSize,線程池會創(chuàng)建新的線程執(zhí)行任務(wù), 當(dāng)系統(tǒng)的線程數(shù)量等于corePoolSize后,因?yàn)槭菬o界的任務(wù)隊(duì)列,總是能成功將任務(wù)添加到任務(wù)隊(duì)列中,所以線程數(shù)量不再增加。若任務(wù)創(chuàng)建的速度遠(yuǎn)大于任務(wù)處理的速度,無界隊(duì)列會快速增長,直到內(nèi)存耗盡。
- SynchronousQueue:一個(gè)不存儲元素的阻塞隊(duì)列,SynchronousQueue沒有容量,所以實(shí)際上提交的任務(wù)不會被添加到任務(wù)隊(duì)列,總是將新任務(wù)提交給線程執(zhí)行,如果沒有空閑的線程,則嘗試創(chuàng)建新的線程,如果線程數(shù)量已經(jīng)達(dá)到最大值(maximumPoolSize),則執(zhí)行拒絕策略。
- ArrayBlockingQueue:一個(gè)由數(shù)組結(jié)構(gòu)組成的有界阻塞隊(duì)列。
- PriorityBlockingQueue:一個(gè)支持優(yōu)先級排序的無界阻塞隊(duì)列。
- DelayQueue:一個(gè)使用優(yōu)先級隊(duì)列實(shí)現(xiàn)的無界阻塞隊(duì)列,只有在延遲期滿時(shí)才能從中提取元素。
- LinkedTransferQueue:一個(gè)由鏈表結(jié)構(gòu)組成的無界阻塞隊(duì)列。與SynchronousQueue類似,還含有非阻塞方法。
- LinkedBlockingDeque:一個(gè)由鏈表結(jié)構(gòu)組成的雙向阻塞隊(duì)列。
ThreadFactory 創(chuàng)建線程的工廠
線程池創(chuàng)建線程時(shí)調(diào)用的工廠方法,通過此方法可以設(shè)置線程的優(yōu)先級、線程命名規(guī)則以及線程類型(用戶線程還是守護(hù)線程)等。一般情況下使用默認(rèn)的,即Executors類的靜態(tài)方法defaultThreadFactory(),也可以自定義。
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class Test {
public static void main(String[] args) {
// 創(chuàng)建線程工廠
ThreadFactory threadFactory = new ThreadFactory() {
@Override
public Thread newThread(Runnable runnable) {
// 創(chuàng)建線程池中的線程
Thread thread = new Thread(runnable);
// 設(shè)置線程名稱
thread.setName("Thread-" + runnable.hashCode());
// 設(shè)置線程優(yōu)先級(最大值:10)
thread.setPriority(Thread.MAX_PRIORITY);
//......
return thread;
}
};
// 創(chuàng)建線程池
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
5,
10,
0L,
TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(10),
threadFactory
);
try{
threadPoolExecutor.submit(new Runnable() {
@Override
public void run() {
Thread thread = Thread.currentThread();
System.out.println(String.format("線程:%s,線程優(yōu)先級:%d",thread.getName(), thread.getPriority()));
}
});
}catch (Exception e) {
} finally {
threadPoolExecutor.shutdown();
}
}
}
Executors.defaultThreadFactory()
RejectedExecutionHandler
拒絕策略。當(dāng)線程池的任務(wù)超出線程池隊(duì)列可以存儲的最大值之后,執(zhí)行的策略。
- AbortPolicy策略:直接拋出異常,阻止系統(tǒng)正常工作。(線程池的默認(rèn)策略)。
- DiscardOldestPolicy策略:丟棄任務(wù)隊(duì)列中最早添加的任務(wù),并嘗試提交當(dāng)前任務(wù)。
- CallerRunsPolicy策略:調(diào)用主線程執(zhí)行被拒絕的任務(wù),這提供了一種簡單的反饋控制機(jī)制,將降低新任務(wù)的提交速度。
- DiscardPolicy策略:忽略并拋棄當(dāng)前任務(wù)。
new一個(gè)線程池