java自帶的四種線程池實例詳解
java預(yù)定義的哪四種線程池?
- newSingleThreadExexcutor:單線程數(shù)的線程池(核心線程數(shù)=最大線程數(shù)=1)
- newFixedThreadPool:固定線程數(shù)的線程池(核心線程數(shù)=最大線程數(shù)=自定義)
- newCacheThreadPool:可緩存的線程池(核心線程數(shù)=0,最大線程數(shù)=Integer.MAX_VALUE)
- newScheduledThreadPool:支持定時或周期任務(wù)的線程池(核心線程數(shù)=自定義,最大線程數(shù)=Integer.MAX_VALUE)
四種線程池有什么區(qū)別?
上面四種線程池類都繼承ThreadPoolExecutor,在創(chuàng)建時都是直接返回new ThreadPoolExecutor(參數(shù)),它們的區(qū)別是定義的ThreadPoolExecutor(參數(shù))中參數(shù)不同,而ThreadPoolExecutor又繼承ExecutorService接口類
- newFixedThreadPool
定義:
xecutorService executorService=Executors.newFixedThreadPool(2);

缺點:使用了LinkBlockQueue的鏈表型阻塞隊列,當(dāng)任務(wù)的堆積速度大于處理速度時,容易堆積任務(wù)而導(dǎo)致OOM內(nèi)存溢出
- newSingleThreadExecutor
定義:ExecutorService executorService =Executors.newSingleThreadExecutor();

上面代碼神似new FixedThreadPoop(1),但又有區(qū)別,因為外面多了一層FinalizableDelegatedExecutorService,其作用:

可知,fixedExecutorService的本質(zhì)是ThreadPoolExecutor,所以fixedExecutorService可以強(qiáng)轉(zhuǎn)成ThreadPoolExecutor,但singleExecutorService與ThreadPoolExecutor無任何關(guān)系,所以強(qiáng)轉(zhuǎn)失敗,故newSingleThreadExecutor()被創(chuàng)建后,無法再修改其線程池參數(shù),真正地做到single單個線程。
缺點:使用了LinkBlockQueue的鏈表型阻塞隊列,當(dāng)任務(wù)的堆積速度大于處理速度時,容易堆積任務(wù)而導(dǎo)致OOM內(nèi)存溢出
newCacheThreadPool
定義:ExecutorService executorService=Executors.newCacheThreadPool();

缺點:SynchronousQueue是BlockingQueue的一種實現(xiàn),它也是一個隊列,因為最大線程數(shù)為Integer.MAX_VALUE,所有當(dāng)線程過多時容易OOM內(nèi)存溢出
ScheduledThreadPool
定義:ExecutorService executorService=Executors.newScheduledThreadPool(2);
源碼:
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
//ScheduledThreadPoolExecutor繼承ThreadPoolExecutor
return new ScheduledThreadPoolExecutor(corePoolSize);
}
public ScheduledThreadPoolExecutor(int corePoolSize) {
//ScheduledThreadPoolExecutor繼承ThreadPoolExecutor,故super()會調(diào)用ThreadPoolExecutor的構(gòu)造函數(shù)初始化并返回一個ThreadPoolExecutor,而ThreadPoolExecutor使實現(xiàn)ExecutorService接口的
//最終ScheduledThreadPoolExecutor也和上面幾種線程池一樣返回的是ExecutorService接口的實現(xiàn)類ThreadPoolExecutor
super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
new DelayedWorkQueue());
}
線程池有哪幾個重要參數(shù)?
ThreadPoolExecutor構(gòu)造方法如下:


