本篇是多線程系列的第三篇声搁,如果對(duì)前兩篇感興趣的也可以去看看。
Android進(jìn)階系列文章是我在學(xué)習(xí)的同時(shí)對(duì)知識(shí)點(diǎn)的整理,一是為了加深印象讶泰,二是方便后續(xù)查閱咏瑟。
如果文中有錯(cuò)誤的地方,歡迎批評(píng)指出痪署。
前言
如果在Android里面码泞,直接 new Thread
,阿里巴巴 Android 開發(fā)規(guī)范會(huì)提示你不要顯示創(chuàng)建線程,請(qǐng)使用線程池狼犯,為啥要用線程池余寥?你對(duì)線程池了解多少?
一悯森、線程池ThreadPoolExecutor 基礎(chǔ)概念
1宋舷、什么是線程池
在 多線程(一)、基礎(chǔ)概念及notify()和wait()的使用 講了線程的創(chuàng)建呐馆,每當(dāng)有任務(wù)來的時(shí)候肥缔,通過創(chuàng)建一個(gè)線程來執(zhí)行任務(wù),當(dāng)任務(wù)執(zhí)行結(jié)束汹来,對(duì)線程進(jìn)行銷毀续膳,并發(fā)操作的時(shí)候,大量任務(wù)需要執(zhí)行收班,每個(gè)任務(wù)都要需要重復(fù)線程的創(chuàng)建坟岔、執(zhí)行、銷毀摔桦,造成了CPU的資源銷毀社付,并降低了響應(yīng)速度承疲。
new Thread(new Runnable() {
@Override
public void run() {
// 任務(wù)執(zhí)行
}
}).start();
**線程池 **:字面上理解就是將線程通過一個(gè)池子進(jìn)行管理,當(dāng)任務(wù)來的時(shí)候鸥咖,從池子中取出一個(gè)已經(jīng)創(chuàng)建好的線程進(jìn)行任務(wù)的執(zhí)行燕鸽,執(zhí)行結(jié)束后再將線程放回池中,待線程池銷毀的時(shí)候再統(tǒng)一對(duì)線程進(jìn)行銷毀啼辣。
2啊研、使用線程池的好處
通過上面的對(duì)比,使用線程池基本有以前好處:
1鸥拧、降低資源消耗党远。通過重復(fù)使用線程池中的線程,降低了線程創(chuàng)建和銷毀帶來的資源消耗富弦。
2沟娱、提高響應(yīng)速度。重復(fù)使用池中線程腕柜,減少了重復(fù)創(chuàng)建和銷毀線程帶來的時(shí)間開銷济似。
3、提高線程的可管理性盏缤。線程是稀缺資源碱屁,我們不可能無節(jié)制創(chuàng)建,這樣會(huì)大量消耗系統(tǒng)資源蛾找,使用線程池可以統(tǒng)一分配娩脾,管理和監(jiān)控線程。
3打毛、線程池參數(shù)說明
要使用線程池柿赊,就必須要用到 ThreadPoolExecutor
類,
/**
* Creates a new {@code ThreadPoolExecutor} with the given initial
* parameters.
*
* @param corePoolSize the number of threads to keep in the pool, even
* if they are idle, unless {@code allowCoreThreadTimeOut} is set
* @param maximumPoolSize the maximum number of threads to allow in the
* pool
* @param keepAliveTime when the number of threads is greater than
* the core, this is the maximum time that excess idle threads
* will wait for new tasks before terminating.
* @param unit the time unit for the {@code keepAliveTime} argument
* @param workQueue the queue to use for holding tasks before they are
* executed. This queue will hold only the {@code Runnable}
* tasks submitted by the {@code execute} method.
* @param threadFactory the factory to use when the executor
* creates a new thread
* @param handler the handler to use when execution is blocked
* because the thread bounds and queue capacities are reached
* @throws IllegalArgumentException if one of the following holds:<br>
* {@code corePoolSize < 0}<br>
* {@code keepAliveTime < 0}<br>
* {@code maximumPoolSize <= 0}<br>
* {@code maximumPoolSize < corePoolSize}
* @throws NullPointerException if {@code workQueue}
* or {@code threadFactory} or {@code handler} is null
*/
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;
}
這里貼了 ThreadPoolExecutor
最復(fù)雜的一個(gè)構(gòu)造方法幻枉,我們把參數(shù)單獨(dú)拎出來講
1碰声、int corePoolSize
核心線程數(shù)量:每當(dāng)接收到一個(gè)任務(wù)的時(shí)候,線程池會(huì)創(chuàng)建一個(gè)新的線程來執(zhí)行任務(wù)熬甫,直到當(dāng)前線程池中的線程數(shù)目等于 corePoolSize
胰挑,當(dāng)任務(wù)大于corePoolSize
時(shí)候,會(huì)放入阻塞隊(duì)列
2椿肩、int maximumPoolSize
非核心線程數(shù)量:線程池中允許的最大線程數(shù)瞻颂,如果當(dāng)前阻塞隊(duì)列滿了,當(dāng)接收到新的任務(wù)就會(huì)再次創(chuàng)建線程進(jìn)行執(zhí)行郑象,直到線程池中的數(shù)目等于maximumPoolSize
3贡这、long keepAliveTime
線程空閑時(shí)存活時(shí)間:當(dāng)線程數(shù)大于沒有任務(wù)執(zhí)行的時(shí)候,繼續(xù)存活的時(shí)間厂榛,默認(rèn)該參數(shù)只有線程數(shù)大于corePoolSize
時(shí)才有用
4盖矫、TimeUnit unit
keepAliveTime的時(shí)間單位
5丽惭、BlockingQueue<Runnable> workQueue
阻塞隊(duì)列:當(dāng)線程池中線程數(shù)目超過 corePoolSize
的時(shí)候,線程會(huì)進(jìn)入阻塞隊(duì)列進(jìn)行阻塞等待辈双,當(dāng)阻塞隊(duì)列滿了的時(shí)候责掏,會(huì)根據(jù) maximumPoolSize
數(shù)量新開線程執(zhí)行。
隊(duì)列:
是一種特殊的線性表湃望,特殊之處在于它只允許在表的前端(front)進(jìn)行刪除操作拷橘,而在表的后端(rear)進(jìn)行插入操作,和棧一樣喜爷,隊(duì)列是一種操作受限制的線性表。
進(jìn)行插入操作的端稱為隊(duì)尾萄唇,進(jìn)行刪除操作的端稱為隊(duì)頭檩帐。
隊(duì)列中沒有元素時(shí),稱為空隊(duì)列另萤。隊(duì)列的數(shù)據(jù)元素又稱為隊(duì)列元素湃密。
在隊(duì)列中插入一個(gè)隊(duì)列元素稱為入隊(duì),從隊(duì)列中刪除一個(gè)隊(duì)列元素稱為出隊(duì)四敞。
因?yàn)殛?duì)列只允許在一端插入泛源,在另一端刪除,所以只有最早進(jìn)入隊(duì)列的元素才能最先從隊(duì)列中刪除忿危,故隊(duì)列又稱為先進(jìn)先出(FIFO—first in first out)線性表达箍。
阻塞隊(duì)列常用于生產(chǎn)者和消費(fèi)者的場景,生產(chǎn)者是往隊(duì)列里添加元素的線程铺厨,消費(fèi)者是從隊(duì)列里拿元素的線程缎玫。阻塞隊(duì)列就是生產(chǎn)者存放元素的緩存容器,而消費(fèi)者也只從容器里拿元素解滓。
先看看 BlockingQueue
赃磨,它是一個(gè)接口,繼承 Queue
public interface BlockingQueue<E> extends Queue<E>
再看看它里面的方法
針對(duì)這幾個(gè)方法洼裤,簡單的進(jìn)行介紹:
拋出異常 | 返回特殊值 | 阻塞 | 超時(shí) | |
---|---|---|---|---|
插入 | add(e) | offer(e) | put(e) | offer(e, time, unit) |
移除 | remove() | poll() | take() | poll(time, unit) |
檢查 | element() | peek() |
? 拋出異常:是指當(dāng)阻塞隊(duì)列滿時(shí)候邻辉,再往隊(duì)列里插入元素,會(huì)拋出IllegalStateException("Queue full")異常腮鞍。當(dāng)隊(duì)列為空時(shí)值骇,從隊(duì)列里獲取元素時(shí)會(huì)拋出NoSuchElementException異常 。
? ? 返回特殊值:插入方法會(huì)返回是否成功移国,成功則返回true雷客。移除方法,則是從隊(duì)列里拿出一個(gè)元素桥狡,如果沒有則返回null
? ? 阻塞:當(dāng)阻塞隊(duì)列滿時(shí)搅裙,如果生產(chǎn)者線程往隊(duì)列里put元素皱卓,隊(duì)列會(huì)一直阻塞生產(chǎn)者線程,直到拿到數(shù)據(jù)部逮,或者響應(yīng)中斷退出娜汁。當(dāng)隊(duì)列空時(shí),消費(fèi)者線程試圖從隊(duì)列里take元素兄朋,隊(duì)列也會(huì)阻塞消費(fèi)者線程掐禁,直到隊(duì)列可用。
? ? 超時(shí):當(dāng)阻塞隊(duì)列滿時(shí)颅和,隊(duì)列會(huì)阻塞生產(chǎn)者線程一段時(shí)間傅事,如果超過一定的時(shí)間,生產(chǎn)者線程就會(huì)退出峡扩。
我們?cè)倏碕DK為我們提供的一些阻塞隊(duì)列蹭越,如下圖:
簡單說明:
阻塞隊(duì)列 | 用法 |
---|---|
ArrayBlockingQueue | 一個(gè)由數(shù)組結(jié)構(gòu)組成的有界阻塞隊(duì)列。 |
LinkedBlockingQueue | 一個(gè)由鏈表結(jié)構(gòu)組成的有界阻塞隊(duì)列教届。 |
PriorityBlockingQueue | 一個(gè)支持優(yōu)先級(jí)排序的無界阻塞隊(duì)列响鹃。 |
DelayQueue | 一個(gè)使用優(yōu)先級(jí)隊(duì)列實(shí)現(xiàn)的無界阻塞隊(duì)列。 |
SynchronousQueue | 一個(gè)不存儲(chǔ)元素的阻塞隊(duì)列案训。 |
LinkedTransferQueue | 一個(gè)由鏈表結(jié)構(gòu)組成的無界阻塞隊(duì)列买置。 |
LinkedBlockingDeque | 一個(gè)由鏈表結(jié)構(gòu)組成的雙向阻塞隊(duì)列。 |
6强霎、ThreadFactory threadFactory
創(chuàng)建線程的工廠忿项,通過自定義的線程工廠可以給每個(gè)新建的線程設(shè)置一個(gè)具有識(shí)別度的線程名Executors靜態(tài)工廠里默認(rèn)的threadFactory,線程的命名規(guī)則是“pool-數(shù)字-thread-數(shù)字”城舞。
7倦卖、RejectedExecutionHandler handler (飽和策略)
線程池的飽和策略,如果任務(wù)特別多椿争,隊(duì)列也滿了怕膛,且沒有空閑線程進(jìn)行處理,線程池將必須對(duì)新的任務(wù)采取飽和策略秦踪,即提供一種方式來處理這部分任務(wù)褐捻。
jdk 給我們提供了四種策略,如圖:
策略 | 作用 |
---|---|
AbortPolicy | 直接拋出異常椅邓,該策略也為默認(rèn)策略 |
CallerRunsPolicy | 在調(diào)用者線程中執(zhí)行該任務(wù) |
DiscardOldestPolicy | 丟棄阻塞隊(duì)列最前面的任務(wù)柠逞,并執(zhí)行當(dāng)前任務(wù) |
DiscardPolicy | 直接丟棄任務(wù) |
我們可以看到 RejectedExecutionHandler
實(shí)際是一個(gè)接口,且只有一個(gè) rejectedExecution
所以我們可以根據(jù)自己的需求定義自己的飽和策略景馁。
/**
* A handler for tasks that cannot be executed by a {@link ThreadPoolExecutor}.
*
* @since 1.5
* @author Doug Lea
*/
public interface RejectedExecutionHandler {
/**
* Method that may be invoked by a {@link ThreadPoolExecutor} when
* {@link ThreadPoolExecutor#execute execute} cannot accept a
* task. This may occur when no more threads or queue slots are
* available because their bounds would be exceeded, or upon
* shutdown of the Executor.
*
* <p>In the absence of other alternatives, the method may throw
* an unchecked {@link RejectedExecutionException}, which will be
* propagated to the caller of {@code execute}.
*
* @param r the runnable task requested to be executed
* @param executor the executor attempting to execute this task
* @throws RejectedExecutionException if there is no remedy
*/
void rejectedExecution(Runnable r, ThreadPoolExecutor executor);
}
4板壮、線程池工作機(jī)制
熟悉了上面線程池的各個(gè)參數(shù)含義,對(duì)線程池的工作原理合住,我們也可以大致總結(jié)如下:
1绰精、線程池剛創(chuàng)建的時(shí)候撒璧,里面沒有線程在運(yùn)行,當(dāng)有任務(wù)進(jìn)來笨使,并且線程池開始執(zhí)行的時(shí)候卿樱,會(huì)根據(jù)實(shí)際情況處理。
2硫椰、當(dāng)前線程池線程數(shù)量少于 corePoolSize
時(shí)候繁调,每當(dāng)有新的任務(wù)來時(shí),都會(huì)創(chuàng)建一個(gè)新的線程進(jìn)行執(zhí)行靶草。
3蹄胰、當(dāng)線程池中運(yùn)行的線程數(shù)大于等于 corePoolSize
,每當(dāng)有新的任務(wù)來的時(shí)候奕翔,都會(huì)加入阻塞隊(duì)列中裕寨。
4、當(dāng)阻塞隊(duì)列加滿糠悯,無法再加入新的任務(wù)的時(shí)候,則會(huì)再根據(jù) maximumPoolSize
數(shù) 來創(chuàng)建新的非核心線程執(zhí)行任務(wù)妻往。
4互艾、當(dāng)線程池中線程數(shù)目大于等于 maximumPoolSize
時(shí)候,當(dāng)有新的任務(wù)來的時(shí)候讯泣,拒絕執(zhí)行該任務(wù)纫普,采取飽和策略。
5好渠、當(dāng)一個(gè)線程無事可做昨稼,超過一定的時(shí)間(keepAliveTime)時(shí),線程池會(huì)判斷拳锚,如果當(dāng)前運(yùn)行的線程數(shù)大于 corePoolSize假栓,那么這個(gè)線程就被停掉。所以線程池的所有任務(wù)完成后霍掺,它最終會(huì)收縮到 corePoolSize
的大小匾荆。
5、創(chuàng)建線程池
5.1杆烁、ThreadPoolExecutor
直接通過 ThreadPoolExecutor 創(chuàng)建:
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
2, 10
, 1, TimeUnit.SECONDS
, new LinkedBlockingQueue<Runnable>(50)
, Executors.defaultThreadFactory()
, new ThreadPoolExecutor.AbortPolicy());
5.2牙丽、Executors 靜態(tài)方法
通過工具類java.util.concurrent.Executors
創(chuàng)建的線程池,其實(shí)質(zhì)也是調(diào)用 ThreadPoolExecutor
進(jìn)行創(chuàng)建兔魂,只是針對(duì)不同的需求烤芦,對(duì)參數(shù)進(jìn)行了設(shè)置。
1析校、FixedThreadPool
可重用固定線程數(shù)
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
參數(shù)說明:
int corePoolSize: nThreads
int maximumPoolSize: nThreads
long keepAliveTime:0L
TimeUnit unit:TimeUnit.MILLISECONDS
BlockingQueue<Runnable> workQueue:new LinkedBlockingQueue<Runnable>()
可以看到核心線程和非核心線程一致构罗,及不會(huì)創(chuàng)建非核心線程铜涉,超時(shí)時(shí)間為0,即就算線程處于空閑狀態(tài)绰播,也不會(huì)對(duì)其進(jìn)行回收骄噪,阻塞隊(duì)列為LinkedBlockingQueue
無界阻塞隊(duì)列。
當(dāng)有任務(wù)來的時(shí)候蠢箩,先創(chuàng)建核心線程链蕊,線程數(shù)超過 corePoolSize
就進(jìn)入阻塞隊(duì)列,當(dāng)有空閑線程的時(shí)候谬泌,再在阻塞隊(duì)列中去任務(wù)執(zhí)行滔韵。
使用場景:線程池線程數(shù)固定,且不會(huì)回收掌实,線程生命周期與線程池生命周期同步陪蜻,適用任務(wù)量比較固定且耗時(shí)的長的任務(wù)。
2贱鼻、newSingleThreadExecutor
單線程執(zhí)行
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()));
}
參數(shù)說明:
int corePoolSize: 1
int maximumPoolSize: 1
long keepAliveTime:0L
TimeUnit unit:TimeUnit.MILLISECONDS
BlockingQueue<Runnable> workQueue:new LinkedBlockingQueue<Runnable>()
基本和 FixedThreadPool 一致宴卖,最明顯的區(qū)別就是線程池中只存在一個(gè)核心線程來執(zhí)行任務(wù)。
使用場景:只有一個(gè)線程邻悬,確保所以任務(wù)都在一個(gè)線程中順序執(zhí)行症昏,不需要處理線程同步問題,適用多個(gè)任務(wù)順序執(zhí)行父丰。
3肝谭、newCachedThreadPool
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}
參數(shù)說明:
int corePoolSize: 0
int maximumPoolSize: Integer.MAX_VALUE
long keepAliveTime:60L
TimeUnit unit:TimeUnit.SECONDS
BlockingQueue<Runnable> workQueue:new SynchronousQueue<Runnable>()
無核心線程,非核心線程數(shù)量 Integer.MAX_VALUE蛾扇,可以無限創(chuàng)建攘烛,空閑線程60秒會(huì)被回收,任務(wù)隊(duì)列采用的是SynchronousQueue
镀首,這個(gè)隊(duì)列是無法插入任務(wù)的坟漱,一有任務(wù)立即執(zhí)行。
使用場景:由于非核心線程無限制更哄,且使用無法插入的SynchronousQueue
隊(duì)列靖秩,所以適合任務(wù)量大但耗時(shí)少的任務(wù)。
4竖瘾、newScheduledThreadPool
定時(shí)延時(shí)執(zhí)行
public ScheduledThreadPoolExecutor(int corePoolSize,
ThreadFactory threadFactory) {
super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
new DelayedWorkQueue(), threadFactory);
}
參數(shù)說明:
int corePoolSize: corePoolSize (設(shè)定)
int maximumPoolSize: Integer.MAX_VALUE
long keepAliveTime:0
TimeUnit unit:NANOSECONDS
BlockingQueue<Runnable> workQueue:new DelayedWorkQueue()
核心線程數(shù)固定(設(shè)置)沟突,非核心線程數(shù)創(chuàng)建無限制,但是空閑時(shí)間為0捕传,即非核心線程一旦空閑就回收惠拭, DelayedWorkQueue()
無界隊(duì)列會(huì)將任務(wù)進(jìn)行排序,延時(shí)執(zhí)行隊(duì)列任務(wù)。
使用場景:newScheduledThreadPool是唯一一個(gè)具有定時(shí)定期執(zhí)行任務(wù)功能的線程池职辅。它適合執(zhí)行一些周期性任務(wù)或者延時(shí)任務(wù)棒呛,可以通過schedule(Runnable command, long delay, TimeUnit unit)
方法實(shí)現(xiàn)。
6域携、線程池的執(zhí)行
線程池提供了 execute
和 submit
兩個(gè)方法來執(zhí)行
execute:
public void execute(Runnable command) {
if (command == null)
throw new NullPointerException();
// 獲得當(dāng)前線程的生命周期對(duì)應(yīng)的二進(jìn)制狀態(tài)碼
int c = ctl.get();
//判斷當(dāng)前線程數(shù)量是否小于核心線程數(shù)量,如果小于就直接創(chuàng)建核心線程執(zhí)行任務(wù),創(chuàng)建成功直接跳出,失敗則接著往下走.
if (workerCountOf(c) < corePoolSize) {
if (addWorker(command, true))
return;
c = ctl.get();
}
//判斷線程池是否為RUNNING狀態(tài),并且將任務(wù)添加至隊(duì)列中.
if (isRunning(c) && workQueue.offer(command)) {
int recheck = ctl.get();
//審核下線程池的狀態(tài),如果不是RUNNING狀態(tài),直接移除隊(duì)列中
if (! isRunning(recheck) && remove(command))
reject(command);
//如果當(dāng)前線程數(shù)量為0,則單獨(dú)創(chuàng)建線程,而不指定任務(wù).
else if (workerCountOf(recheck) == 0)
addWorker(null, false);
}
//如果不滿足上述條件,嘗試創(chuàng)建一個(gè)非核心線程來執(zhí)行任務(wù),如果創(chuàng)建失敗,調(diào)用reject()方法.
else if (!addWorker(command, false))
reject(command);
}
submit():
源碼:
public <T> Future<T> submit(Runnable task, T result) {
if (task == null) throw new NullPointerException();
// 將runnable封裝成 Future 對(duì)象
RunnableFuture<T> ftask = newTaskFor(task, result);
// 執(zhí)行 execute 方法
execute(ftask);
// 返回包裝好的Runable
return ftask;
}
// newTaskFor : 通過 FutureTask
protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) {
return new FutureTask<T>(runnable, value);
}
其中 newTaskFor
返回的 RunnableFuture<T>
方法繼承了 Runnable
接口簇秒,所以可以直接通過 execute
方法執(zhí)行。
public interface RunnableFuture<V> extends Runnable, Future<V> {
/**
* Sets this Future to the result of its computation
* unless it has been cancelled.
*/
void run();
}
可以看到秀鞭,submit 中實(shí)際也是調(diào)用了 execute() 方法趋观,只不過在調(diào)用方法之前,先將Runnable
對(duì)象封裝成FutureTask
對(duì)象锋边,然后再返回 Future<T>
皱坛,我們可以通過Future
的 get
方法,拿到任務(wù)執(zhí)行結(jié)束后的返回值豆巨。
我們?cè)?多線程(一)剩辟、基礎(chǔ)概念及notify()和wait()的使用 中也講了 FutureTask
它提供了 cancel
、isCancelled
往扔、isDone
贩猎、get
幾個(gè)方法,來對(duì)任務(wù)進(jìn)行相應(yīng)的操作萍膛。
總結(jié):
通常情況下吭服,我們不需要對(duì)線程或者獲取執(zhí)行結(jié)果,可以直接使用
execute
方法卦羡。如果我們要獲取任務(wù)執(zhí)行的結(jié)果噪馏,或者想對(duì)任務(wù)進(jìn)行取消等操作麦到,就使用
submit
方法绿饵。
7、線程池的關(guān)閉
關(guān)于線程的中斷在 多線程(一)瓶颠、基礎(chǔ)概念及notify()和wait()的使用 里面有介紹拟赊。
- shutdown():不會(huì)立即終止線程池,而是要等所有任務(wù)緩存隊(duì)列中的任務(wù)都執(zhí)行完后才終止粹淋,但再也不會(huì)接受新的任務(wù)
- shutdownNow():立即終止線程池吸祟,并嘗試打斷正在執(zhí)行的任務(wù),并且清空任務(wù)緩存隊(duì)列桃移,返回尚未執(zhí)行的任務(wù)
8屋匕、線程池的合理配置
線程池的參數(shù)比較靈活,我們可以自由設(shè)置借杰,但是具體每個(gè)參數(shù)該設(shè)置成多少比較合理呢过吻?這個(gè)要根據(jù)我們處理的任務(wù)來決定,對(duì)任務(wù)一般從以下幾個(gè)點(diǎn)分析:
8.1、任務(wù)的性質(zhì)
CPU 密集型纤虽、IO 密集型乳绕、混合型
CPU密集型應(yīng)配置盡可能小的線程,如Ncpu+1個(gè)線程的線程池
IO密集型逼纸,IO操作有關(guān)洋措,如磁盤,內(nèi)存杰刽,網(wǎng)絡(luò)等等,對(duì)CPU的要求不高則應(yīng)配置盡可能多的線程菠发,如2*Ncpu個(gè)線程的線程池
混合型需要拆成CPU 密集型和IO 密集型分別分析,根據(jù)任務(wù)數(shù)量和執(zhí)行時(shí)間专缠,來決定線程的數(shù)量
8.2雷酪、任務(wù)的優(yōu)先級(jí)
高中低優(yōu)先級(jí)
8.3、任務(wù)執(zhí)行時(shí)間
長中短
8.4涝婉、任務(wù)的依耐性
是否需要依賴其他系統(tǒng)資源哥力,如數(shù)據(jù)庫連接
Runtime.getRuntime().availableProcessors() : 當(dāng)前設(shè)備的CPU個(gè)數(shù)
9、線程池實(shí)戰(zhàn)
又巴拉巴拉說了一大推墩弯,我覺得唯有代碼運(yùn)行吩跋,通過結(jié)果分析最能打動(dòng)人心,下面就通過代碼運(yùn)行結(jié)果來分析渔工。
先看這么一段代碼:
public static void main(String[] args) {
// 1锌钮、通過 ThreadPoolExecutor 創(chuàng)建基本線程池
final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
3,
5,
1,
TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(50));
for (int i = 0; i < 30; i++) {
final int num = i;
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
// 睡兩秒后執(zhí)行
Thread.sleep(2000);
System.out.println("run : " + num + " 當(dāng)前線程:" + Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
// 執(zhí)行
threadPoolExecutor.execute(runnable);
}
}
我們通過 ThreadPoolExecutor
創(chuàng)建了一個(gè)線程池,然后執(zhí)行30個(gè)任務(wù)引矩。
參數(shù)說明:
int corePoolSize: 3
int maximumPoolSize: 5
long keepAliveTime:1
TimeUnit unit:TimeUnit.SECONDS
BlockingQueue<Runnable> workQueue:new LinkedBlockingQueue<Runnable>(50)
線程池核心線程數(shù)為3梁丘,非核心線程數(shù)為5,非核心線程空閑1秒被回收旺韭,阻塞隊(duì)列使用了 new LinkedBlockingQueue
并指定了隊(duì)列容量為50氛谜。
結(jié)果:
我們看到每兩秒后,有三個(gè)任務(wù)被執(zhí)行区端。這是因?yàn)楹诵奈覀冊(cè)O(shè)置的核心線程數(shù)為3值漫,當(dāng)多余的任務(wù)到來后,會(huì)先放入到阻塞隊(duì)列中织盼,又由于我們?cè)O(shè)置的阻塞隊(duì)列容量為50杨何,所以,阻塞隊(duì)列永遠(yuǎn)不會(huì)滿沥邻,就不會(huì)啟動(dòng)非核心線程危虱。
我們改一下我們的線程池如下:
final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
2,
5,
1,
TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(25));
參數(shù)就不分析了,我們直接看結(jié)果:
我們看到這次每隔兩秒有五個(gè)任務(wù)在執(zhí)行唐全,為什么埃跷?這里要根據(jù)我們前面線程池的工作原理來分析,我們有三十個(gè)任務(wù)需要執(zhí)行,核心線程數(shù)為2捌蚊,其余的任務(wù)放入阻塞隊(duì)列中集畅,阻塞隊(duì)列容量為25,剩余任務(wù)不超過非核心線程數(shù)缅糟,當(dāng)阻塞隊(duì)列滿的時(shí)候挺智,就啟動(dòng)了非核心線程來執(zhí)行。
我們?cè)俸唵胃囊幌挛覀兊木€程池窗宦,代碼如下:
final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
2,
5,
1,
TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(24));
相比上面的赦颇,我們就將阻塞隊(duì)列容量改成了24,如果上面你對(duì)線程池的工作原理清楚了赴涵,你應(yīng)該能知道我這里改成 24 的良苦用心了媒怯,我們先看結(jié)果。
最直接的就是拋異常了髓窜,但是線程池仍然再執(zhí)行任務(wù)扇苞,首先為啥拋異常?首先寄纵,我們需要執(zhí)行三十個(gè)任務(wù)鳖敷,但是我們的阻塞隊(duì)列容量為 24,隊(duì)列滿后啟動(dòng)了非核心線程程拭,但是非核心線程數(shù)量為5定踱,當(dāng)剩下的這個(gè)任務(wù)來的時(shí)候,線程池將采取飽和策略恃鞋,我們沒有設(shè)置崖媚,默認(rèn)為 AbortPolicy
,即直接拋異常恤浪,如果我們手動(dòng)設(shè)置飽和策略如下:
final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
2,
5,
1,
TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(24),new ThreadPoolExecutor.DiscardPolicy());
我們這里采用的飽和策略為 DiscardPolicy
畅哑,即丟棄多余任務(wù)。最終可以看到結(jié)果沒有拋異常资锰,最終只執(zhí)行了29個(gè)任務(wù)敢课,最后一個(gè)任務(wù)被拋棄了阶祭。
最后再看一下通過 Executors 靜態(tài)方法創(chuàng)建的線程池運(yùn)行上面的任務(wù)結(jié)果如何绷杜,Executors 創(chuàng)建的線程池本質(zhì)也是通過創(chuàng)建 ThreadPoolExecutor
來執(zhí)行,可結(jié)合上面分析自行總結(jié)濒募。
1鞭盟、FixedThreadPool
ExecutorService threadPoolExecutor = Executors.newFixedThreadPool(3);
結(jié)果:
2、newSingleThreadExecutor
ExecutorService threadPoolExecutor = Executors.newSingleThreadExecutor();
結(jié)果:
3瑰剃、newCachedThreadPool
ExecutorService threadPoolExecutor = Executors.newCachedThreadPool();
結(jié)果 :
4齿诉、newScheduledThreadPool
ScheduledExecutorService threadPoolExecutor = Executors.newScheduledThreadPool(3);
總結(jié)
這是多線程的第三篇,這篇文章篇幅有點(diǎn)多, 有點(diǎn)小亂粤剧,后續(xù)會(huì)再整理一下歇竟,基本都是跟著自己的思路,在寫的同時(shí)抵恋,自己也會(huì)再操作一遍焕议,源碼分析過程中,也會(huì)盡可能的詳細(xì)弧关,一步步的深入盅安,后續(xù)查閱的時(shí)候也方便,文章中有些不是很詳細(xì)的地方世囊,后面可能會(huì)再次更新别瞭,或者單獨(dú)用一篇文章來講。