本篇是多線程系列的第三篇,如果對前兩篇感興趣的也可以去看看。
Android進階系列文章是我在學習的同時對知識點的整理,一是為了加深印象,二是方便后續(xù)查閱。
如果文中有錯誤的地方,歡迎批評指出。
前言
如果在Android里面,直接 new Thread
,阿里巴巴 Android 開發(fā)規(guī)范會提示你不要顯示創(chuàng)建線程,請使用線程池,為啥要用線程池?你對線程池了解多少?
一、線程池ThreadPoolExecutor 基礎概念
1、什么是線程池
在 多線程(一)、基礎概念及notify()和wait()的使用 講了線程的創(chuàng)建,每當有任務來的時候,通過創(chuàng)建一個線程來執(zhí)行任務,當任務執(zhí)行結(jié)束,對線程進行銷毀,并發(fā)操作的時候,大量任務需要執(zhí)行,每個任務都要需要重復線程的創(chuàng)建、執(zhí)行、銷毀,造成了CPU的資源銷毀,并降低了響應速度。
new Thread(new Runnable() {
@Override
public void run() {
// 任務執(zhí)行
}
}).start();
**線程池 **:字面上理解就是將線程通過一個池子進行管理,當任務來的時候,從池子中取出一個已經(jīng)創(chuàng)建好的線程進行任務的執(zhí)行,執(zhí)行結(jié)束后再將線程放回池中,待線程池銷毀的時候再統(tǒng)一對線程進行銷毀。
2、使用線程池的好處
通過上面的對比,使用線程池基本有以前好處:
1、降低資源消耗。通過重復使用線程池中的線程,降低了線程創(chuàng)建和銷毀帶來的資源消耗。
2、提高響應速度。重復使用池中線程,減少了重復創(chuàng)建和銷毀線程帶來的時間開銷。
3、提高線程的可管理性。線程是稀缺資源,我們不可能無節(jié)制創(chuàng)建,這樣會大量消耗系統(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
最復雜的一個構(gòu)造方法,我們把參數(shù)單獨拎出來講
1、int corePoolSize
核心線程數(shù)量:每當接收到一個任務的時候,線程池會創(chuàng)建一個新的線程來執(zhí)行任務,直到當前線程池中的線程數(shù)目等于 corePoolSize
,當任務大于corePoolSize
時候,會放入阻塞隊列
2、int maximumPoolSize
非核心線程數(shù)量:線程池中允許的最大線程數(shù),如果當前阻塞隊列滿了,當接收到新的任務就會再次創(chuàng)建線程進行執(zhí)行,直到線程池中的數(shù)目等于maximumPoolSize
3、long keepAliveTime
線程空閑時存活時間:當線程數(shù)大于沒有任務執(zhí)行的時候,繼續(xù)存活的時間,默認該參數(shù)只有線程數(shù)大于corePoolSize
時才有用
4、TimeUnit unit
keepAliveTime的時間單位
5、BlockingQueue<Runnable> workQueue
阻塞隊列:當線程池中線程數(shù)目超過 corePoolSize
的時候,線程會進入阻塞隊列進行阻塞等待,當阻塞隊列滿了的時候,會根據(jù) maximumPoolSize
數(shù)量新開線程執(zhí)行。
隊列:
是一種特殊的線性表,特殊之處在于它只允許在表的前端(front)進行刪除操作,而在表的后端(rear)進行插入操作,和棧一樣,隊列是一種操作受限制的線性表。
進行插入操作的端稱為隊尾,進行刪除操作的端稱為隊頭。
隊列中沒有元素時,稱為空隊列。隊列的數(shù)據(jù)元素又稱為隊列元素。
在隊列中插入一個隊列元素稱為入隊,從隊列中刪除一個隊列元素稱為出隊。
因為隊列只允許在一端插入,在另一端刪除,所以只有最早進入隊列的元素才能最先從隊列中刪除,故隊列又稱為先進先出(FIFO—first in first out)線性表。
阻塞隊列常用于生產(chǎn)者和消費者的場景,生產(chǎn)者是往隊列里添加元素的線程,消費者是從隊列里拿元素的線程。阻塞隊列就是生產(chǎn)者存放元素的緩存容器,而消費者也只從容器里拿元素。
先看看 BlockingQueue
,它是一個接口,繼承 Queue
public interface BlockingQueue<E> extends Queue<E>
再看看它里面的方法
針對這幾個方法,簡單的進行介紹:
拋出異常 | 返回特殊值 | 阻塞 | 超時 | |
---|---|---|---|---|
插入 | add(e) | offer(e) | put(e) | offer(e, time, unit) |
移除 | remove() | poll() | take() | poll(time, unit) |
檢查 | element() | peek() |
? 拋出異常:是指當阻塞隊列滿時候,再往隊列里插入元素,會拋出IllegalStateException("Queue full")異常。當隊列為空時,從隊列里獲取元素時會拋出NoSuchElementException異常 。
? ? 返回特殊值:插入方法會返回是否成功,成功則返回true。移除方法,則是從隊列里拿出一個元素,如果沒有則返回null
? ? 阻塞:當阻塞隊列滿時,如果生產(chǎn)者線程往隊列里put元素,隊列會一直阻塞生產(chǎn)者線程,直到拿到數(shù)據(jù),或者響應中斷退出。當隊列空時,消費者線程試圖從隊列里take元素,隊列也會阻塞消費者線程,直到隊列可用。
? ? 超時:當阻塞隊列滿時,隊列會阻塞生產(chǎn)者線程一段時間,如果超過一定的時間,生產(chǎn)者線程就會退出。
我們再看JDK為我們提供的一些阻塞隊列,如下圖:
簡單說明:
阻塞隊列 | 用法 |
---|---|
ArrayBlockingQueue | 一個由數(shù)組結(jié)構(gòu)組成的有界阻塞隊列。 |
LinkedBlockingQueue | 一個由鏈表結(jié)構(gòu)組成的有界阻塞隊列。 |
PriorityBlockingQueue | 一個支持優(yōu)先級排序的無界阻塞隊列。 |
DelayQueue | 一個使用優(yōu)先級隊列實現(xiàn)的無界阻塞隊列。 |
SynchronousQueue | 一個不存儲元素的阻塞隊列。 |
LinkedTransferQueue | 一個由鏈表結(jié)構(gòu)組成的無界阻塞隊列。 |
LinkedBlockingDeque | 一個由鏈表結(jié)構(gòu)組成的雙向阻塞隊列。 |
6、ThreadFactory threadFactory
創(chuàng)建線程的工廠,通過自定義的線程工廠可以給每個新建的線程設置一個具有識別度的線程名Executors靜態(tài)工廠里默認的threadFactory,線程的命名規(guī)則是“pool-數(shù)字-thread-數(shù)字”。
7、RejectedExecutionHandler handler (飽和策略)
線程池的飽和策略,如果任務特別多,隊列也滿了,且沒有空閑線程進行處理,線程池將必須對新的任務采取飽和策略,即提供一種方式來處理這部分任務。
jdk 給我們提供了四種策略,如圖:
策略 | 作用 |
---|---|
AbortPolicy | 直接拋出異常,該策略也為默認策略 |
CallerRunsPolicy | 在調(diào)用者線程中執(zhí)行該任務 |
DiscardOldestPolicy | 丟棄阻塞隊列最前面的任務,并執(zhí)行當前任務 |
DiscardPolicy | 直接丟棄任務 |
我們可以看到 RejectedExecutionHandler
實際是一個接口,且只有一個 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、線程池工作機制
熟悉了上面線程池的各個參數(shù)含義,對線程池的工作原理,我們也可以大致總結(jié)如下:
1、線程池剛創(chuàng)建的時候,里面沒有線程在運行,當有任務進來,并且線程池開始執(zhí)行的時候,會根據(jù)實際情況處理。
2、當前線程池線程數(shù)量少于 corePoolSize
時候,每當有新的任務來時,都會創(chuàng)建一個新的線程進行執(zhí)行。
3、當線程池中運行的線程數(shù)大于等于 corePoolSize
,每當有新的任務來的時候,都會加入阻塞隊列中。
4、當阻塞隊列加滿,無法再加入新的任務的時候,則會再根據(jù) maximumPoolSize
數(shù) 來創(chuàng)建新的非核心線程執(zhí)行任務。
4、當線程池中線程數(shù)目大于等于 maximumPoolSize
時候,當有新的任務來的時候,拒絕執(zhí)行該任務,采取飽和策略。
5、當一個線程無事可做,超過一定的時間(keepAliveTime)時,線程池會判斷,如果當前運行的線程數(shù)大于 corePoolSize,那么這個線程就被停掉。所以線程池的所有任務完成后,它最終會收縮到 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)建的線程池,其實質(zhì)也是調(diào)用 ThreadPoolExecutor
進行創(chuàng)建,只是針對不同的需求,對參數(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>()
可以看到核心線程和非核心線程一致,及不會創(chuàng)建非核心線程,超時時間為0,即就算線程處于空閑狀態(tài),也不會對其進行回收,阻塞隊列為LinkedBlockingQueue
無界阻塞隊列。
當有任務來的時候,先創(chuàng)建核心線程,線程數(shù)超過 corePoolSize
就進入阻塞隊列,當有空閑線程的時候,再在阻塞隊列中去任務執(zhí)行。
使用場景:線程池線程數(shù)固定,且不會回收,線程生命周期與線程池生命周期同步,適用任務量比較固定且耗時的長的任務。
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ū)別就是線程池中只存在一個核心線程來執(zhí)行任務。
使用場景:只有一個線程,確保所以任務都在一個線程中順序執(zhí)行,不需要處理線程同步問題,適用多個任務順序執(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秒會被回收,任務隊列采用的是SynchronousQueue
,這個隊列是無法插入任務的,一有任務立即執(zhí)行。
使用場景:由于非核心線程無限制,且使用無法插入的SynchronousQueue
隊列,所以適合任務量大但耗時少的任務。
4、newScheduledThreadPool
定時延時執(zhí)行
public ScheduledThreadPoolExecutor(int corePoolSize,
ThreadFactory threadFactory) {
super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
new DelayedWorkQueue(), threadFactory);
}
參數(shù)說明:
int corePoolSize: corePoolSize (設定)
int maximumPoolSize: Integer.MAX_VALUE
long keepAliveTime:0
TimeUnit unit:NANOSECONDS
BlockingQueue<Runnable> workQueue:new DelayedWorkQueue()
核心線程數(shù)固定(設置),非核心線程數(shù)創(chuàng)建無限制,但是空閑時間為0,即非核心線程一旦空閑就回收, DelayedWorkQueue()
無界隊列會將任務進行排序,延時執(zhí)行隊列任務。
使用場景:newScheduledThreadPool是唯一一個具有定時定期執(zhí)行任務功能的線程池。它適合執(zhí)行一些周期性任務或者延時任務,可以通過schedule(Runnable command, long delay, TimeUnit unit)
方法實現(xiàn)。
6、線程池的執(zhí)行
線程池提供了 execute
和 submit
兩個方法來執(zhí)行
execute:
public void execute(Runnable command) {
if (command == null)
throw new NullPointerException();
// 獲得當前線程的生命周期對應的二進制狀態(tài)碼
int c = ctl.get();
//判斷當前線程數(shù)量是否小于核心線程數(shù)量,如果小于就直接創(chuàng)建核心線程執(zhí)行任務,創(chuàng)建成功直接跳出,失敗則接著往下走.
if (workerCountOf(c) < corePoolSize) {
if (addWorker(command, true))
return;
c = ctl.get();
}
//判斷線程池是否為RUNNING狀態(tài),并且將任務添加至隊列中.
if (isRunning(c) && workQueue.offer(command)) {
int recheck = ctl.get();
//審核下線程池的狀態(tài),如果不是RUNNING狀態(tài),直接移除隊列中
if (! isRunning(recheck) && remove(command))
reject(command);
//如果當前線程數(shù)量為0,則單獨創(chuàng)建線程,而不指定任務.
else if (workerCountOf(recheck) == 0)
addWorker(null, false);
}
//如果不滿足上述條件,嘗試創(chuàng)建一個非核心線程來執(zhí)行任務,如果創(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 對象
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 中實際也是調(diào)用了 execute() 方法,只不過在調(diào)用方法之前,先將Runnable
對象封裝成FutureTask
對象,然后再返回 Future<T>
,我們可以通過Future
的 get
方法,拿到任務執(zhí)行結(jié)束后的返回值。
我們在 多線程(一)、基礎概念及notify()和wait()的使用 中也講了 FutureTask
它提供了 cancel
、isCancelled
、isDone
、get
幾個方法,來對任務進行相應的操作。
總結(jié):
通常情況下,我們不需要對線程或者獲取執(zhí)行結(jié)果,可以直接使用
execute
方法。如果我們要獲取任務執(zhí)行的結(jié)果,或者想對任務進行取消等操作,就使用
submit
方法。
7、線程池的關(guān)閉
關(guān)于線程的中斷在 多線程(一)、基礎概念及notify()和wait()的使用 里面有介紹。
- shutdown():不會立即終止線程池,而是要等所有任務緩存隊列中的任務都執(zhí)行完后才終止,但再也不會接受新的任務
- shutdownNow():立即終止線程池,并嘗試打斷正在執(zhí)行的任務,并且清空任務緩存隊列,返回尚未執(zhí)行的任務
8、線程池的合理配置
線程池的參數(shù)比較靈活,我們可以自由設置,但是具體每個參數(shù)該設置成多少比較合理呢?這個要根據(jù)我們處理的任務來決定,對任務一般從以下幾個點分析:
8.1、任務的性質(zhì)
CPU 密集型、IO 密集型、混合型
CPU密集型應配置盡可能小的線程,如Ncpu+1個線程的線程池
IO密集型,IO操作有關(guān),如磁盤,內(nèi)存,網(wǎng)絡等等,對CPU的要求不高則應配置盡可能多的線程,如2*Ncpu個線程的線程池
混合型需要拆成CPU 密集型和IO 密集型分別分析,根據(jù)任務數(shù)量和執(zhí)行時間,來決定線程的數(shù)量
8.2、任務的優(yōu)先級
高中低優(yōu)先級
8.3、任務執(zhí)行時間
長中短
8.4、任務的依耐性
是否需要依賴其他系統(tǒng)資源,如數(shù)據(jù)庫連接
Runtime.getRuntime().availableProcessors() : 當前設備的CPU個數(shù)
9、線程池實戰(zhàn)
又巴拉巴拉說了一大推,我覺得唯有代碼運行,通過結(jié)果分析最能打動人心,下面就通過代碼運行結(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 + " 當前線程:" + Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
// 執(zhí)行
threadPoolExecutor.execute(runnable);
}
}
我們通過 ThreadPoolExecutor
創(chuàng)建了一個線程池,然后執(zhí)行30個任務。
參數(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秒被回收,阻塞隊列使用了 new LinkedBlockingQueue
并指定了隊列容量為50。
結(jié)果:
我們看到每兩秒后,有三個任務被執(zhí)行。這是因為核心我們設置的核心線程數(shù)為3,當多余的任務到來后,會先放入到阻塞隊列中,又由于我們設置的阻塞隊列容量為50,所以,阻塞隊列永遠不會滿,就不會啟動非核心線程。
我們改一下我們的線程池如下:
final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
2,
5,
1,
TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(25));
參數(shù)就不分析了,我們直接看結(jié)果:
我們看到這次每隔兩秒有五個任務在執(zhí)行,為什么?這里要根據(jù)我們前面線程池的工作原理來分析,我們有三十個任務需要執(zhí)行,核心線程數(shù)為2,其余的任務放入阻塞隊列中,阻塞隊列容量為25,剩余任務不超過非核心線程數(shù),當阻塞隊列滿的時候,就啟動了非核心線程來執(zhí)行。
我們再簡單改一下我們的線程池,代碼如下:
final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
2,
5,
1,
TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(24));
相比上面的,我們就將阻塞隊列容量改成了24,如果上面你對線程池的工作原理清楚了,你應該能知道我這里改成 24 的良苦用心了,我們先看結(jié)果。
最直接的就是拋異常了,但是線程池仍然再執(zhí)行任務,首先為啥拋異常?首先,我們需要執(zhí)行三十個任務,但是我們的阻塞隊列容量為 24,隊列滿后啟動了非核心線程,但是非核心線程數(shù)量為5,當剩下的這個任務來的時候,線程池將采取飽和策略,我們沒有設置,默認為 AbortPolicy
,即直接拋異常,如果我們手動設置飽和策略如下:
final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
2,
5,
1,
TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(24),new ThreadPoolExecutor.DiscardPolicy());
我們這里采用的飽和策略為 DiscardPolicy
,即丟棄多余任務。最終可以看到結(jié)果沒有拋異常,最終只執(zhí)行了29個任務,最后一個任務被拋棄了。
最后再看一下通過 Executors 靜態(tài)方法創(chuàng)建的線程池運行上面的任務結(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é)
這是多線程的第三篇,這篇文章篇幅有點多, 有點小亂,后續(xù)會再整理一下,基本都是跟著自己的思路,在寫的同時,自己也會再操作一遍,源碼分析過程中,也會盡可能的詳細,一步步的深入,后續(xù)查閱的時候也方便,文章中有些不是很詳細的地方,后面可能會再次更新,或者單獨用一篇文章來講。