參考文章:
1.淺談線程池ThreadPoolExecutor核心參數
https://www.cnblogs.com/stupid-chan/p/9991307.html
2.Java線程池 ThreadPoolExecutor(一)線程池的核心方法以及原理
https://blog.csdn.net/m0_37506254/article/details/90574038
3.Java 中的幾種線程池,你之前用對了嗎
https://www.cnblogs.com/fengzheng/p/9297602.html
4.線程池異常處理之重啓線程處理任務
https://www.cnblogs.com/hapjin/p/10240863.html
整理下線程池的相關知識。阿里巴巴的規範是不允許使用Java提供的 Executors 返回的線程池,因爲默認的線程池都存在一定的問題。本文主要從以下幾個方面進行總結
1.默認線程池的問題
2.線程池的核心參數
3.線程池的相關問題
4.手動創建線程池
默認線程池的問題
如果使用 Executors 去創建線程池,使用阿里巴巴的插件會自動進行提示,
提示如下 :
說明 Java,默認提供的4種線程池創建方式都是不安全的。先看下默認的線程池創建方式的問題:
單線程線程池
ExecutorService singleThreadPool = Executors.newSingleThreadExecutor(Executors.defaultThreadFactory());
/**
* Creates an Executor that uses a single worker thread operating
* off an unbounded queue, and uses the provided ThreadFactory to
* create a new thread when needed. Unlike the otherwise
* equivalent {@code newFixedThreadPool(1, threadFactory)} the
* returned executor is guaranteed not to be reconfigurable to use
* additional threads.
*
* @param threadFactory the factory to use when creating new
* threads
*
* @return the newly created single-threaded Executor
* @throws NullPointerException if threadFactory is null
*/
public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(),
threadFactory));
}
再向下跟蹤
/**
* Creates a new {@code ThreadPoolExecutor} with the given initial
* parameters and default rejected execution handler.
*
* @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
* @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} is null
*/
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory) {
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
threadFactory, defaultHandler);
}
可以看到內部調用了線程池的核心創建方法,newSingleThreadExecutor 創建出來的單線程線程池 最主要的問題,是因爲使用了 new LinkedBlockingQueue<Runnable>() 作爲等待隊列,該隊列爲無界隊列,會導致堆積大量請求線程,從而導致OOM.
固定大小線程池
ExecutorService fixedThreadPool = Executors.newFixedThreadPool(10,Executors.defaultThreadFactory());
/**
* Creates a thread pool that reuses a fixed number of threads
* operating off a shared unbounded queue, using the provided
* ThreadFactory to create new threads when needed. At any point,
* at most {@code nThreads} threads will be active processing
* tasks. If additional tasks are submitted when all threads are
* active, they will wait in the queue until a thread is
* available. If any thread terminates due to a failure during
* execution prior to shutdown, a new one will take its place if
* needed to execute subsequent tasks. The threads in the pool will
* exist until it is explicitly {@link ExecutorService#shutdown
* shutdown}.
*
* @param nThreads the number of threads in the pool
* @param threadFactory the factory to use when creating new threads
* @return the newly created thread pool
* @throws NullPointerException if threadFactory is null
* @throws IllegalArgumentException if {@code nThreads <= 0}
*/
public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(),
threadFactory);
也和單線程線程池一樣的問題, 是因爲使用了 new LinkedBlockingQueue<Runnable>() 作爲等待隊列,該隊列爲無界隊列,會導致堆積大量請求線程,從而導致OOM.
緩存型線程池
ExecutorService cachedThreadPool = Executors.newCachedThreadPool(Executors.defaultThreadFactory());
向下跟蹤
/**
* Creates a thread pool that creates new threads as needed, but
* will reuse previously constructed threads when they are
* available, and uses the provided
* ThreadFactory to create new threads when needed.
* @param threadFactory the factory to use when creating new threads
* @return the newly created thread pool
* @throws NullPointerException if threadFactory is null
*/
public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>(),
threadFactory);
線程池的最大線程大小 max 爲 Integer 上限,會創建大量的等待線程,從而引發OOM
延遲執行線程池
public void scheduleThreadPool() throws Exception{ ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(10, Executors.defaultThreadFactory()); scheduledExecutorService.schedule(new Runnable() { @Override public void run() { System.out.println("666" + new Date()); } }, 4, TimeUnit.SECONDS); scheduledExecutorService.scheduleWithFixedDelay(new Runnable() { @Override public void run() { System.out.println("777" + new Date()); } }, 1, 4, TimeUnit.SECONDS); Thread.sleep(1000 * 60); scheduledExecutorService.shutdown(); }
向下跟蹤代碼:
/**
* Creates a thread pool that can schedule commands to run after a
* given delay, or to execute periodically.
* @param corePoolSize the number of threads to keep in the pool,
* even if they are idle
* @param threadFactory the factory to use when the executor
* creates a new thread
* @return a newly created scheduled thread pool
* @throws IllegalArgumentException if {@code corePoolSize < 0}
* @throws NullPointerException if threadFactory is null
*/
public static ScheduledExecutorService newScheduledThreadPool(
int corePoolSize, ThreadFactory threadFactory) {
return new ScheduledThreadPoolExecutor(corePoolSize, threadFactory);
}
跟蹤 ScheduledThreadPoolExecutor(corePoolSize, threadFactory); 構造方法:
/**
* Creates a new {@code ScheduledThreadPoolExecutor} with the
* given core pool size.
*
* @param corePoolSize the number of threads to keep in the pool, even
* if they are idle, unless {@code allowCoreThreadTimeOut} is set
* @throws IllegalArgumentException if {@code corePoolSize < 0}
*/
public ScheduledThreadPoolExecutor(int corePoolSize) {
super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
new DelayedWorkQueue());
}
最後看到 線程池的最大線程大小 max 爲 Integer 上限,會創建大量的等待線程,從而引發OOM
結論:
說明默認的4種線程池都多多少少存在問題 !!
============================
線程池的核心參數
看到上面的默認線程池都用到了 ThreadPoolExecutor 這個類,這個類也是手動創建線程的核心類
return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), threadFactory);
看下最後的最終構造函數:
/**
* 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;
}
線程池的核心參數爲以下7個
int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler
下面一一進行解釋
-
int corePoolSize
核心線程數,當有任務進來的時候,如果當前線程數還未達到 corePoolSize 個數,則創建核心線。
默認情況下,線程池中並沒有任何線程,而是等待有任務到來才創建線程去執行任務,
核心線程有幾個特點:
1、當線程數未達到核心線程最大值的時候,新任務進來,即使有空閒線程,也不會複用,仍然新建核心線程;
2、核心線程一般不會被銷燬,即使是空閒的狀態,但是如果通過方法 allowCoreThreadTimeOut(boolean value) 設置爲 true 時,超時也同樣會被銷燬;
3、生產環境首次初始化的時候,可以調用 prestartCoreThread() / prestartAllCoreThreads() 方法 ,來預先創建所有核心線程,避免第一次調用緩慢;
-
int maximumPoolSize
除了有核心線程外,有些策略是當核心線程佔滿(無空閒)的時候,還會創建一些臨時的線程來處理任務,maximumPoolSize 就是核心線程 + 臨時線程的最大上限。臨時線程有一個超時機制,超過了設置的空閒時間沒有事兒幹,就會被銷燬
-
long keepAliveTime
表示線程沒有任務執行時最多保持多久時間會終止。默認情況下,只有當線程池中的線程數大於corePoolSize時,keepAliveTime纔會起作用,直到線程池中的線程數不大於corePoolSize,即當線程池中的線程數大於corePoolSize時,如果一個線程空閒的時間達到keepAliveTime,則會終止,直到線程池中的線程數不超過corePoolSize。
但是如果調用了allowCoreThreadTimeOut(boolean)方法,在線程池中的線程數不大於corePoolSize時,keepAliveTime參數也會起作用,直到線程池中的線程數爲0;
-
TimeUnit unit
參數keepAliveTime的時間單位,有7種取值,在TimeUnit類中有7種靜態屬性:
TimeUnit.DAYS; //天
TimeUnit.HOURS; //小時
TimeUnit.MINUTES; //分鐘
TimeUnit.SECONDS; //秒
TimeUnit.MILLISECONDS; //毫秒
TimeUnit.MICROSECONDS; //微妙
TimeUnit.NANOSECONDS; //納秒
-
BlockingQueue<Runnable> workQueue
一個阻塞隊列,用來存儲等待執行的任務,這個參數的選擇也很重要,會對線程池的運行過程產生重大影響。
隊列分爲有界隊列和無界隊列。
有界隊列:隊列的長度有上限,當核心線程滿載的時候,新任務進來進入隊列,當達到上限,有沒有核心線程去即時取走處理,這個時候,就會創建臨時線程。(警惕臨時線程無限增加的風險)
無界隊列:隊列沒有上限的,當沒有核心線程空閒的時候,新來的任務可以無止境的向隊列中添加,而永遠也不會創建臨時線程。(警惕任務隊列無限堆積的風險)
除此之外,這裏的阻塞隊列有以下幾種選擇:
1、ArrayBlockingQueue:基於數組的先進先出,創建時必須指定大小,超出直接corePoolSize個任務,則加入到該隊列中,只能加該queue設置的大小,其餘的任務則創建線程,直到(corePoolSize+新建線程)> maximumPoolSize。
2、LinkedBlockingQueue:基於鏈表的先進先出,無界隊列。超出直接corePoolSize個任務,則加入到該隊列中,直到資源耗盡。
3、SynchronousQueue:這個隊列比較特殊,它不會保存提交的任務,而是將直接新建一個線程來執行新來的任務。
ArrayBlockingQueue和PriorityBlockingQueue使用較少,一般使用LinkedBlockingQueue 和 Synchronous。線程池的排隊策略與BlockingQueue有關。
-
ThreadFactory threadFactory
它是一個接口,用於實現生成線程的方式、定義線程名格式、是否後臺執行等等.
可以用 Executors.defaultThreadFactory() 默認的實現即可,
也可以用 Guava 等三方庫提供的方法實現,
如果有特殊要求的話可以自己定義。它最重要的地方應該就是定義線程名稱的格式,便於排查問題了吧
-
RejectedExecutionHandler handler
當沒有空閒的線程處理任務,並且等待隊列已滿(當然這隻對有界隊列有效),再有新任務進來的話,就要做一些取捨了,而這個參數就是指定取捨策略的,有下面四種策略可以選擇:
ThreadPoolExecutor.AbortPolicy:直接拋出異常 RejectedExecutionException ,這是默認策略;
ThreadPoolExecutor.DiscardPolicy:直接丟棄任務,但是不拋出異常。
ThreadPoolExecutor.DiscardOldestPolicy:丟棄隊列最前面的任務,然後將新來的任務加入等待隊列
ThreadPoolExecutor.CallerRunsPolicy:由調用線程處理該任務,並提供一種簡單的反饋機制,可以有效防止新任務的提交。比如在 main 函數中提交線程,如果執行此策略,將有 main 線程來執行該任務
ThreadPoolExecutor.AbortPolicy:直接拋出異常 RejectedExecutionException ,這是默認策略; Java 提供的4種默認實現的線程池都是使用的這種策略。
線程池的相關問題
線程池相關方法
線程池也提供了一些相關的方法,大致如下:
execute()
submit()
shutdown()
shutdownNow()
還有很多其他的方法:
比如:getQueue() 、getPoolSize() 、getActiveCount()、getCompletedTaskCount()等獲取與線程池相關屬性的方法,有興趣的朋友可以自行查閱API。
execute()
execute()方法實際上是Executor中聲明的方法,在ThreadPoolExecutor進行了具體的實現,這個方法是ThreadPoolExecutor的核心方法,通過這個方法可以向線程池提交一個任務,交由線程池去執行。
submit()
submit()方法是在ExecutorService中聲明的方法,在AbstractExecutorService就已經有了具體的實現,在ThreadPoolExecutor中並沒有對其進行重寫,這個方法也是用來向線程池提交任務的,但是它和execute()方法不同,它能夠返回任務執行的結果,去看submit()方法的實現,會發現它實際上還是調用的execute()方法,只不過它利用了Future來獲取任務執行結果(Future相關內容將在下一篇講述)。
shutdown()
shutdown() 提供一種有序的關機,會等待當前緩存隊列任務全部執行完成纔會關閉,但不會再接收新的任務(相對較優雅)。
shutdownNow()
shutdownNow() 會立即關閉線程池,會打斷正在執行的任務並且會清空緩存隊列中的任務,返回的是尚未執行的任務。
corePoolSize與maximumPoolSize關係
1、池中線程數小於corePoolSize,新任務都不排隊而是直接添加新線程
2、池中線程數大於等於corePoolSize,workQueue未滿,首選將新任務加入workQueue而不是添加新線程
3、池中線程數大於等於corePoolSize,workQueue已滿,但是線程數小於maximumPoolSize,添加新的線程來處理被添加的任務
4、池中線程數大於大於corePoolSize,workQueue已滿,並且線程數大於等於maximumPoolSize,新任務被拒絕,使用handler處理被拒絕的任務
手動創建線程池
下面演示下如何手動創建線程池:
這裏我們使用的 Guava 的 ThreadFactory, 相關的 pom
<dependencies> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>14.0.1</version> </dependency> </dependencies>
線程池創建代碼
ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("thread-call-runner-%d").build();
ExecutorService taskExe = new ThreadPoolExecutor(1, 1, 200L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());
創建了1個線程池 coreSize 1, maxSize 1, 使用有界等待隊列初始大小爲1, 傳遞Guava 創建的線程工廠(主要是爲了給線程命名), 拒絕策略爲直接拋出異常
測試代碼
package thread.pool;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import java.util.concurrent.*;
/**
* Created by szh on 2020/6/8.
*/
public class ThreadPoolManual {
public static int i = 1;
public static volatile boolean flag = false;
public static void main(String[] args) {
ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("thread-call-runner-%d").build();
ExecutorService taskExe = new ThreadPoolExecutor(1, 1, 200L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());
taskExe.submit(new Thread(() -> {
while (ThreadPoolManual.i <= 99) {
if (ThreadPoolManual.flag == false) {
System.out.println(Thread.currentThread().getName() + " " + i);
ThreadPoolManual.i++;
ThreadPoolManual.flag = true;
}
}
}));
taskExe.submit(new Thread(() -> {
while (ThreadPoolManual.i <= 100) {
if (ThreadPoolManual.flag == true) {
System.out.println(Thread.currentThread().getName() + " " + i);
ThreadPoolManual.i++;
ThreadPoolManual.flag = false;
}
}
}));
taskExe.submit(new Runnable() {
@Override
public void run() {
System.out.println("xxxxx");
}
}
);
}
}
分析
總共3個線程,2個線程 交替打印 0~ 100, 格外並提交了1個線程用來干擾,
因爲線程池當前運行一個線程1,另一個線程處於等待隊列,第3個線程觸發了拒絕策略。
輸出
Exception in thread "main" java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.FutureTask@5b480cf9 rejected from java.util.concurrent.ThreadPoolExecutor@6f496d9f[Running, pool size = 1, active threads = 1, queued tasks = 1, completed tasks = 0]
at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2047)
at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:823)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1369)
at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:112)
at thread.pool.ThreadPoolManual.main(ThreadPoolManual.java:42)
thread-call-runner-0 1