今天還是說一下線程池的兩個思考。
池子
我們常用的線程池,
CompletableFuture.supplyAsync(()- >{ return "hello word";});
- 還有Tomcat中的線程池
org.apache.tomcat.util.threads.TaskQueue
org.apache.tomcat.util.threads.ThreadPoolExecutor
線程池維護多個線程,等待監督管理者分配可并發執行的任務。這種做法,一方面避免了處理任務時創建銷毀線程開銷的代價,另一方面避免了線程數量膨脹導致的過分調度問題,保證了對內核的充分利用。
JDK 線程池
public ThreadPoolExecutor(
int corePoolSize, //核心線程數
int maximumPoolSize,//最大線程數
long keepAliveTime, //大于核心線程數量的線程存活時間,如果沒有新任務就會關閉
TimeUnit unit, // 時間單位
BlockingQueue< Runnable > workQueue, //線程等待隊列
ThreadFactory threadFactory,//創建線程的工廠
RejectedExecutionHandler handler//拒絕策略
) {
JDK線程池執行任務:
- 提交任務給線程池后,線程池會檢查線程池中正在運行的線程數量,如果線程數量小于核心線程,則創建一個新的線程來處理任務。
- 如果線程池中線程數量達到和corePoolSize的大小,則將線程放入等待隊列BlockingQueue中。
- 如果提交任務時連等待隊列都已經滿了的話,線程池會繼續創建新的線程來處理任務,直到線程池數量達到maximumPoolSize。
- 如果線程數量達到了最大容量,則會執行拒絕策略。
這里需要注意直接使用LinkedBlockingQueue阻塞隊列作為線程池會存在一個問題,當workcount > corePool時優先進入隊列排隊, 當請求并發過多時會導致請求緩慢,隊列太長可能會出現內存溢出(先排隊再漲線程池)
Tomcat線程池
下面時Tomcat線程池的構造方法
public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue< Runnable > workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) {
this.ctl = new AtomicInteger(ctlOf(-536870912, 0));
this.mainLock = new ReentrantLock();
this.workers = new HashSet();
this.termination = this.mainLock.newCondition();
this.submittedCount = new AtomicInteger(0);
this.lastContextStoppedTime = new AtomicLong(0L);
this.lastTimeThreadKilledItself = new AtomicLong(0L);
this.threadRenewalDelay = 1000L;
if (corePoolSize >= 0 && maximumPoolSize > 0 && maximumPoolSize >= corePoolSize && keepAliveTime >= 0L) {
if (workQueue != null && threadFactory != null && handler != null) {
this.corePoolSize = corePoolSize;
this.maximumPoolSize = maximumPoolSize;
this.workQueue = workQueue;
this.keepAliveTime = unit.toNanos(keepAliveTime);
this.threadFactory = threadFactory;
this.handler = handler;
this.prestartAllCoreThreads();
} else {
throw new NullPointerException();
}
} else {
throw new IllegalArgumentException();
}
}
Tomcat主要針對web接口請求,不能因為LinkedBlockingQueue的排隊導致接口出現大量延遲和緩慢, 從而使用了tomcat的TaskQueue,TaskQueue繼承了JDK的LinkedBlockingQueue 并擴展了JDK線程池的功能。
主要有一下幾點優化:
- Tomcat的ThreadPoolExecutor使用的TaskQueue,是無界的LinkedBlockingQueue,但是通過taskQueue的offer方法覆蓋了LinkedBlockingQueue的offer方法,修改了線程池增長規則,使得線程池能在任務較多的情況下增長線程池數量。(先漲線程池再排隊。)
- Tomcat的ThreadPoolExecutor改寫了execute方法,當任務被reject時,捕獲異常,會強制入隊
public void execute(Runnable command, long timeout, TimeUnit unit) {
this.submittedCount.incrementAndGet();
try {
this.executeInternal(command);
} catch (RejectedExecutionException var9) {
if (!(this.getQueue() instanceof TaskQueue)) {
this.submittedCount.decrementAndGet();
throw var9;
}
TaskQueue queue = (TaskQueue)this.getQueue();
try {
if (!queue.force(command, timeout, unit)) {
this.submittedCount.decrementAndGet();
throw new RejectedExecutionException(sm.getString("threadPoolExecutor.queueFull"));
}
} catch (InterruptedException var8) {
this.submittedCount.decrementAndGet();
throw new RejectedExecutionException(var8);
}
}
}
那個線程池適合
我們看看AI如何回復
了不起認為大多數情況下使用JDK的線程池就夠用了,如果覺得線程數據處理不過來,需要多一點線程直接增加核心線程數量設置就可以了。針對資源比較緊張,對線程使用代價比較高時可以考慮。
tomcat對線程池做過優化,也必然是有一定的考量,對于線程資源的使用頻率比較高的情況下可以使用。
-
內核
+關注
關注
3文章
1382瀏覽量
40422 -
接口
+關注
關注
33文章
8691瀏覽量
151913 -
線程池
+關注
關注
0文章
57瀏覽量
6893 -
JDK
+關注
關注
0文章
82瀏覽量
16636 -
tomcat
+關注
關注
0文章
30瀏覽量
4871
發布評論請先 登錄
相關推薦
評論