Java多線程實現(xiàn)之Executor詳解
Executor 通過創(chuàng)建線程池的方式來管理線程,需要的朋友可以參考下
Executor 線程實現(xiàn)
Runnable runnable = new Runnable() {
@Override
public void run() {
System.out.println("runnable 線程開始執(zhí)行");
}
};
Executor executor = Executors.newCachedThreadPool();
executor.execute(runnable);
executor.execute(runnable);
executor.execute(runnable);1 Executor 簡介
- Executor 給他一個 Runnable,他就能自動很安全的幫你把這個線程執(zhí)行完畢
- Executor 通過創(chuàng)建線程池的方式來管理線程
- Executor 有五種創(chuàng)建線程池的方式,其中 newCachedThreadPool() 是一種非常簡單的線程池創(chuàng)建方式
- Executor 有兩種結(jié)束方式,shutDown() 和 shutDownNow()
- shutDown 會將當前正在執(zhí)行的和排隊中的線程執(zhí)行完畢后結(jié)束,并且不允許有新的線程加入到待執(zhí)行隊列中
- shutDownNow 會立即結(jié)束執(zhí)行,采取的是一種比較安全的結(jié)束方式:interrupt()
2 Executor 中 其他幾種創(chuàng)建線程池的方式
2.1 Executor 中的第一種線程池創(chuàng)建方式 newCachedThreadPool()
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}newCachedThreadPool() 的實現(xiàn):
在 newCachedThreadPool 中,會直接返回一個 ThreadPoolExecutor,這里解釋一下創(chuàng)建 ThreadPoolExecutor 的幾個參數(shù):
/**
* @param corePoolSize 代表默認線程數(shù),或者說最低線程數(shù),也就是此線程池創(chuàng)建后里面立馬會有幾個線程的數(shù)量
* @param maximumPoolSize 代表最大線程數(shù),超過此線程數(shù)量則排隊等待執(zhí)行,排隊線程放入最后一個參數(shù)中
* @param keepAliveTime 表示默認線程數(shù)量外的已經(jīng)創(chuàng)建出來的線程在處于空閑狀態(tài)下,被回收前需要等待的時間
* @param unit 表示第三個時間值的單位
* @param workQueue 用于盛放多余任務的線程隊列
*/
ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue)- 第一個參數(shù)代表默認線程數(shù),或者說最低線程數(shù),也就是此線程池創(chuàng)建后里面立馬會有幾個線程的數(shù)量
- 第二個參數(shù)代表最大線程數(shù),超過此線程數(shù)量則排隊等待執(zhí)行,排隊線程放入最后一個參數(shù)中
- 第三個參數(shù)表示默認線程數(shù)量外的已經(jīng)創(chuàng)建出來的線程在處于空閑狀態(tài)下,被回收前需要等待的時間
- 第四個參數(shù)表示第三個時間值的單位
- 第五個參數(shù)用于盛放多余任務的線程隊列
例:
假如說傳入的值分別為 5,20,10L,TimeUnit.SECONDS,new SynchronousQueue< Runnable>()
則表示,創(chuàng)建一個默認數(shù)量為 5 的線程池,這個線程池最多允許 20 條線程同時工作,如果超過 20 個則存放進隊列中等待這 20 個線程騰出位置,每個線程執(zhí)行完畢都去隊列中取新的任務并執(zhí)行,如果隊列中沒有新的任務,則線程等待 10 秒,如果 10 后仍然沒有新的任務,則線程會被回收,一直回收到只剩余 5 個線程
2.1 Executor.newSingleThreadExecutor()
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()));
}通過前兩個參數(shù)可以看出,這是創(chuàng)建單線程的線程池,也就是說這個線程池中只有一個線程,并且最多也只能有一個線程,應用場景比較少。
2.2 Executor.newFixedThreadPool()
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}創(chuàng)建固定數(shù)量線程的線程池,通過前兩個參數(shù)可以看出,傳入的參數(shù)被同時應用中前兩個參數(shù)中,這就意味著我創(chuàng)建一個線程池,里面的默認線程數(shù)和最大線程數(shù)一致
并且不能擴容、不被回收,一直放在那里,表面上可能比較合理,但實際的應用場景也很少
在處理瞬時爆發(fā)性任務時會能用得到,例:
Runnable precessImageRunner = new Runnable() {
@Override
public void run() {
System.out.println("開始處理圖片了,我很耗時,,,,");
}
};
List<Bitmap> bitmaps = new ArrayList<>();
ExecutorService fixedExecutor = Executors.newFixedThreadPool(30);
for (Bitmap bitmap : bitmaps) {
fixedExecutor.execute(precessImageRunner);
}
fixedExecutor.shutdown();fixedExecutor.shutdown() 來防止永遠不回收造成資源浪費的情況
Executor.newScheduledThreadPool()
創(chuàng)建一個延遲的線程池
Executor.newSingleThreadScheduledExecutor()
創(chuàng)建一個延遲的單線程線程池
總結(jié)
以上就是 Executor 的簡介及 Executor 五種創(chuàng)建線程的方式,你學廢了嗎?
復習一遍:
// Executor 五種創(chuàng)建線程的方式
Executors.newCachedThreadPool();
Executors.newSingleThreadExecutor();
Executors.newFixedThreadPool();
Executors.newScheduledThreadPool();
Executors.newSingleThreadScheduledExecutor();到此這篇關于Java多線程實現(xiàn)之Executor詳解的文章就介紹到這了,更多相關Java多線程Executor內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java?LinkedList實現(xiàn)班級信息管理系統(tǒng)
這篇文章主要為大家詳細介紹了Java?LinkedList實現(xiàn)班級信息管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02
谷歌二維碼引擎com.google.zxing二維碼生成與解析
這篇文章主要給大家介紹了關于谷歌二維碼引擎com.google.zxing二維碼生成與解析的相關資料,zxing是google開源的二維碼生成和解析工具,需要的朋友可以參考下2023-07-07

