如何在队列排队之前让ThreadPoolExecutor将线程增加到最大数量
| 1 | corePoolSize | int | 核心線程池大小 |
| 2 | maximumPoolSize | int | 最大線程池大小 |
| 3 | keepAliveTime | long | 線程最大空閑時間 |
| 4 | unit | TimeUnit | 時間單位 |
| 5 | workQueue | BlockingQueue | 線程等待隊列 |
| 6 | threadFactory | ThreadFactory | 線程創建工廠 |
| 7 | handler | RejectedExecutionHandler | 拒絕策略 |
下面是一些源代碼的實現:
? ?public void execute(Runnable command) {if (command == null)throw new NullPointerException(); ?int c = ctl.get();if (workerCountOf(c) < corePoolSize) {if (addWorker(command, true))return;c = ctl.get();}if (isRunning(c) && workQueue.offer(command)) {int recheck = ctl.get();if (! isRunning(recheck) && remove(command))reject(command);else if (workerCountOf(recheck) == 0)addWorker(null, false);}else if (!addWorker(command, false))reject(command);}ArrayBlockingQueue.offer(E e)
? ?public boolean offer(E e) {checkNotNull(e);final ReentrantLock lock = this.lock;lock.lock();try {if (count == items.length)return false;else {enqueue(e);return true;}} finally {lock.unlock();}} ?我們可以總結出線程池默認的工作行為:
-
不會初始化 corePoolSize 個線程,有任務來了才創建工作線程;
-
當核心線程滿了之后不會立即擴容線程池,而是把任務堆積到工作隊列中;
-
當工作隊列滿了后擴容線程池,一直到線程個數達到 maximumPoolSize 為止;
-
如果隊列已滿且達到了最大線程后還有任務進來,按照拒絕策略處理;
-
當線程數大于核心線程數時,線程等待 keepAliveTime 后還是沒有任務需要處理的話,收縮線程到核心線程數。
了解這個策略,有助于我們根據實際的容量規劃需求,為線程池設置合適的初始化參數。當然,我們也可以通過一些手段來改變這些默認工作行為,比如:
-
聲明線程池后立即調用 prestartAllCoreThreads 方法,來啟動所有核心線程;
-
傳入 true 給 allowCoreThreadTimeOut 方法,來讓線程池在空閑的時候同樣回收核心線程。
不知道你有沒有想過:Java 線程池是先用工作隊列來存放來不及處理的任務,滿了之后再擴容線程池。當我們的工作隊列設置的很大時,最大線程數這個參數顯得沒有意義,因為隊列很難滿,或者到滿的時候再去擴容線程池已經于事無補了。
?
那么,我們有沒有辦法讓線程池更激進一點,優先開啟更多的線程,而把隊列當成一個后備方案呢?
下面我們自己實現一個ThreadPoolExecutor,重寫某些方法。
public class MyThreadPoolExecutor extends ThreadPoolExecutor { ?/*** 構造方法** @param corePoolSize ? 核心線程數* @param maximumPoolSize 最大線程數* @param keepAliveTime ? 非核心線程數保留時長* @param unit ? ? ? ? ? 非核心線程數保留時長單位* @param blockQueueSize 阻塞隊列長度*/public MyThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, int blockQueueSize) {super(corePoolSize, maximumPoolSize, keepAliveTime, unit, new MyThreadPoolExecutor.ExtremeBlockQueue<>(blockQueueSize), Executors.defaultThreadFactory(), new MyThreadPoolExecutor.ExtremePolicy()); ?} ?/*** 自定義阻塞隊列** @param <Runnable>*/static class ExtremeBlockQueue<Runnable> extends LinkedBlockingQueue<Runnable> {public ExtremeBlockQueue(int capacity) {super(capacity);} ?/*** 覆蓋默認的offer方法,觸發拒絕策略執行** @param e* @return*/@Overridepublic boolean offer(Runnable e) {if (size() == 0) {return super.offer(e);} else {return false;}} ?} ?/*** 自定義拒絕策略*/static class ExtremePolicy implements RejectedExecutionHandler {@Overridepublic void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {try {executor.getQueue().put(r);if (executor.isShutdown()) {throw new RejectedExecutionException("Task " + r + " rejected from " + executor);}} catch (InterruptedException e) {Thread.currentThread().interrupt();return;}}} }我們創建一個自定義線程池
ExecutorService threadPool = new MyThreadPoolExecutor(1, 50, 5, TimeUnit.SECONDS, ?Integer.MAX_VALUE);通過這種機制,當我將任務提交到隊列時,ThreadPoolExecutor將:
最初將線程數擴展到核心大小(此處為1)。
將其提供給隊列。如果隊列為空,它將排隊等待由現有線程處理。
如果隊列中已經有1個或多個元素,offer(...)將返回false。
如果返回false,則按比例擴大池中的線程數量,直到達到最大數量(此處為50)。
如果達到最大值,則調用 RejectedExecutionHandler
在RejectedExecutionHandler隨后會將任務到隊列通過FIFO順序交給第一個可用線程處理。
盡管在上面的示例代碼中,隊列是無界的,但是您也可以將其定義為有界隊列。例如,如果將容量添加到1000,LinkedBlockingQueue則它將:
將線程放大到最大
然后排隊直到完成1000個任務
然后阻塞,直到隊列可用為止。
?
我們還可以使用一個最簡單的方法,將核心線程大小和最大線程大小設置為相同的值,并使用允許從池中刪除核心線程allowCoreThreadTimeOut(true)。
?
下面我們寫一個demo演示一下:
我們創建一個Spring項目,用戶在瀏覽器調用該方法:
? ?@GetMapping("right")public int right() throws InterruptedException {AtomicInteger atomicInteger = new AtomicInteger();ThreadPoolExecutor threadPool = new MyThreadPoolExecutor(2, 5,5, TimeUnit.SECONDS,10);printStats(threadPool);IntStream.rangeClosed(1, 20).forEach(i -> {try {TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}int id = atomicInteger.incrementAndGet();try {threadPool.submit(() -> {log.info("{} started", id);try {TimeUnit.SECONDS.sleep(10);} catch (InterruptedException e) {}log.info("{} finished", id);});} catch (Exception ex) {log.error("error submitting task {}", id, ex);atomicInteger.decrementAndGet();}}); ?TimeUnit.SECONDS.sleep(60);return atomicInteger.intValue();} ?/*** 定時任務每秒打印當前線程池的狀態** @param threadPool*/private void printStats(ThreadPoolExecutor threadPool) {Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(() -> {log.info("=========================");log.info("Pool Size: {}", threadPool.getPoolSize());log.info("Active Threads: {}", threadPool.getActiveCount());log.info("Number of Tasks Completed: {}", threadPool.getCompletedTaskCount());log.info("Number of Tasks in Queue: {}", threadPool.getQueue().size()); ?log.info("=========================");}, 0, 1, TimeUnit.SECONDS);}下面是日志的一部分輸出:
[16:33:43.453] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:22 ] - ========================= [16:33:43.453] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:23 ] - Pool Size: 0 [16:33:43.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:24 ] - Active Threads: 0 [16:33:43.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:25 ] - Number of Tasks Completed: 0 [16:33:43.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:26 ] - Number of Tasks in Queue: 0 [16:33:43.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:28 ] - ========================= [16:33:44.453] [pool-5-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:90 ] - 1 started [16:33:44.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:22 ] - ========================= [16:33:44.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:23 ] - Pool Size: 1 [16:33:44.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:24 ] - Active Threads: 1 [16:33:44.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:25 ] - Number of Tasks Completed: 0 [16:33:44.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:26 ] - Number of Tasks in Queue: 0 [16:33:44.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:28 ] - ========================= [16:33:45.453] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:22 ] - ========================= [16:33:45.453] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:23 ] - Pool Size: 2 [16:33:45.453] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:24 ] - Active Threads: 2 [16:33:45.453] [pool-5-thread-2] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:90 ] - 2 started [16:33:45.453] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:25 ] - Number of Tasks Completed: 0 [16:33:45.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:26 ] - Number of Tasks in Queue: 0 [16:33:45.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:28 ] - ========================= [16:33:46.456] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:22 ] - ========================= [16:33:46.456] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:23 ] - Pool Size: 2 [16:33:46.456] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:24 ] - Active Threads: 2 [16:33:46.457] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:25 ] - Number of Tasks Completed: 0 [16:33:46.457] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:26 ] - Number of Tasks in Queue: 0 [16:33:46.457] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:28 ] - ========================= [16:33:47.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:22 ] - ========================= [16:33:47.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:23 ] - Pool Size: 2 [16:33:47.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:24 ] - Active Threads: 2 [16:33:47.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:25 ] - Number of Tasks Completed: 0 [16:33:47.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:26 ] - Number of Tasks in Queue: 1 [16:33:47.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:28 ] - ========================= [16:33:47.457] [pool-5-thread-3] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:90 ] - 4 started [16:33:48.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:22 ] - ========================= [16:33:48.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:23 ] - Pool Size: 3 [16:33:48.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:24 ] - Active Threads: 3 [16:33:48.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:25 ] - Number of Tasks Completed: 0 [16:33:48.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:26 ] - Number of Tasks in Queue: 1 [16:33:48.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:28 ] - ========================= [16:33:48.457] [pool-5-thread-4] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:90 ] - 5 started [16:33:49.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:22 ] - ========================= [16:33:49.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:23 ] - Pool Size: 4 [16:33:49.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:24 ] - Active Threads: 4 [16:33:49.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:25 ] - Number of Tasks Completed: 0 [16:33:49.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:26 ] - Number of Tasks in Queue: 1 [16:33:49.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:28 ] - ========================= [16:33:49.458] [pool-5-thread-5] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:90 ] - 6 started [16:33:50.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:22 ] - ========================= [16:33:50.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:23 ] - Pool Size: 5 [16:33:50.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:24 ] - Active Threads: 5 [16:33:50.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:25 ] - Number of Tasks Completed: 0 [16:33:50.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:26 ] - Number of Tasks in Queue: 1 [16:33:50.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:28 ] - ========================= [16:33:51.453] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:22 ] - ========================= [16:33:51.453] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:23 ] - Pool Size: 5 [16:33:51.453] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:24 ] - Active Threads: 5 [16:33:51.453] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:25 ] - Number of Tasks Completed: 0 [16:33:51.453] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:26 ] - Number of Tasks in Queue: 2 [16:33:51.453] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:28 ] - ========================= [16:33:52.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:22 ] - ========================= [16:33:52.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:23 ] - Pool Size: 5 [16:33:52.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:24 ] - Active Threads: 5 [16:33:52.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:25 ] - Number of Tasks Completed: 0 [16:33:52.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:26 ] - Number of Tasks in Queue: 3 [16:33:52.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:28 ] - ========================= [16:33:53.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:22 ] - ========================= [16:33:53.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:23 ] - Pool Size: 5 [16:33:53.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:24 ] - Active Threads: 5 [16:33:53.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:25 ] - Number of Tasks Completed: 0 [16:33:53.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:26 ] - Number of Tasks in Queue: 4 [16:33:53.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:28 ] - ========================= [16:33:54.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:22 ] - ========================= [16:33:54.454] [pool-5-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:95 ] - 1 finished [16:33:54.454] [pool-5-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:90 ] - 3 started [16:33:54.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:23 ] - Pool Size: 5 [16:33:54.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:24 ] - Active Threads: 5 [16:33:54.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:25 ] - Number of Tasks Completed: 1 [16:33:54.456] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:26 ] - Number of Tasks in Queue: 4 [16:33:54.456] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:28 ] - ========================= [16:33:55.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:22 ] - ========================= [16:33:55.454] [pool-5-thread-2] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:95 ] - 2 finished [16:33:55.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:23 ] - Pool Size: 5 [16:33:55.454] [pool-5-thread-2] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:90 ] - 7 started [16:33:55.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:24 ] - Active Threads: 5 [16:33:55.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:25 ] - Number of Tasks Completed: 2 [16:33:55.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:26 ] - Number of Tasks in Queue: 4 [16:33:55.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:28 ] - ========================= [16:33:56.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:22 ] - ========================= [16:33:56.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:23 ] - Pool Size: 5 [16:33:56.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:24 ] - Active Threads: 5 [16:33:56.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:25 ] - Number of Tasks Completed: 2 [16:33:56.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:26 ] - Number of Tasks in Queue: 5 [16:33:56.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:28 ] - ========================= [16:33:57.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:22 ] - ========================= [16:33:57.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:23 ] - Pool Size: 5 [16:33:57.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:24 ] - Active Threads: 5 [16:33:57.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:25 ] - Number of Tasks Completed: 2 [16:33:57.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:26 ] - Number of Tasks in Queue: 6 [16:33:57.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:28 ] - ========================= [16:33:57.457] [pool-5-thread-3] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:95 ] - 4 finished [16:33:57.457] [pool-5-thread-3] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:90 ] - 8 started [16:33:58.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:22 ] - ========================= [16:33:58.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:23 ] - Pool Size: 5 [16:33:58.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:24 ] - Active Threads: 5 [16:33:58.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:25 ] - Number of Tasks Completed: 3 [16:33:58.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:26 ] - Number of Tasks in Queue: 6 [16:33:58.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:28 ] - ========================= [16:33:58.458] [pool-5-thread-4] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:95 ] - 5 finished [16:33:58.458] [pool-5-thread-4] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:90 ] - 9 started [16:33:59.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:22 ] - ========================= [16:33:59.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:23 ] - Pool Size: 5 [16:33:59.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:24 ] - Active Threads: 5 [16:33:59.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:25 ] - Number of Tasks Completed: 4 [16:33:59.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:26 ] - Number of Tasks in Queue: 6 [16:33:59.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:28 ] - ========================= [16:33:59.458] [pool-5-thread-5] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:95 ] - 6 finished [16:33:59.458] [pool-5-thread-5] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:90 ] - 10 started [16:34:00.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:22 ] - ========================= [16:34:00.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:23 ] - Pool Size: 5 [16:34:00.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:24 ] - Active Threads: 5 [16:34:00.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:25 ] - Number of Tasks Completed: 5 [16:34:00.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:26 ] - Number of Tasks in Queue: 6 [16:34:00.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:28 ] - ========================= [16:34:01.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:22 ] - ========================= [16:34:01.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:23 ] - Pool Size: 5 [16:34:01.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:24 ] - Active Threads: 5 [16:34:01.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:25 ] - Number of Tasks Completed: 5 [16:34:01.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:26 ] - Number of Tasks in Queue: 7 [16:34:01.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:28 ] - ========================= [16:34:02.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:22 ] - ========================= [16:34:02.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:23 ] - Pool Size: 5 [16:34:02.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:24 ] - Active Threads: 5 [16:34:02.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:25 ] - Number of Tasks Completed: 5 [16:34:02.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:26 ] - Number of Tasks in Queue: 8 [16:34:02.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:28 ] - ========================= [16:34:03.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:22 ] - ========================= [16:34:03.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:23 ] - Pool Size: 5 [16:34:03.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:24 ] - Active Threads: 5 [16:34:03.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:25 ] - Number of Tasks Completed: 5 [16:34:03.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:26 ] - Number of Tasks in Queue: 9 [16:34:03.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:28 ] - ========================= [16:34:04.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:22 ] - ========================= [16:34:04.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:23 ] - Pool Size: 5 [16:34:04.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:24 ] - Active Threads: 5 [16:34:04.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:25 ] - Number of Tasks Completed: 5 [16:34:04.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:26 ] - Number of Tasks in Queue: 10 [16:34:04.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:28 ] - ========================= [16:34:04.455] [pool-5-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:95 ] - 3 finished [16:34:04.455] [pool-5-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:90 ] - 11 started [16:34:05.454] [pool-5-thread-2] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:95 ] - 7 finished [16:34:05.454] [pool-5-thread-2] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:90 ] - 12 started [16:34:05.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:22 ] - ========================= [16:34:05.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:23 ] - Pool Size: 5 [16:34:05.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:24 ] - Active Threads: 5 [16:34:05.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:25 ] - Number of Tasks Completed: 7 [16:34:05.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:26 ] - Number of Tasks in Queue: 8可以看到線程池的線程數量會先從0增加到5,然后當線程數到達最大線程數時,后面的線程會進入到隊列中排隊等待
總結
以上是生活随笔為你收集整理的如何在队列排队之前让ThreadPoolExecutor将线程增加到最大数量的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ThreadLocal线程复用导致的安全
- 下一篇: Spring 声明式事务在业务开发中容易