Java多線程ThreadPoolExecutor詳解
前言:
根據(jù)ThreadPoolExecutor的構(gòu)造方法,JDK提供了很多工廠方法來(lái)創(chuàng)建各種用途的線程池.
1 newFixedThreadPool
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}說(shuō)明:
- 核心線程數(shù) == 最大線程數(shù)(沒(méi)有救急線程被創(chuàng)建),因此也無(wú)需超時(shí)時(shí)間
- 阻塞隊(duì)列是無(wú)界的,可以放任意數(shù)量的任務(wù)(最大為Integer.MAX_VALUE)
適用于 任務(wù)量一已知,相對(duì)耗時(shí)的任務(wù)
2 newCachedThreadPool
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}說(shuō)明:
- 核心線程數(shù)是 0, 最大線程數(shù)是 Integer.MAX_VALUE,救急線程的空閑生存時(shí)間是 60s
- 全部都是救急線程(60s 后可以回收)
- 救急線程可以無(wú)限創(chuàng)建(最大是Integer.MAX_VALUE)
- 隊(duì)列采用了 SynchronousQueue 實(shí)現(xiàn)特點(diǎn)是,它沒(méi)有容量,沒(méi)有線程來(lái)取是放不進(jìn)去的(一手交錢(qián)、一手交 貨)
如下案例:
SynchronousQueue<Integer> integers = new SynchronousQueue<>();
new Thread(() -> {
try {
log.debug("putting {} ", 1);
integers.put(1);
log.debug("{} putted...", 1);
log.debug("putting...{} ", 2);
integers.put(2);
log.debug("{} putted...", 2);
} catch (InterruptedException e) {
e.printStackTrace();
}
},"t1").start();
sleep(1);
new Thread(() -> {
try {
log.debug("taking {}", 1);
integers.take();
} catch (InterruptedException e) {
e.printStackTrace();
}
},"t2").start();
sleep(1);
new Thread(() -> {
try {
log.debug("taking {}", 2);
integers.take();
} catch (InterruptedException e) {
e.printStackTrace();
}
},"t3").start();
/*
運(yùn)行結(jié)果:
11:48:15.500 c.TestSynchronousQueue [t1] - putting 1
11:48:16.500 c.TestSynchronousQueue [t2] - taking 1
11:48:16.500 c.TestSynchronousQueue [t1] - 1 putted...
11:48:16.500 c.TestSynchronousQueue [t1] - putting...2
11:48:17.502 c.TestSynchronousQueue [t3] - taking 2
11:48:17.503 c.TestSynchronousQueue [t1] - 2 putted...
*/整個(gè)線程池表現(xiàn)為線程數(shù)會(huì)根據(jù)任務(wù)量不斷增長(zhǎng),沒(méi)有上限,當(dāng)任務(wù)執(zhí)行完畢,空閑 1分鐘后釋放線程。
適用于 任務(wù)數(shù)比較密集,但每個(gè)任務(wù)執(zhí)行時(shí)間較短的情況
3 newSingleThreadExecutor
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()));
}希望多個(gè)任務(wù)排隊(duì)執(zhí)行。線程數(shù)固定為 1,任務(wù)數(shù)多于 1 時(shí),會(huì)放入無(wú)界隊(duì)列排隊(duì)。任務(wù)執(zhí)行完畢,這唯一的線程也不會(huì)被釋放.
與其他線程區(qū)別:
- 自己創(chuàng)建一個(gè)單線程串行執(zhí)行任務(wù),如果任務(wù)執(zhí)行失敗而終止那么沒(méi)有任何補(bǔ)救措施,而線程池還會(huì)新建一 個(gè)線程,保證池的正常工作
- Executors.newSingleThreadExecutor() 線程個(gè)數(shù)始終為1,不能修改
- FinalizableDelegatedExecutorService 應(yīng)用的是裝飾器模式,只對(duì)外暴露了 ExecutorService 接口,因此不能調(diào)用 ThreadPoolExecutor 中特有的方法.
- Executors.newFixedThreadPool(1) 初始時(shí)為1,以后還可以修改
- 對(duì)外暴露的是 ThreadPoolExecutor 對(duì)象,可以強(qiáng)轉(zhuǎn)后調(diào)用 setCorePoolSize 等方法進(jìn)行修改
4 提交任務(wù)
// 執(zhí)行任務(wù) void execute(Runnable command); // 提交任務(wù) task,用返回值 Future 獲得任務(wù)執(zhí)行結(jié)果 <T> Future<T> submit(Callable<T> task); // 提交 tasks 中所有任務(wù) <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException; // 提交 tasks 中所有任務(wù),帶超時(shí)時(shí)間 <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException; // 提交 tasks 中所有任務(wù),哪個(gè)任務(wù)先成功執(zhí)行完畢,返回此任務(wù)執(zhí)行結(jié)果,其它任務(wù)取消 <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException; // 提交 tasks 中所有任務(wù),哪個(gè)任務(wù)先成功執(zhí)行完畢,返回此任務(wù)執(zhí)行結(jié)果,其它任務(wù)取消,帶超時(shí)時(shí)間 <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException;
上述都是提供的提交任務(wù)的方法,根據(jù)不同的業(yè)務(wù)場(chǎng)景需求,選擇對(duì)應(yīng)的提交方法.
5 關(guān)閉線程池
shutdown
/* 線程池狀態(tài)變?yōu)?SHUTDOWN - 不會(huì)接收新任務(wù) - 但已提交任務(wù)會(huì)執(zhí)行完 - 此方法不會(huì)阻塞調(diào)用線程的執(zhí)行 */ void shutdown();
public void shutdown() {
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
checkShutdownAccess();
// 修改線程池狀態(tài)
advanceRunState(SHUTDOWN);
// 僅會(huì)打斷空閑線程
interruptIdleWorkers();
onShutdown(); // 擴(kuò)展點(diǎn) ScheduledThreadPoolExecutor
} finally {
mainLock.unlock();
}
// 嘗試終結(jié)(沒(méi)有運(yùn)行的線程可以立刻終結(jié),如果還有運(yùn)行的線程也不會(huì)等)
tryTerminate();
}shutdownNow
/* 線程池狀態(tài)變?yōu)?STOP - 不會(huì)接收新任務(wù) - 會(huì)將隊(duì)列中的任務(wù)返回 - 并用 interrupt 的方式中斷正在執(zhí)行的任務(wù) */ List<Runnable> shutdownNow();
public List<Runnable> shutdownNow() {
List<Runnable> tasks;
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
checkShutdownAccess();
// 修改線程池狀態(tài)
advanceRunState(STOP);
// 打斷所有線程
interruptWorkers();
// 獲取隊(duì)列中剩余任務(wù)
tasks = drainQueue();
} finally {
mainLock.unlock();
}
// 嘗試終結(jié)
tryTerminate();
return tasks;
}其他打斷方法
// 不在 RUNNING 狀態(tài)的線程池,此方法就返回 true boolean isShutdown(); // 線程池狀態(tài)是否是 TERMINATED boolean isTerminated(); // 調(diào)用 shutdown 后,由于調(diào)用線程并不會(huì)等待所有任務(wù)運(yùn)行結(jié)束,因此如果它想在線程池 TERMINATED 后做些事 情,可以利用此方法等待 boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException;
到此這篇關(guān)于Java多線程ThreadPoolExecutor詳解的文章就介紹到這了,更多相關(guān)Java ThreadPoolExecutor內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Java線程池?ThreadPoolExecutor?詳解
- Java線程池ThreadPoolExecutor源碼深入分析
- java高并發(fā)ThreadPoolExecutor類(lèi)解析線程池執(zhí)行流程
- java高并發(fā)ScheduledThreadPoolExecutor與Timer區(qū)別
- 徹底搞懂java并發(fā)ThreadPoolExecutor使用
- Java多線程編程基石ThreadPoolExecutor示例詳解
- 源碼分析Java中ThreadPoolExecutor的底層原理
- 一文搞懂Java的ThreadPoolExecutor原理
- 一文弄懂Java中ThreadPoolExecutor
相關(guān)文章
Java超過(guò)long類(lèi)型的數(shù)據(jù)表示方法
這篇文章主要給大家介紹Java超過(guò)long類(lèi)型的數(shù)據(jù)如何表示,在 Java 中,如果需要表示超過(guò) long 類(lèi)型范圍的數(shù)據(jù),可以使用 BigInteger 類(lèi),BigInteger 是 Java 提供的一個(gè)用于處理任意精度整數(shù)的類(lèi),它可以表示非常大或非常小的整數(shù),需要的朋友可以參考下2023-09-09
Java throw Exception實(shí)現(xiàn)異常轉(zhuǎn)換
這篇文章主要介紹了Java throw Exception實(shí)現(xiàn)異常轉(zhuǎn)換,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
關(guān)于Rabbitmq死信隊(duì)列及延時(shí)隊(duì)列的實(shí)現(xiàn)
這篇文章主要介紹了關(guān)于Rabbitmq死信隊(duì)列及延時(shí)隊(duì)列的實(shí)現(xiàn),TTL就是消息或者隊(duì)列的過(guò)期功能,當(dāng)消息過(guò)期就會(huì)進(jìn)到死信隊(duì)列,死信隊(duì)列和普通隊(duì)列沒(méi)啥區(qū)別,然后我們只需要配置一個(gè)消費(fèi)者來(lái)消費(fèi)死信隊(duì)列里面的消息就可以了,需要的朋友可以參考下2023-08-08
java 并發(fā)編程之共享變量的實(shí)現(xiàn)方法
這篇文章主要介紹了java 并發(fā)編程之共享變量的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
Spring?Cloud?Alibaba負(fù)載均衡實(shí)現(xiàn)方式
這篇文章主要為大家介紹了Spring?Cloud?Alibaba負(fù)載均衡實(shí)現(xiàn)方式詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
IDEA不能生成SerialVersionUID,alt+enter不提示沒(méi)有效果問(wèn)題
文章介紹了在使用IntelliJ IDEA時(shí),通過(guò)安裝AutoFillingJavaCallArguments插件并導(dǎo)入Serializable接口后,遇到無(wú)法自動(dòng)生成序列ID的問(wèn)題,解決方法是在設(shè)置中搜索serial,勾選“不帶'serialVersionUID'的可序列化類(lèi)”選項(xiàng)2025-01-01
java設(shè)計(jì)模式之外觀模式學(xué)習(xí)筆記
這篇文章主要為大家詳細(xì)介紹了java設(shè)計(jì)模式之外觀模式學(xué)習(xí)筆記,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
DolphinScheduler容錯(cuò)Master源碼分析
這篇文章主要為大家介紹了DolphinScheduler容錯(cuò)Master源碼分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02

