java自帶的四種線程池實例詳解
java預定義的哪四種線程池?
- newSingleThreadExexcutor:單線程數的線程池(核心線程數=最大線程數=1)
- newFixedThreadPool:固定線程數的線程池(核心線程數=最大線程數=自定義)
- newCacheThreadPool:可緩存的線程池(核心線程數=0,最大線程數=Integer.MAX_VALUE)
- newScheduledThreadPool:支持定時或周期任務的線程池(核心線程數=自定義,最大線程數=Integer.MAX_VALUE)
四種線程池有什么區(qū)別?
上面四種線程池類都繼承ThreadPoolExecutor,在創(chuàng)建時都是直接返回new ThreadPoolExecutor(參數),它們的區(qū)別是定義的ThreadPoolExecutor(參數)中參數不同,而ThreadPoolExecutor又繼承ExecutorService接口類
- newFixedThreadPool
定義:
xecutorService executorService=Executors.newFixedThreadPool(2);
缺點:使用了LinkBlockQueue的鏈表型阻塞隊列,當任務的堆積速度大于處理速度時,容易堆積任務而導致OOM內存溢出
- newSingleThreadExecutor
定義:ExecutorService executorService =Executors.newSingleThreadExecutor();
上面代碼神似new FixedThreadPoop(1),但又有區(qū)別,因為外面多了一層FinalizableDelegatedExecutorService,其作用:
可知,fixedExecutorService的本質是ThreadPoolExecutor,所以fixedExecutorService可以強轉成ThreadPoolExecutor,但singleExecutorService與ThreadPoolExecutor無任何關系,所以強轉失敗,故newSingleThreadExecutor()被創(chuàng)建后,無法再修改其線程池參數,真正地做到single單個線程。
缺點:使用了LinkBlockQueue的鏈表型阻塞隊列,當任務的堆積速度大于處理速度時,容易堆積任務而導致OOM內存溢出
newCacheThreadPool
定義:ExecutorService executorService=Executors.newCacheThreadPool();
缺點:SynchronousQueue是BlockingQueue的一種實現,它也是一個隊列,因為最大線程數為Integer.MAX_VALUE,所有當線程過多時容易OOM內存溢出
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()會調用ThreadPoolExecutor的構造函數初始化并返回一個ThreadPoolExecutor,而ThreadPoolExecutor使實現ExecutorService接口的 //最終ScheduledThreadPoolExecutor也和上面幾種線程池一樣返回的是ExecutorService接口的實現類ThreadPoolExecutor super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS, new DelayedWorkQueue()); }
線程池有哪幾個重要參數?
ThreadPoolExecutor構造方法如下:
- keepAliveTime是指當前線程數位于 [核心線程數,最大線程數] 之間的這些非核心線程等待多久空閑時間而沒有活干時,就退出線程池;
- 等待丟列的大小與最大線程數是沒有任何關系的,線程創(chuàng)建優(yōu)先級=核心線程 > 阻塞隊列 > 擴容的線程(當前核心線程數小于最大線程數時才能擴容線程)
- 假如核心線程數5,等待隊列長度為3,最大線程數10:當線程數不斷在增加時,先創(chuàng)建5個核心線程,核心線程數滿了再把線程丟進等待丟列,等待隊列滿了(3個線程),此時會比較最大線程數(只有等待丟列滿了最大線程數才能出場),還可以繼續(xù)創(chuàng)建2個線程(5+3+2),若線程數超過了最大線程數,則執(zhí)行拒絕策略;
- 假如核心線程數5,等待隊列長度為3,最大線程數7:當線程數不斷在增加時,先創(chuàng)建5個核心線程,核心線程數滿了再把線程丟進等待丟列,當等待隊列中有2個線程時達到了最大線程數(5+2=7),但是等待丟列還沒滿所以不用管最大線程數,直到等待丟列滿了(3個阻塞線程),此時會比較最大線程數(只有等待丟列滿了最大線程數才能出場),此時核心+等待丟列=5+3=8>7=最大線程數,即已經達到最大線程數了,則執(zhí)行拒絕策略;
- 如果把等待丟列設置為LinkedBlockingQueue無界丟列,這個丟列是無限大的,就永遠不會走到判斷最大線程數那一步了
如何自定義線程池
可以使用有界隊列,自定義線程創(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(); // 預啟動所有核心線程 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); //讓任務執(zhí)行慢點 } catch (InterruptedException e) { e.printStackTrace(); } } public String getName() { return name; } @Override public String toString() { return "MyTask [name=" + name + "]"; } } }
運行結果:
其中7-10號線程被拒絕策略拒絕了,1、2號線程執(zhí)行完后,3、6號線程進入核心線程池執(zhí)行,此時4、5號線程在任務隊列等待執(zhí)行,3、6線程執(zhí)行完再通知4、5線程執(zhí)行
總結
到此這篇關于java自帶的四種線程池的文章就介紹到這了,更多相關java四種線程池內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot中@ConditionalOnBean實現原理解讀
這篇文章主要介紹了SpringBoot中@ConditionalOnBean實現原理,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02java中利用List的subList方法實現對List分頁(簡單易學)
本篇文章主要介紹了java中l(wèi)ist數據拆分為sublist實現頁面分頁的簡單代碼,具有一定的參考價值,有需要的可以了解一下。2016-11-11