這篇文章給大家介紹深入淺析Java中ThreadPoolExecutor的參數(shù),內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

康樂(lè)ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書(shū)未來(lái)市場(chǎng)廣闊!成為創(chuàng)新互聯(lián)建站的ssl證書(shū)銷(xiāo)售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書(shū)合作)期待與您的合作!
Java ThreadPoolExecutor的參數(shù)深入理解
一、使用Executors創(chuàng)建線程池
之前創(chuàng)建線程的時(shí)候都是用的Executors的newFixedThreadPool(),newSingleThreadExecutor(),newCachedThreadPool()這三個(gè)方法。當(dāng)然Executors也是用不同的參數(shù)去new ThreadPoolExecutor
1. newFixedThreadPool()
創(chuàng)建線程數(shù)固定大小的線程池。 由于使用了LinkedBlockingQueue所以maximumPoolSize 沒(méi)用,當(dāng)corePoolSize滿(mǎn)了之后就加入到LinkedBlockingQueue隊(duì)列中。每當(dāng)某個(gè)線程執(zhí)行完成之后就從LinkedBlockingQueue隊(duì)列中取一個(gè)。所以這個(gè)是創(chuàng)建固定大小的線程池。
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue) {
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
Executors.defaultThreadFactory(), defaultHandler);
}2.newSingleThreadPool()
創(chuàng)建線程數(shù)為1的線程池,由于使用了LinkedBlockingQueue所以maximumPoolSize 沒(méi)用,corePoolSize為1表示線程數(shù)大小為1,滿(mǎn)了就放入隊(duì)列中,執(zhí)行完了就從隊(duì)列取一個(gè)。
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()));
}3.newCachedThreadPool()
創(chuàng)建可緩沖的線程池。沒(méi)有大小限制。由于corePoolSize為0所以任務(wù)會(huì)放入SynchronousQueue隊(duì)列中,SynchronousQueue只能存放大小為1,所以會(huì)立刻新起線程,由于maxumumPoolSize為Integer.MAX_VALUE所以可以認(rèn)為大小為2147483647。受內(nèi)存大小限制。
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue) {
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
Executors.defaultThreadFactory(), defaultHandler);
}
二、使用ThreadPoolExecutor創(chuàng)建線程池
ThreadPoolExecutor的構(gòu)造函數(shù)
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler) {
if (corePoolSize < 0 ||
maximumPoolSize <= 0 ||
maximumPoolSize < corePoolSize ||
keepAliveTime < 0)
throw new IllegalArgumentException();
if (workQueue == null || threadFactory == null || handler == null)
throw new NullPointerException();
this.corePoolSize = corePoolSize;
this.maximumPoolSize = maximumPoolSize;
this.workQueue = workQueue;
this.keepAliveTime = unit.toNanos(keepAliveTime);
this.threadFactory = threadFactory;
this.handler = handler;
}參數(shù):
1、corePoolSize核心線程數(shù)大小,當(dāng)線程數(shù)<corePoolSize ,會(huì)創(chuàng)建線程執(zhí)行runnable
2、maximumPoolSize 最大線程數(shù), 當(dāng)線程數(shù) >= corePoolSize的時(shí)候,會(huì)把runnable放入workQueue中
3、keepAliveTime 保持存活時(shí)間,當(dāng)線程數(shù)大于corePoolSize的空閑線程能保持的最大時(shí)間。
4、unit 時(shí)間單位
5、workQueue 保存任務(wù)的阻塞隊(duì)列
6、threadFactory 創(chuàng)建線程的工廠
7、handler 拒絕策略
任務(wù)執(zhí)行順序:
1、當(dāng)線程數(shù)小于corePoolSize時(shí),創(chuàng)建線程執(zhí)行任務(wù)。
2、當(dāng)線程數(shù)大于等于corePoolSize并且workQueue沒(méi)有滿(mǎn)時(shí),放入workQueue中
3、線程數(shù)大于等于corePoolSize并且當(dāng)workQueue滿(mǎn)時(shí),新任務(wù)新建線程運(yùn)行,線程總數(shù)要小于maximumPoolSize
4、當(dāng)線程總數(shù)等于maximumPoolSize并且workQueue滿(mǎn)了的時(shí)候執(zhí)行handler的rejectedExecution。也就是拒絕策略。
ThreadPoolExecutor默認(rèn)有四個(gè)拒絕策略:
1、ThreadPoolExecutor.AbortPolicy() 直接拋出異常RejectedExecutionException
2、ThreadPoolExecutor.CallerRunsPolicy() 直接調(diào)用run方法并且阻塞執(zhí)行
3、ThreadPoolExecutor.DiscardPolicy() 直接丟棄后來(lái)的任務(wù)
4、ThreadPoolExecutor.DiscardOldestPolicy() 丟棄在隊(duì)列中隊(duì)首的任務(wù)
當(dāng)然可以自己繼承RejectedExecutionHandler來(lái)寫(xiě)拒絕策略.
int corePoolSize = 1;
int maximumPoolSize = 2;
int keepAliveTime = 10;
// BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<Runnable>();
BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<Runnable>(5);
ThreadFactory threadFactory = Executors.defaultThreadFactory();
//線程池和隊(duì)列滿(mǎn)了之后的處理方式
//1.跑出異常
RejectedExecutionHandler handler = new ThreadPoolExecutor.AbortPolicy();
RejectedExecutionHandler handler2 = new ThreadPoolExecutor.CallerRunsPolicy();
RejectedExecutionHandler handler3 = new ThreadPoolExecutor.DiscardPolicy();
RejectedExecutionHandler handler4 = new ThreadPoolExecutor.DiscardOldestPolicy();
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit.SECONDS, workQueue, threadFactory, handler2);
for (int j = 1; j < 15; j++) {
threadPoolExecutor.execute(new Runnable() {
public void run() {
try {
System.out.println(Thread.currentThread().getName());
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
System.out.println(threadPoolExecutor);
}關(guān)于深入淺析Java中ThreadPoolExecutor的參數(shù)就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
新聞名稱(chēng):深入淺析Java中ThreadPoolExecutor的參數(shù)
文章源于:http://chinadenli.net/article16/jhpegg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、外貿(mào)建站、網(wǎng)站排名、網(wǎng)站設(shè)計(jì)公司、企業(yè)網(wǎng)站制作、
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)