- keepAliveTime是指當(dāng)前線程數(shù)位于 [核心線程數(shù),最大線程數(shù)] 之間的這些非核心線程等待多久空閑時間而沒有活干時,就退出線程池;
- 等待丟列的大小與最大線程數(shù)是沒有任何關(guān)系的,線程創(chuàng)建優(yōu)先級=核心線程 > 阻塞隊列 > 擴(kuò)容的線程(當(dāng)前核心線程數(shù)小于最大線程數(shù)時才能擴(kuò)容線程)
- 假如核心線程數(shù)5,等待隊列長度為3,最大線程數(shù)10:當(dāng)線程數(shù)不斷在增加時,先創(chuàng)建5個核心線程,核心線程數(shù)滿了再把線程丟進(jìn)等待丟列,等待隊列滿了(3個線程),此時會比較最大線程數(shù)(只有等待丟列滿了最大線程數(shù)才能出場),還可以繼續(xù)創(chuàng)建2個線程(5+3+2),若線程數(shù)超過了最大線程數(shù),則執(zhí)行拒絕策略;
- 假如核心線程數(shù)5,等待隊列長度為3,最大線程數(shù)7:當(dāng)線程數(shù)不斷在增加時,先創(chuàng)建5個核心線程,核心線程數(shù)滿了再把線程丟進(jìn)等待丟列,當(dāng)?shù)却犃兄杏?個線程時達(dá)到了最大線程數(shù)(5+2=7),但是等待丟列還沒滿所以不用管最大線程數(shù),直到等待丟列滿了(3個阻塞線程),此時會比較最大線程數(shù)(只有等待丟列滿了最大線程數(shù)才能出場),此時核心+等待丟列=5+3=8>7=最大線程數(shù),即已經(jīng)達(dá)到最大線程數(shù)了,則執(zhí)行拒絕策略;
- 如果把等待丟列設(shè)置為LinkedBlockingQueue無界丟列,這個丟列是無限大的,就永遠(yuǎn)不會走到判斷最大線程數(shù)那一步了
如何自定義線程池
可以使用有界隊列,自定義線程創(chuàng)建工廠ThreadFactory和拒絕策略handler來自定義線程池
public class ThreadTest {
public static void main(String[] args) throws InterruptedException, IOException {
int corePoolSize = 2;
int maximumPoolSize = 4;
long keepAliveTime = 10;
TimeUnit unit = TimeUnit.SECONDS;
BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(2);
ThreadFactory threadFactory = new NameTreadFactory();
RejectedExecutionHandler handler = new MyIgnorePolicy();
ThreadPoolExecutor executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit,
workQueue, threadFactory, handler);
executor.prestartAllCoreThreads(); // 預(yù)啟動所有核心線程
for (int i = 1; i <= 10; i++) {
MyTask task = new MyTask(String.valueOf(i));
executor.execute(task);
}
System.in.read(); //阻塞主線程
}
static class NameTreadFactory implements ThreadFactory {
private final AtomicInteger mThreadNum = new AtomicInteger(1);
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r, "my-thread-" + mThreadNum.getAndIncrement());
System.out.println(t.getName() + " has been created");
return t;
}
}
public static class MyIgnorePolicy implements RejectedExecutionHandler {
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
doLog(r, e);
}
private void doLog(Runnable r, ThreadPoolExecutor e) {
// 可做日志記錄等
System.err.println( r.toString() + " rejected");
// System.out.println("completedTaskCount: " + e.getCompletedTaskCount());
}
}
static class MyTask implements Runnable {
private String name;
public MyTask(String name) {
this.name = name;
}
@Override
public void run() {
try {
System.out.println(this.toString() + " is running!");
Thread.sleep(3000); //讓任務(wù)執(zhí)行慢點
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public String getName() {
return name;
}
@Override
public String toString() {
return "MyTask [name=" + name + "]";
}
}
}
運行結(jié)果:

其中7-10號線程被拒絕策略拒絕了,1、2號線程執(zhí)行完后,3、6號線程進(jìn)入核心線程池執(zhí)行,此時4、5號線程在任務(wù)隊列等待執(zhí)行,3、6線程執(zhí)行完再通知4、5線程執(zhí)行
總結(jié)
到此這篇關(guān)于java自帶的四種線程池的文章就介紹到這了,更多相關(guān)java四種線程池內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java面試題-實現(xiàn)復(fù)雜鏈表的復(fù)制代碼分享
這篇文章主要介紹了Java面試題-實現(xiàn)復(fù)雜鏈表的復(fù)制代碼分享,小編覺得還是挺不錯的,具有參考價值,需要的朋友可以了解下。2017-10-10
SpringBoot?自定義starter?yaml提示失效問題及解決方法
在自定義starter后,必不可少會有properties配置參數(shù)需要指定,而在有時又不知道為什么出現(xiàn)這個問題,這篇文章主要介紹了SpringBoot?自定義starter?yaml提示失效問題,需要的朋友可以參考下2022-12-12
Java實現(xiàn)將數(shù)字日期翻譯成英文單詞的工具類實例
這篇文章主要介紹了Java實現(xiàn)將數(shù)字日期翻譯成英文單詞的工具類,結(jié)合完整實例形式分析了Java日期轉(zhuǎn)換與字符串操作相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2017-09-09
如何在SpringBoot中使用Spring-AOP實現(xiàn)接口鑒權(quán)
這篇文章主要介紹了如何在SpringBoot中使用Spring-AOP實現(xiàn)接口鑒權(quán),文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,感興趣的小伙伴可以參考一下2022-09-09
Java 實戰(zhàn)圖書管理系統(tǒng)的實現(xiàn)流程
讀萬卷書不如行萬里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SSM+jsp+mysql+maven實現(xiàn)一個圖書管理系統(tǒng),大家可以在過程中查缺補(bǔ)漏,提升水平2021-11-11

